JDBC调用存储过程
CallableStatement(从PreperedStatement扩展来)
cs = connection.prepareCall(“{call psname(?,?,?)}”);
cs.registerOutParameter(index, Types.INTEGER);//注册存储过程的输出参数
cs.setXXX(i, xxxx);
cs.executeUpdate();
int id=cs.getInt(index);//得到存储过程的输出参数
public class PsTest {
public static void main(String[] args) throws SQLException {
ps();
}
static void ps() throws SQLException {
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
String sql = "{ call addUser(?,?,?,?) } ";
cs = conn.prepareCall(sql);
cs.registerOutParameter(4, Types.INTEGER);//注册输出参数
cs.setString(1, "ps name");
cs.setDate(2, new java.sql.Date(System.currentTimeMillis()));
cs.setFloat(3, 100f);
cs.executeUpdate();
int id = cs.getInt(4);//得到输出参数
System.out.println("id=" + id);
} finally {
JdbcUtils.free(rs, cs, conn);
}
}
}