sql server database pushes data to java interface

Keywords: Database Java Stored Procedure SQL

Because I have never used the database to push data to the java interface before, in order to achieve this demo, I step by step step stepping on the pit!!!

The function and application scenario of this article: using database to push data actively, realizing real-time updating of front-end page data, replacing ajax polling mechanism. Push is based on the fact that the database triggers a trigger and then invokes the message push interface through the stored procedure as long as the data in the table specified by the database changes.

 

The code is as follows:....

     

----------------------------trigger---------------------------------------
USE [devRep]
GO
/****** Object:  Trigger [dbo].[tr_sm_demo]    Script Date: 2019/9/26 14:06:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================


-- Description:	<Insert trigger>
-- =============================================
ALTER TRIGGER [dbo].[tr_sm_demo] 
 
ON [dbo].[demo] 
 
AFTER insert
 
AS 
BEGIN
	declare @id int
 
	set @id = (select id from demo where id=@id)  --If a conditional query is not specified, a sub-query will return a parameter other than a parameter. !<> >>?  A messy mistake is that the return here must be a data.
 
	exec proc_useJPushAPI @id --Calling stored procedures and passing arguments will not trigger if the incoming parameters remain unchanged
 
	SET NOCOUNT ON;
END
----------------------------stored procedure--------------------------------

USE [devRep]
GO
/****** Object:  StoredProcedure [dbo].[proc_useJPushAPI]    Script Date: 2019/9/26 13:56:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================

-- Description:	<Call message push interface>
-- =============================================
CREATE procedure [dbo].[proc_useJPushAPI]
	@id varchar(20)		--demo Table id field
AS
BEGIN
	--Create parameters in stored procedures
	declare @url varchar(4000)	--Interface routing
	declare @object int			--OLE Object instance
	declare @responseText varchar(4000) --text
	declare @name varchar(20)
    select @name=name from demo;  --query demo Table for interface transfer parameters
	set @url = 'http://127.0.0.1:8080/site/recedata?name='+@name ;
	
	print @url
 
	exec sp_OACreate'MSXML2.XMLHTTP',@object out
	exec sp_OAMethod @object,'open',null,'get',@url,'false'
	exec sp_OAMethod @object,'send'
	exec sp_OAMethod @object,'responseText',@responseText output
 
	print @responseText
 
	exec sp_OADestroy @object
 
	SET NOCOUNT ON;
 
END
//Receiving java interface code 
@RequestMapping(value = "/recedata")
    public String recedata( String name) {
        try {
            System.out.println("sql server Visit---");
            return "HELLO";
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
        }
        return null;
    }

Now just execute this sql in demo (name) values ('you're really great') in the database; you can access the interface.

If your name is demo and the @RequestMapping @RequestMapping that receives the java interface is also called demo, then no matter how much you add it, it will not run to this method.

 

 

Make a record.

Pushing websocket with database to receive and render front-end pages should be Wan Ruigoude! ____________

Posted by misterm on Wed, 09 Oct 2019 18:25:05 -0700