当前位置: 技术问答>java相关
java怎样执行sqlserver的存储过程
来源: 互联网 发布时间:2017-05-03
本文导语: 不知哪个位高手知道怎么样在java种执行sqlserver的存储过程 | import java.sql.*; /* *create proc returnvaluetest as begin return 1234 end create proc outvaluetest @outshort smallint output, @outint int output, @outstr...
不知哪个位高手知道怎么样在java种执行sqlserver的存储过程
|
import java.sql.*;
/*
*create proc returnvaluetest
as
begin
return 1234
end
create proc outvaluetest
@outshort smallint output,
@outint int output,
@outstr varchar(10) output
as
begin
set @outshort=123
set @outint=1234567
set @outstr='aaabbb'
return 1234
end
GO
CREATE PROCEDURE inserttable1
@name char(10)
AS
insert into table1(createdate,fixlenstr) values(getdate(),@name)
return @@IDENTITY
GO
**/
public class StoredProc
{
public static void main1(String args[])throws Exception
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
CallableStatement cstmt=null;
cstmt=con.prepareCall("{?=call returnvaluetest()}");
cstmt.registerOutParameter(1,Types.INTEGER);
cstmt.execute();
int returnValue=cstmt.getInt(1);
System.out.println("returnValue:"+returnValue);
cstmt.close();
con.close();
}
public static void main(String args[])throws Exception
{
sqlfirst();
spfirst();
}
public static void spfirst()throws Exception
{
System.out.println();
System.out.println("call sp before sql");
CallableStatement cstmt=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
long lstart=0;
long lstart2=0;
long sptime=0;
long sqltime=0;
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
lstart=System.currentTimeMillis();
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
lstart2=System.currentTimeMillis();
sptime=lstart2-lstart;
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
sqltime=System.currentTimeMillis()-lstart2;
con.close();
System.out.println("first execution time:"+sptime+";second:"+sqltime);
}
public static void sqlfirst()throws Exception
{
System.out.println();
System.out.println("callsql before sp");
CallableStatement cstmt=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
long lstart=0;
long lstart2=0;
long sptime=0;
long sqltime=0;
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
lstart=System.currentTimeMillis();
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
lstart2=System.currentTimeMillis();
sptime=lstart2-lstart;
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
sqltime=System.currentTimeMillis()-lstart2;
con.close();
System.out.println("first execution time:"+sptime+";second:"+sqltime);
}
public static void callsp(Connection con)throws Exception
{
long start=System.currentTimeMillis();
CallableStatement cstmt=con.prepareCall("{?=call inserttable1(?)}");
int index=1;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.setString(index,"aaa");
cstmt.execute();
int returnValue=cstmt.getInt(1);
cstmt.close();
System.out.println("SP returnValue:"+returnValue+" use "+(System.currentTimeMillis()-start));
}
public static int callsql(Connection con)throws Exception
{
long start=System.currentTimeMillis();
String insertsql="INSERT INTO Table1 (createdate, fixlenstr) VALUES (GETDATE(), ?)";
PreparedStatement ps=con.prepareStatement(insertsql);
ps.setString(1,"aaa");
ps.executeUpdate();
String getautoidsql="SELECT max(autoid) from table1";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(getautoidsql);
int ret=0;
if(rs.next())
ret=rs.getInt(1);
rs.close();
ps.close();
st.close();
System.out.println("SQL ret:"+ret+" use "+(System.currentTimeMillis()-start));
return ret;
}
public static void main2(String args[])throws Exception
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
CallableStatement cstmt=null;
cstmt=con.prepareCall("{?=call outvaluetest(?,?,?)}");
int index=1;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.registerOutParameter(index,Types.VARCHAR); index++;
cstmt.execute();
int returnValue=cstmt.getInt(1);
System.out.println("returnValue:"+returnValue);
System.out.println("short value:"+cstmt.getShort(2));
System.out.println("int value:"+cstmt.getInt(3));
System.out.println("string value:"+cstmt.getString(4));
cstmt.close();
con.close();
}
}
/*
*create proc returnvaluetest
as
begin
return 1234
end
create proc outvaluetest
@outshort smallint output,
@outint int output,
@outstr varchar(10) output
as
begin
set @outshort=123
set @outint=1234567
set @outstr='aaabbb'
return 1234
end
GO
CREATE PROCEDURE inserttable1
@name char(10)
AS
insert into table1(createdate,fixlenstr) values(getdate(),@name)
return @@IDENTITY
GO
**/
public class StoredProc
{
public static void main1(String args[])throws Exception
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
CallableStatement cstmt=null;
cstmt=con.prepareCall("{?=call returnvaluetest()}");
cstmt.registerOutParameter(1,Types.INTEGER);
cstmt.execute();
int returnValue=cstmt.getInt(1);
System.out.println("returnValue:"+returnValue);
cstmt.close();
con.close();
}
public static void main(String args[])throws Exception
{
sqlfirst();
spfirst();
}
public static void spfirst()throws Exception
{
System.out.println();
System.out.println("call sp before sql");
CallableStatement cstmt=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
long lstart=0;
long lstart2=0;
long sptime=0;
long sqltime=0;
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
lstart=System.currentTimeMillis();
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
lstart2=System.currentTimeMillis();
sptime=lstart2-lstart;
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
sqltime=System.currentTimeMillis()-lstart2;
con.close();
System.out.println("first execution time:"+sptime+";second:"+sqltime);
}
public static void sqlfirst()throws Exception
{
System.out.println();
System.out.println("callsql before sp");
CallableStatement cstmt=null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
long lstart=0;
long lstart2=0;
long sptime=0;
long sqltime=0;
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
lstart=System.currentTimeMillis();
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
callsql(con);
lstart2=System.currentTimeMillis();
sptime=lstart2-lstart;
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
callsp(con);
sqltime=System.currentTimeMillis()-lstart2;
con.close();
System.out.println("first execution time:"+sptime+";second:"+sqltime);
}
public static void callsp(Connection con)throws Exception
{
long start=System.currentTimeMillis();
CallableStatement cstmt=con.prepareCall("{?=call inserttable1(?)}");
int index=1;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.setString(index,"aaa");
cstmt.execute();
int returnValue=cstmt.getInt(1);
cstmt.close();
System.out.println("SP returnValue:"+returnValue+" use "+(System.currentTimeMillis()-start));
}
public static int callsql(Connection con)throws Exception
{
long start=System.currentTimeMillis();
String insertsql="INSERT INTO Table1 (createdate, fixlenstr) VALUES (GETDATE(), ?)";
PreparedStatement ps=con.prepareStatement(insertsql);
ps.setString(1,"aaa");
ps.executeUpdate();
String getautoidsql="SELECT max(autoid) from table1";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(getautoidsql);
int ret=0;
if(rs.next())
ret=rs.getInt(1);
rs.close();
ps.close();
st.close();
System.out.println("SQL ret:"+ret+" use "+(System.currentTimeMillis()-start));
return ret;
}
public static void main2(String args[])throws Exception
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;"
+ "user=masterz;password=sa;DatabaseName=NorthWind");
CallableStatement cstmt=null;
cstmt=con.prepareCall("{?=call outvaluetest(?,?,?)}");
int index=1;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.registerOutParameter(index,Types.INTEGER); index++;
cstmt.registerOutParameter(index,Types.VARCHAR); index++;
cstmt.execute();
int returnValue=cstmt.getInt(1);
System.out.println("returnValue:"+returnValue);
System.out.println("short value:"+cstmt.getShort(2));
System.out.println("int value:"+cstmt.getInt(3));
System.out.println("string value:"+cstmt.getString(4));
cstmt.close();
con.close();
}
}
|
http://www.fanqiang.com/a4/b5/20010530/180801279.html
-----------------------------------------------------------------
欢迎访问:http://CoolSlob.fykj.com/
本站◆第一主打:《查询利器》,在这里你可以查询CSDN FAQ---目前唯一可查询的站点(CSDN自身也不支持)
◆第二主打:《CSDN精华贴》,在这里你可以看到网友们出奇的搞笑本领;可以看到网友们高深莫测的技术功底:)
◆第三主打:《下载专区》,在这里你可以下载到C++ Primer 3rd这样的经典书籍,可以下载到够Inside的《Insdie SQL Server7.0》,前些时候,我本人就是借助这本书来破解MS SQL Server数据库的.mdf和.ldf文件的:)
◆只要有大家的支持,网站有可能加入搜索“CSDN精华贴”功能!
-----------------------------------------------------------------
欢迎访问:http://CoolSlob.fykj.com/
本站◆第一主打:《查询利器》,在这里你可以查询CSDN FAQ---目前唯一可查询的站点(CSDN自身也不支持)
◆第二主打:《CSDN精华贴》,在这里你可以看到网友们出奇的搞笑本领;可以看到网友们高深莫测的技术功底:)
◆第三主打:《下载专区》,在这里你可以下载到C++ Primer 3rd这样的经典书籍,可以下载到够Inside的《Insdie SQL Server7.0》,前些时候,我本人就是借助这本书来破解MS SQL Server数据库的.mdf和.ldf文件的:)
◆只要有大家的支持,网站有可能加入搜索“CSDN精华贴”功能!