Java_jdbc

连接数据库代码   收藏代码
  1. //-------------------  
  2. Class.forName("com.mysql.jdbc.Driver");  
  3.         String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=****";  
  4.         Connection conn = DriverManager.getConnection(url);  
  5. //------------------------  
  6. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  7. Connection conn = DriverManager.getConnection("jdbc:odbc:dataBase")  
  8. //--------------------  
  9. Class.forName("oracle.jdbc.driver.OracleDriver");  
  10.         String url = "url=jdbc:oracle:thin:@localhost:1521:XE";  
  11.         Connection conn = DriverManager.getConnection(url,username,pwd);  
  12. //-----------------------  
 
/batch;;;数据库,专门大量数插;;代码   收藏代码
  1. //Batch;;;数据库,专门大量数插;;  
  2. public void method3(){  
  3.     Connection conn = null;  
  4.     PreparedStatement stmt = null;  
  5.     DBHelper db = null;  
  6.     long start = System.currentTimeMillis();  
  7.     try {  
  8.         db = new DBHelper();  
  9.         conn = db.getConn();  
  10.         //jdbc默认自动提交;;;insert commit  
  11.         // 下面这句可别创建for  
  12.         conn.setAutoCommit(false);  
  13.         String sql = "insert into t_sal values(?,'kaka',?)";  
  14.   
  15.         stmt = conn.prepareStatement(sql);  
  16.         for (int i = 1; i < 1000; i++) {  
  17.             stmt.setInt(1, i);  
  18.             stmt.setInt(2, i*100);  
  19.             stmt.addBatch();  
  20.             if(i % 100 == 0){  
  21.                 stmt.executeBatch();  
  22.                 conn.commit();  
  23.                 stmt.clearBatch();  
  24.                   
  25.             }  
  26.             //stmt.executeUpdate();  
  27.         }  
  28.         //conn.commit();  
  29.         //提交方式,改原来自动提交,,一句一提交..  
  30.         conn.setAutoCommit(true);  
  31.     } catch (SQLException e) {  
  32.         try {  
  33.             conn.rollback();  
  34.         } catch (SQLException e1) {  
  35.             e1.printStackTrace();  
  36.         }  
  37.         e.printStackTrace();  
  38.     } finally {  
  39.         db.close(null, stmt, conn);  
  40.     }  
  41.     long end = System.currentTimeMillis();  
  42.     //ps bach一共花了578毫秒  
  43.     System.out.println("ps bach一共花了"+(end - start)+"毫秒");  
  44.           
  45. }  
 
取出刚insert记录的自增id代码   收藏代码
  1. //-----取出刚insert记录的自增id-----第一种方法...  
  2.    public int getInsert_id1(){  
  3.     DBHelper db = new DBHelper();  
  4.     Connection conn = db.getConn();  
  5.     PreparedStatement pstmt_insert = null;  
  6.     PreparedStatement pstmt_select = null;  
  7.     ResultSet rs = null;  
  8.     int row = 0;  
  9.       
  10.     String insertSQL = "insert into pro_stu values(pro_stu_id.NEXTVAL,'tom','13912345678','重庆西永')";  
  11.     //第二种方法...selectSQL = "select pro_stu_id.CURRVAL from dual";    
  12.     String selectSQL = "select max(stu_id) from pro_stu";  
  13.     try{  
  14.        //执行插入语句...  
  15.     pstmt_insert = conn.prepareStatement(insertSQL);  
  16.     pstmt_insert.executeUpdate();  
  17.     //执行查询语句...  
  18.     pstmt_select = conn.prepareStatement(selectSQL);  
  19.     rs = pstmt_select.executeQuery();  
  20.     while(rs.next()){  
  21.         row = rs.getInt(1);  
  22.     }  
  23.     }catch(SQLException e){  
  24.         e.printStackTrace();  
  25.     }  
  26.     db.close(rs, pstmt_select, null);  
  27.     db.close(null, pstmt_insert, conn);  
  28.    
  29.     return row;  
  30.    }  
  31.    //----------取出刚insert记录通用方法.............  
  32.    public int getInsert_id2(){  
  33.     DBHelper db = new DBHelper();  
  34.       
  35.     Connection conn = db.getConn();  
  36.       
  37.     PreparedStatement pstmt_insert = null;  
  38.     //不曾见,,数据库封装对象;;知道..数据库版;  
  39.     DatabaseMetaData dbmd = null;  
  40.     ResultSet rs = null;  
  41.     int row = 0;  
  42.       
  43.     /*首先你要知道,你的jdbc是不是 3.0是否支持功能  
  44.     System.out.println("product name"+dmd.getDatabaseProductName());  
  45.     System.out.println("product version"+dmd.getDatabaseProductVersion());  
  46.     System.out.println("support generate keys?"+dmd.supportsGetGeneratedKeys());  
  47.     */  
  48.       
  49.     String sql = "insert into pro_stu values" +  
  50.             "(pro_stu_id.NEXTVAL,'jack','13812345678','广东深圳')";  
  51.     try{  
  52.         dbmd = conn.getMetaData();  
  53.           
  54.         pstmt_insert = conn.prepareStatement(sql,new String[]{"stu_id"});  
  55.         pstmt_insert.executeUpdate();  
  56.           
  57.         if(dbmd.supportsGetGeneratedKeys()){     
  58.             rs = pstmt_insert.getGeneratedKeys();  
  59.             if(rs.next()){  
  60.                 row = rs.getInt(1);  
  61.             }  
  62.         }  
  63.     }catch(SQLException e){  
  64.         e.printStackTrace();  
  65.     }  
  66.     db.close(rs, pstmt_insert, conn);  
  67.     return row;  
  68.    }  
 
事务----代码   收藏代码
  1. //事务-----------------  
  2.    public void inTimeRun(){  
  3.     DBHelper db = new DBHelper();  
  4.     Connection conn = db.getConn();  
  5.     PreparedStatement pstmt1 = null;  
  6.     PreparedStatement pstmt2 = null;  
  7.     try{  
  8.     //取消自动提交更新设置为手动...  
  9.     conn.setAutoCommit(false);  
  10.       
  11.     String sql1 = "update pro_stu set stu_name = 'jarry' where stu_id = 6";  
  12.     String sql2 = "delete from pro_stu where stu_id = 10";  
  13.       
  14.     pstmt1 = conn.prepareStatement(sql1);  
  15.     pstmt2 = conn.prepareStatement(sql2);  
  16.       
  17.     pstmt1.executeUpdate();  
  18.     pstmt2.executeUpdate();  
  19.       
  20.     //System.out.println("正在更新...");  
  21.       
  22.     conn.commit();  
  23.       
  24.       
  25.     }catch(SQLException e){  
  26.         e.printStackTrace();  
  27.         try{  
  28.             System.out.println("更新失败...已回滚....");  
  29.             //设置回自动提交更新...  
  30.             conn.rollback();  
  31.             conn.setAutoCommit(true);  
  32.         }catch(SQLException e1){  
  33.             e1.printStackTrace();  
  34.         }  
  35.     }  
  36.     db.close(null, pstmt2, null);  
  37.     db.close(null, pstmt1, conn);  
  38.    }  
 
数据库图片保存...代码   收藏代码
  1. public class SaveFile {  
  2.     public void saveF(){  
  3.         DBHelper db = new DBHelper();  
  4.         Connection conn = db.getConn();  
  5.         PreparedStatement pstmt = null;  
  6.         try{  
  7.             pstmt = conn.prepareStatement("insert into saveImg values(?,?)");  
  8.               
  9.             pstmt.setString(1,"e:/西湖醋鱼.jpg");  
  10.             InputStream in = new FileInputStream("e:/西湖醋鱼.jpg");  
  11.             pstmt.setBinaryStream(2,in,in.available());  
  12.             pstmt.executeQuery();  
  13.             System.out.println("图片保存成功...");  
  14.         }catch(SQLException e){  
  15.             e.printStackTrace();  
  16.         }catch(FileNotFoundException e1){  
  17.             e1.printStackTrace();  
  18.         }catch(IOException e2){  
  19.             e2.printStackTrace();  
  20.         }  
  21.     }  
  22.     public void getF(){  
  23.         DBHelper db = new DBHelper();  
  24.         Connection conn = db.getConn();  
  25.         PreparedStatement pstmt = null;  
  26.         ResultSet rs = null;  
  27.         try{  
  28.             pstmt = conn.prepareStatement("select * from saveImg where filename = ?");  
  29.             pstmt.setString(1,"e:/西湖醋鱼.jpg");  
  30.             rs = pstmt.executeQuery();  
  31.             rs.next();  
  32.             InputStream in = rs.getBinaryStream("filecontent");  
  33.             FileOutputStream fos = new FileOutputStream("e:/index.jpg");  
  34.             byte[] buff = new byte[1024];  
  35.             int length = 0;  
  36.             while((length = in.read(buff)) != -1){  
  37.                 fos.write(buff,0,length);  
  38.                 fos.flush();  
  39.             }  
  40.             fos.close();  
  41.             in.close();  
  42.             System.out.println("数据写入成功...");  
  43.         }catch(SQLException e){  
  44.             e.printStackTrace();  
  45.         }catch(FileNotFoundException e1){  
  46.             e1.printStackTrace();  
  47.         }catch(IOException e2){  
  48.             e2.printStackTrace();  
  49.         }  
  50.     }  
  51.     public static void main(String[] args){  
  52.         SaveFile save = new SaveFile();  
  53.         save.getF();  
  54.     }  
  55. }  
 
Oracledatasource代码   收藏代码
  1. 1:在conf/context.xml中加入<resource>元素  
  2. <Resource name="OracleDataSource"     
  3.              auth="Container"     
  4.              type="javax.sql.DataSource"       
  5.              maxActive= "10"       
  6.              maxIdle= "2"       
  7.              maxWait="5000"     
  8.              username="scott"       
  9.              password="tiger"       
  10.              driverClassName="oracle.jdbc.driver.OracleDriver"       
  11.              url="jdbc:oracle:thin:@localhost:1521:myOracle"/>   
  12.   
  13. 2:  
  14. 在web.xml中加入<resource-ref>元素  
  15. <resource-ref>元素表示在Web应用中引用JNDI资源  
  16. <resource-ref>   
  17. <description>DB Connection</description>   
  18. <res-ref-name>OracleDataSource</res-ref-name>  
  19.  <res-type>javax.sql.DataSource</res-type>  
  20.  <res-auth>Container</res-auth>   
  21. </resource-ref>   
  22.   
  23. 3:  
  24. 由于数据源由Servlet容器创建并维护,所以必须把JDBC驱动程序拷贝到Tomcat安装目录下的lib目录下,确保Servlet容器能够访问驱动程序  
  25.   
  26. 4:  
  27. 获得对数据源的引用:  
  28.     Context ctx = new InitialContext();  
  29.     DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/OracleDataSource");  
  30. 获得数据库连接对象:  
  31.     Connection con = ds.getConnection();  
  32. 返回数据库连接到连接池:  
  33.       con.close();  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值