package com.test; import java.sql. * ; /** */ /*** 此例说明的问题:创建并使用CallableStatement对象;* @author chb*/ class Hello ... { /** *//** * 该方法用以显示结果集; */ private static void showResultSet( ResultSet rs ) throws Exception...{ System.out.println(" 编号 图书名称 图书单价 图书数量"); while(rs.next()) ...{ int id=rs.getInt("ID"); String book_name=rs.getString("book_name"); double book_price=rs.getDouble("book_price"); int book_quantity=rs.getInt("book_quantity"); System.out.println(id+" "+book_name+" "+book_price+" "+book_quantity); } } public static void main(String args[]) ...{ try ...{ /** *//** * 第一步:加载JDBC驱动; */ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); /** *//** * 第二步:建立连接; */ String url="jdbc:odbc:bookdsn"; Connection con=DriverManager.getConnection(url,"sa",""); /** *//** * 第三步:创建Statement,执行查询并得到结果集; */ Statement stmt=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs=stmt.executeQuery("SELECT ID,book_name,book_price,book_quantity from book_stock"); showResultSet(rs); /** *//** * 第四步:创建CallableStatement用于调用存储过程; * cstmt.getInt(3) -- 只能得到输出参数值; */ CallableStatement cstmt=con.prepareCall("{call getQuantity(?,?,?)}"); cstmt.setInt(1,3); cstmt.setString(2,"JAVA基础"); cstmt.registerOutParameter(3,java.sql.Types.INTEGER); cstmt.execute(); System.out.println( "图书数量:" + cstmt.getInt(3) ); /** *//** * 第五步:关闭连接、结果集; */ rs.close(); stmt.close(); cstmt.close(); con.close(); } catch(Exception e) ...{ System.out.println("发生异常:"+e); } }}