JAVA调用存储过程

上传:ITZYF 浏览: 41 推荐: 0 文件:TXT 大小:2.85KB 上传时间:2019-04-13 04:10:26 版权申诉
注意,以下使用数据库为sql2000,驱动jtds1.2.2 一、调用存储过程(无结果集返回) Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?,?) }"); callableStatement.setString(1, "xxxxxxxx"); callableStatement.setString(2, " ;xxxxxxxx"); callableStatement.execute(); //获得sql的消息并输出,这个估计很多人都需要 SQLWarning sqlWarning = callableStatement.getWarnings(); while (sqlWarning != null) { System.out.println("sqlWarning.getErrorCode() = " + sqlWarning.getErrorCode()); System.out.println("sqlWarning.getSQLState() = " + sqlWarning.getSQLState()); System.out.println("sqlWarning.getMessage() = " + sqlWarning.getMessage()); sqlWarning = sqlWarning.getNextWarning(); } //close ConnectionHelper.closeConnection(callableStatement, connection); 二、调用存储过程,返回sql类型数据(非记录集) Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?,?,?) }"); callableStatement.setString(1, "xxxxxxxx"); callableStatement.setString(2, "xxxxxxxx"); //重点是这句1 callableStatement.registerOutParameter(3, Types.INTEGER); callableStatement.execute(); //取返回结果,重点是这句2 //int rsCount = callableStatement.getInt(3); //close ConnectionHelper.closeConnection(callableStatement, connection); 三、重点来了,返回记录集,多记录集 注意,不需要注册返回结果参数,只需要在sql中select出结果即可 例如:select * from tableName 即可得到返回结果 Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?) }"); //此处参数与结果集返回没有关系 callableStatement.setString(1, "xxxxxxxx"); callableStatement.execute(); ResultSet resultSet = callableStatement.getResultSet(); //以上两个语句,可以使用ResultSet resultSet = callableStatement.executeQuery();替代 //多结果返回 ResultSet resultSet2; if (callableStatement.getMoreResults()) { resultSet2 = callableStatement.getResultSet(); while (resultSet2.next()) { } } //close ConnectionHelper.closeConnection(callableStatement, connection); 提示:多结果返回可以使用如下代码(以上主要让大家明白,单一结果和多结果的区别): Boolean hasMoreResult = true; while (hasMoreResult) { ResultSet resultSet = callableStatement.getResultSet(); while (resultSet.next()) { } hasMoreResult = callableStatement.getMoreResults(); } ;xxxxxxxx"); callableStatement.execute(); //获得sql的消息并输出,这个估计很多人都需要 SQLWarning sqlWarning = callableStatement.getWarnings(); while (sqlWarning != null) { System.out.println("sqlWarning.getErrorCode() = " + sqlWarning.getErrorCode()); System.out.println("sqlWarning.getSQLState() = " + sqlWarning.getSQLState()); System.out.println("sqlWarning.getMessage() = " + sqlWarning.getMessage()); sqlWarning = sqlWarning.getNextWarning(); } //close ConnectionHelper.closeConnection(callableStatement, connection); 二、调用存储过程,返回sql类型数据(非记录集) Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?,?,?) }"); callableStatement.setString(1, "xxxxxxxx"); callableStatement.setString(2, "xxxxxxxx"); //重点是这句1 callableStatement.registerOutParameter(3, Types.INTEGER); callableStatement.execute(); //取返回结果,重点是这句2 //int rsCount = callableStatement.getInt(3); //close ConnectionHelper.closeConnection(callableStatement, connection); 三、重点来了,返回记录集,多记录集 注意,不需要注册返回结果参数,只需要在sql中select出结果即可 例如:select * from tableName 即可得到返回结果 Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?) }"); //此处参数与结果集返回没有关系 callableStatement.setString(1, "xxxxxxxx"); callableStatement.execute(); ResultSet resultSet = callableStatement.getResultSet(); //以上两个语句,可以使用ResultSet resultSet = callableStatement.executeQuery();替代 //多结果返回 ResultSet resultSet2; if (callableStatement.getMoreResults()) { resultSet2 = callableStatement.getResultSet(); while (resultSet2.next()) { } } //close ConnectionHelper.closeConnection(callableStatement, connection); 提示:多结果返回可以使用如下代码(以上主要让大家明白,单一结果和多结果的区别): Boolean hasMoreResult = true; while (hasMoreResult) { ResultSet resultSet = callableStatement.getResultSet(); while (resultSet.next()) { } hasMoreResult = callableStatement.getMoreResults(); }
上传资源
用户评论
相关推荐
java调用存储过程
java,调用存储过程java,调用存储过程java,调用存储过程java,调用存储过程java,调用存储过程java,调用存储过程java,调用存储过程java,调用存储过程java,调用存储过程j
RAR
0B
2019-05-13 13:51
Java调用存储过程
Java调用存储过程 要注意的地方和一些细节说明 资源源于不但搜索,自由源于不但努力
word
0B
2019-01-10 06:40
调用存储过程java
存储过程存储过程存储过程存储过程
RAR
0B
2019-07-08 07:24
存储过程调用存储过程调用
存储过程调用存储过程调用存储过程调用存储过程调用存储过程调用
RAR
0B
2019-05-13 13:51
java调用oracle存储过程
java代码调用oracle的存储过程,非常好用的
DOCX
0B
2019-02-20 20:29
JAVA调用存储过程.txt
JAVA调用存储过程.txtJAVA调用存储过程.txtJAVA调用存储过程.txtJAVA调用存储过程.txt
TXT
0B
2019-05-02 19:19
Java程序调用存储过程
Java程序调用存储过程Java程序调用存储过程Java程序调用存储过程Java程序调用存储过程Java程序调用存储过程
rar
0B
2019-05-13 13:51
java调用存储过程实例
java调用存储过程实例
RAR
0B
2019-05-13 13:51
JAVA调用ORACLE存储过程
自己整理的,JAVA调用ORACLE存储过程例子。
DOC
0B
2019-06-04 18:32
java调用存储过程总结
java调用存储过程总结创建表创建存储过程创建java程序调用
DOC
0B
2019-06-04 01:55
Java调用存储过程.pdf
Java调用存储过程.pdf
PDF
0B
2019-05-23 00:17
jAVA调用存储过程
jAVA中调用存储过程
TXT
0B
2018-12-29 11:18
java调用存储过程
java中调用存储过程 可以发回参数,继续使用
TXT
0B
2018-12-29 11:28
JAVA调用存储过程txt
JAVA调用存储过程.txt JAVA调用存储过程.txt
TXT
0B
2019-01-09 06:04
java调用存储过程示例
使用java调用存储过程,以及对返回值的处理
ZIP
0B
2019-01-20 11:47