java调用SQLServerd的存储过程
package db;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ExecProduce {
private Connection c;
public ExecProduce() throws SQLException{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1443;DatabaseName=test;user=sa;password=123456";
c = DriverManager.getConnection(url);
CallableStatement cstmt = null;
cstmt = c.prepareCall("{call up_search(?,?,?)}");
cstmt.setInt(1, 12);
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
cstmt.registerOutParameter(3, java.sql.Types.VARCHAR);
cstmt.execute();
String tilte = cstmt.getString(2);
String content = cstmt.getString(3);
System.out.println(tilte);
System.out.print(content);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
ExecProduce test = new ExecProduce();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
执行脚本
USE test;
GO
CREATE PROCEDURE up_search
(@id char(6),
@title varchar(15),
@content varchar(15)
)
AS
SELECT @title = title,
@content = content
FROM dbo.news
WHERE id = @id
GO
调用脚本
USE test;
DECLARE @title varchar(15)
DECLARE @content varchar(15)
EXEC up_search '12',@title OUTPUT,@content OUTPUT
PRINT '新闻标题为:'+@title
PRINT '新闻内容为: '+@content
GO
lunzi
2007-10-09 23:37:59
评论:0
阅读:70
引用:0
