JDBC详解

1、JDBC(Java Database Connection):java连接数据库统一接口API,底层主要通过直接的JDBC驱动和
   JDBC-ODBC桥驱动实现与数据库的连接。
   1>.JDBC驱动程序类型:
      <1>.JDBC-ODBC桥加ODBC驱动程序:需要ODBC驱动,适合于企业网或三层结构应用程序
      <2>.本地API:需要驱动程序的二进制代码支持
      <3>.JDBC网络纯java驱动程序:将JDBC转换为与DBMS无关的网络协议,又被某服务器转换为一种DBMS
          协议,以操作各种数据库
      <4>.本地协议纯java驱动程序:将JDBC调用直接转换成JDBC所使用的网络协议 
2、JDBC操作基本流程:
   1>.导入驱动:实例化时自动向DriverManager注册(DriverManager.registerDriver())
      <1>.Class.forName(driver) 
      <2>.Class.forName(driver).newInstance()
      <3>.new driver()
   2>.取得数据库连接(Connect to the DataBase)
      <1>.用DriverManager取数据库连接
          Connection cn = DriverManager.getConnection(url,uid,pwd);
      <2>.用jndi(java的命名和目录服务)方式:多用于jsp
          Context ctx = (Context) new InitialContext().lookup("java:comp/env");
          DataSource ds = (DataSource) ctx.lookup(jndi);
          Connection cn = ds.getConnection();
   3>.执行sql语句(Execute the SQL)
      <1>.用Statement来执行sql语句
          Statement sm = cn.createStatement();
          sm.executeQuery(sql); // 执行数据查询语句(select)
          sm.executeUpdate(sql); // 执行数据更新语句(delete、update、insert、drop等)
      <2>.用PreparedStatement来执行sql语句
          String  sql  = "insert into user (id,name) values (?,?)";
          PreparedStatement ps = cn.prepareStatement(sql);
          ps.setInt(1,xxx);
          ps.setString(2,xxx);
          ...
          ResultSet rs = ps.executeQuery(); // 查询
          int c = ps.executeUpdate(); // 更新
   4>.处理执行结果:
      <1>.查询语句,返回记录集ResultSet
      <2>.更新语句,返回数字,表示该更新影响的记录数
      <3>.ResultSet的方法:while(re.next())
          next(),将游标往后移动一行,如果成功返回true;否则返回false
          getInt("id")或getSting("name"),返回当前游标下某个字段的值
   5>.释放数据库连接
      rs.close();
      ps.close(); /stat.close();
      con.close();
3、创建可滚动、更新的记录集
   1>.创建Statement时指定参数:该Statement取得的ResultSet就是可滚动的
      Statement sm = cn.createStatement(ResultSet.TYPE_SCROLL_ENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
   2>.创建PreparedStatement时指定参数
      PreparedStatemet ps = cn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
          ResultSet.CONCUR_READ_ONLY);
      rs.absolute(9000); 
4、批量更新
   1>.Statement:可执行多个sql语句(delete、update、insert等或兼有),批量更新
      Statement sm = cn.createStatement();
      sm.addBatch(sql1);
      sm.addBatch(sql2);
      ...
      sm.executeBatch()
   2>.PreparedStatement:可把一个sql语句,变换参数多次执行,一次更新
      PreparedStatement ps = cn.preparedStatement(sql);
      {
          ps.setXXX(1,xxx);
          ...
          ps.addBatch();
      }
      ps.executeBatch();
5、事务的处理
   1>.关闭Connection的自动提交
      cn.setAutoCommit(false);
   2>.执行一系列sql语句:执行新sql前,以前的Statement(或PreparedStatemet)须close
      Statement sm ;
      sm = cn.createStatement(insert into user...);
      sm.executeUpdate();
      sm.close();
      sm = cn.createStatement("insert into corp...);
      sm.executeUpdate();
      sm.close();
   3>.提交
      cn.commit();
   4>.如果发生异常,那么回滚
      cn.rollback();
6、SQL语句简介:
   1>.Select ... from T Where...
   2>.Insert into T(...)values(...)
   3>.Create Table T(...)
   4>.Delete from T Where ...
   5>.Update T set f1=... and f2 =... where ...
   6>.Drop table T
7、JDBC示例:
class DBUtil{
     //local variable
    String driver = null;
    String url = null;
  
    Statement stat = null;
  
     //set local variable value
     public DBUtil(String driver,String url){
         this.driver = driver;
         this.url = url;
    }
  
     //get con
     public Connection getConnection() throws ClassNotFoundException, SQLException{
         Connection con = null;
         Class.forName(driver);
         con = DriverManager.getConnection(url);
         return con;
    }
  
     //execute select sentence
     public List executeQuery(String sql){
  
         ResultSet rs = null;
         Connection con = null;
         try {
              con = getConnection();
              Statement stat = con.createStatement();
              rs = stat.executeQuery(sql);
              while(rs.next()){
                   rs.getString(""); 
              } 
         } catch (SQLException e) {
              e.printStackTrace(); 
         } catch (ClassNotFoundException ce) {
              ce.printStackTrace();
         } finally{
             try{
                  if( null == con){
                       con.close();
                       con = null; 
                  }
                  if( null == stat){
                       stat.close();
                       stat = null;
                  }
                  if( null == rs){
                       rs.close();
                       rs = null;
                  } 
             } catch (SQLException e) {
                  e.printStackTrace();
             }
         } 
          return null;
    }
  
     //execute proceduce
  
}
 

本文出自 “不服不行” 博客,转载请与作者联系!

你可能感兴趣的:(java,职场,休闲)