使用JDBC调用存储过程

1732阅读 0评论2012-03-18 高傲的活着
分类:Java

点击(此处)折叠或打开

  1. package cn.itcast.jdbc;

  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Types;

  7. /**
  8.  *
  9.  * 2008-12-7
  10.  *
  11.  * @author <a href="mailto:liyongibm@gmail.com">liyong</a>
  12.  *
  13.  */
  14. public class PsTest {

  15.     /**
  16.      * @param args
  17.      * @throws SQLException
  18.      */
  19.     public static void main(String[] args) throws SQLException {
  20.         ps();
  21.     }

  22.     static void ps() throws SQLException {
  23.         Connection conn = null;
  24.         CallableStatement cs = null;
  25.         ResultSet rs = null;
  26.         try {
  27.             // 2.建立连接
  28.             conn = JdbcUtils.getConnection();
  29.             // conn = JdbcUtilsSing.getInstance().getConnection();
  30.             // 3.创建语句

  31.             String sql = "{ call addUser(?,?,?,?) } ";
  32.             cs = conn.prepareCall(sql);
  33.             cs.registerOutParameter(4, Types.INTEGER);
  34.             cs.setString(1, "ps name");
  35.             cs.setDate(2, new java.sql.Date(System.currentTimeMillis()));
  36.             cs.setFloat(3, 100f);

  37.             cs.executeUpdate();

  38.             int id = cs.getInt(4);

  39.             System.out.println("id=" + id);
  40.         } finally {
  41.             JdbcUtils.free(rs, cs, conn);
  42.         }
  43.     }

  44. }
上一篇:单链表删除一个结点
下一篇:MASM For Windows 集成开发环境