• JDBC向oracle插入数据


      1   public static void main(String[] args) throws SQLException {
      2  2         
      3  3         
      4  4         String driver="oracle.jdbc.driver.OracleDriver";
      5  5         String url="jdbc:oracle:thin:@www.ee.w.www:1521:orcl";   
      6  6         String user="rrr";                               
      7  7         String password="rrr";
      8  8         
      9  9         Connection conn=null;
     10 10         PreparedStatement stmt=null;
     11 11                 //Statement _stmt=null;
     12 12         ResultSet rs=null;
     13 13         List<TestEntity> entityList=null;
     14 14         try {
     15 15             
     16 16             Class.forName(driver);
     17 17             conn=(Connection)DriverManager.getConnection(url, user, password);
     18 18             String sql="SELECT * FROM t_daoshu_test where  c_time>?";
     19 19                 //String _sql="select max(ID) from t_daoshu_test";
     20 20                 //_stmt=(Statement)conn.createStatement();
     21 21             stmt=(PreparedStatement)conn.prepareStatement(sql);
     22 22             stmt.setObject(1,new java.sql.Timestamp(new Date().getTime()));
     23 23             rs=stmt.executeQuery();  //_stmt.executeQuery(sql);
     24 24             entityList=new ArrayList<TestEntity>();
     25 25             int i=0;
     26 26             while(rs.next())
     27 27             {
     28 28                 i=rs.getInt(1);
     29 29             }    
     30 30             
     31 31             /*while(rs.next())
     32 32             {
     33 33                 TestEntity en=new TestEntity();
     34 34                  en.setId(rs.getInt("ID"));
     35 35                  en.setArea(rs.getString("C_AREA"));
     36 36                  en.setType(rs.getString("C_TYPE"));
     37 37                  en.setTitle(rs.getString("C_TITLE"));
     38 38                  en.setLink(rs.getString("C_LINK"));
     39 39                  en.setDate(rs.getDate("C_TIME"));
     40 40                  entityList.add(en);
     41 41                 
     42 42             }*/
     43 43             System.out.println("最大ID:"+i);
     44 44             System.out.println("集合size:"+entityList.size());
     45 45 
     46 46         } catch (ClassNotFoundException e) {
     47 47             e.printStackTrace();
     48 48         }finally
     49 49         {
     50 50             closeResource(rs,stmt,conn);
     51 51             
     52 52         }
     53 53         
     54 54         
     55 55         if(entityList.size()>0)
     56 56         {    
     57 57             //插入数据
     58 58             try {
     59 59                 Class.forName(driver);
     60 60                 conn=(Connection)DriverManager.getConnection(url, user, password);
     61 61                 String sql="INSERT INTO T_FORM_210 (WORK_FLOW_PROCESS_ID, CREATE_TIME, WORK_FLOW_ID, NODE_VALUE, UNITE_STATE, COL0, COL1, COL2, COL3, COL4, COL5)"; 
     62 62                 sql+="VALUES (-1, sysdate, -1, -1, -1,?,?,?,?,?,?)";
     63 63                 
     64 64                 conn.setAutoCommit(false);
     65 65                 PreparedStatement pst=(PreparedStatement)conn.prepareStatement(sql);
     66 66                 for(int i=0;i<entityList.size();i++)
     67 67                 {
     68 68                     TestEntity entity=new TestEntity();
     69 69                     entity=entityList.get(i);
     70 70                     pst.setString(1,entity.getType());
     71 71                     pst.setString(2,entity.getArea());
     72 72                     pst.setString(3,entity.getTitle());
     73 73                     pst.setString(4,entity.getLink());
     74 74                     pst.setObject(5,new java.sql.Timestamp(new Date().getTime()));  //更新时间 
     75 75                     pst.setObject(6,new java.sql.Timestamp(new Date().getTime()));   //最后更新时间
     76 76                     pst.addBatch();  
     77 77                 }    
     78 78                 //执行批量更新
     79 79                 pst.executeBatch();
     80 80                 conn.commit();
     81 81                 System.out.println(new java.sql.Timestamp(new Date().getTime())+" 已经同步数据");
     82 82             } catch (ClassNotFoundException e) {
     83 83                 e.printStackTrace();
     84 84             }finally
     85 85             {
     86 86                 closeResource(rs,stmt,conn);
     87 87                 
     88 88             }
     89 89         
     90 90         }
     91 91         
     92      public static void closeResource(ResultSet rs,Statement stmt,Connection conn)
     93       {
     94         if(rs!=null)
     95         {
     96             try {
     97                 rs.close();
     98             } catch (SQLException e) {
     99                 // TODO Auto-generated catch block
    100                 e.printStackTrace();
    101             }
    102         }    
    103         if(stmt!=null)
    104         {
    105             try {
    106                 stmt.close();
    107             } catch (SQLException e) {
    108                 // TODO Auto-generated catch block
    109                 e.printStackTrace();
    110             }
    111         }    
    112         if(conn!=null)
    113         {
    114             try {
    115                 conn.close();
    116             } catch (SQLException e) {
    117                 // TODO Auto-generated catch block
    118                 e.printStackTrace();
    119             }
    120         }    
    121         
    122     }
  • 相关阅读:
    第五周作业
    关于结对编程的理解
    第四周作业
    总结
    总结
    总结
    总结
    总结
    判断树、判断表
    总结
  • 原文地址:https://www.cnblogs.com/ChinazhouWang/p/5161641.html
Copyright © 2020-2023  润新知