create proc p_1
as
select * from cell
go存储过程p_2----往cell表里插入数据
create proc p_2
@name varchar(40),
@remark varchar(40)
as
insert into cell(name,remark) values(@name,@remark)
go调用存储过程p_1:
import java.sql.*;
/**
* jdbc中调用存储过程
* @author jiafang83
*
*/
public class Procedure2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fish";
String user = "bm";
String pwd = "bm";
Connection conn = null;
CallableStatement proc = null;//执行sql存储过程的接口
ResultSet rs = null;
// 调用存储过程p_1:从Cell表中取出数据
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url,user,pwd);
proc = conn.prepareCall("{? = call p_1 ()}");
proc.registerOutParameter(1, Types.REAL);
rs = proc.executeQuery();//取得结果集
while(rs.next()){
System.out.println(rs.getString("name"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null) rs.close();
if(proc!=null)proc.close();
if(conn!=null)conn.close();
}
}
}调用存储过程p_2:
import java.sql.*;
/**
* jdbc中调用存储过程
* @author jiafang83
*
*/
public class Procedure {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fish";
String user = "bm";
String pwd = "bm";
Connection conn = null;
CallableStatement proc = null;//执行sql存储过程的接口
// 调用存储过程p_2:向Cell表中插入数据
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url,user,pwd);
proc = conn.prepareCall("{call p_2 (?,?)}");
proc.setString(1, "测试");
proc.setString(2, "测试");
proc.execute();
System.out.println("成功调用存储过程,插入数据!");
}catch(Exception e){
e.printStackTrace();
}finally{
if(proc!=null)proc.close();
if(conn!=null)conn.close();
}
}
posted on 2008-11-20 14:25 jiafang83 阅读(2770) 评论(0) 编辑 收藏