使用CallableStatement存储过程查询数据库
package jdbctest01;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import com.lile.jdbc.util.DBUtil;
public class Demo2 {
public static void main(String[] args) {
//连接数据库
Connection conn = DBUtil.getConnection();
PreparedStatement pst = null;
try {
CallableStatement cs=conn.prepareCall("{call sp06(?,?)}");
cs.setInt(1, 1);
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
String bookName = cs.getString("t_bookName");
DBUtil.closeConn(conn, pst);
System.out.println("bookName:"+bookName);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import com.lile.jdbc.util.DBUtil;
public class Demo2 {
public static void main(String[] args) {
//连接数据库
Connection conn = DBUtil.getConnection();
PreparedStatement pst = null;
try {
CallableStatement cs=conn.prepareCall("{call sp06(?,?)}");
cs.setInt(1, 1);
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
String bookName = cs.getString("t_bookName");
DBUtil.closeConn(conn, pst);
System.out.println("bookName:"+bookName);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文介绍了一个使用Java中的CallableStatement来调用数据库存储过程并获取输出参数的具体实例。该示例展示了如何设置输入参数、注册输出参数及执行存储过程。
2694

被折叠的 条评论
为什么被折叠?



