JDBC 增删改查操作示例

来源:互联网 发布:java软件是什么 编辑:程序博客网 时间:2024/06/05 16:34

java操作DB2,增删改查

 
Java代码  收藏代码
  1. //:Db2ConnTest  
  2.  /** 
  3.  *功能:实现连接DB2数据库,并实现基本获取数据功能 
  4.  *jiangbin 
  5.  */  
  6. import java.sql.*;  
  7. import java.util.*;     
  8.   
  9. @SuppressWarnings("unchecked")  
  10. public class Db2ConnTest{  
  11.     Connection conn;  
  12.     Statement stat;  
  13.     ResultSet rs;  
  14.     ResultSetMetaData rsmd;  
  15.     PreparedStatement pstat; //预编译  
  16.     List ll = new ArrayList();  
  17.       
  18.     /** 
  19.     *建立连接 
  20.     */  
  21.     public void connDb2(){    
  22.         try{  
  23.             //第一步:加载驱动  
  24.             Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");  
  25.             //第二步:得到连接  
  26.             conn = DriverManager.getConnection("jdbc:db2:mydb","db2admin","123");  
  27.             //第三步:建立statement,同一时间每个 Statement 对象在只能打开一个 ResultSet 对象。  
  28.             stat = conn.createStatement();        
  29.             }  
  30.             catch(ClassNotFoundException ee){  
  31.                 System.out.println("ClassNotFoundException:"+ee.getMessage());  
  32.                 }  
  33.             catch(SQLException e){  
  34.                 System.out.println(e);  
  35.                 }             
  36.         }  
  37.       
  38.     /** 
  39.     *断开连接 
  40.     */  
  41.     public void closeDb2(){  
  42.         try{  
  43.             if(rs!=null) rs.close();  
  44.             if(stat!=null) stat.close();  
  45.             if(conn!=null) conn.close();  
  46.       }  
  47.     catch(SQLException e){  
  48.         System.out.println(e);  
  49.         }  
  50.         }  
  51.       
  52.     /** 
  53.     *查询数据 
  54.     */  
  55.     public void searchData(){  
  56.         try{  
  57.          //得到结果集  
  58.             rs = stat.executeQuery("select * from db.tblStudent");  
  59.             //用于获取关于 ResultSet 对象中列的类型和属性信息的对象  
  60.             rsmd = rs.getMetaData();  
  61.             while(rs.next()){  
  62.                 Map rowData = new HashMap();  
  63.                 for(int i=1;i<=rsmd.getColumnCount();i++)  
  64.                 rowData.put(rsmd.getColumnName(i),rs.getString(i));  
  65.                 ll.add(rowData);  
  66.                 }     
  67.             }  
  68.             catch(SQLException e){  
  69.         System.out.println(e);  
  70.         }  
  71.         }  
  72.       
  73.     /** 
  74.     *打印数据 
  75.     */    
  76.     public void printData(){  
  77.          for(int i=0;i<ll.size();i++ ){  
  78.                 System.out.println(ll.get(i));  
  79.                 }  
  80.         }  
  81.           
  82.     /** 
  83.     *插入数据 
  84.     */  
  85.     public void insert(){  
  86.         try{  
  87.         //插入语句  
  88.         String str = "INSERT INTO db.tblStudent(strName,intAge,strAddress,strSex,grandId)"  
  89.                       +"VALUES(?,?,?,?,?)";  
  90.         pstat = conn.prepareStatement(str);  
  91.         pstat.setString(1,"王文远");  
  92.         pstat.setInt(2,20);  
  93.         pstat.setString(3,"北京");  
  94.         pstat.setString(4,"T");  
  95.         pstat.setInt(5,4);  
  96.         int record = pstat.executeUpdate();  
  97.         System.out.println("插入"+record+"数据");  
  98.       }  
  99.      catch(SQLException e){  
  100.         System.out.println(e);  
  101.         }  
  102.         }  
  103.           
  104.     /** 
  105.     *删除数据 
  106.     */  
  107.     public void delete(){  
  108.         try{  
  109.         //删除语句  
  110.         String str = "DELETE FROM db.tblStudent where strName='王五'";  
  111.         stat.executeUpdate(str);  
  112.       }  
  113.      catch(SQLException e){  
  114.         System.out.println(e);  
  115.         }  
  116.         System.out.println("删除数据成功");  
  117.         }  
  118.           
  119.     /** 
  120.     *修改数据 
  121.     */  
  122.     public void update(){  
  123.         try{  
  124.         //修改语句  
  125.         String str = "UPDATE db.tblstudent SET strAddress='北京',intAge=intAge+5 where intId=1";  
  126.         stat.executeUpdate(str);  
  127.       }  
  128.      catch(SQLException e){  
  129.         System.out.println(e);  
  130.         }  
  131.         System.out.println("修改数据成功");  
  132.         }  
  133.           
  134.     public static void main(String[] args){  
  135.         Db2ConnTest dc = new Db2ConnTest();  
  136.         dc.connDb2();  
  137.       dc.insert();  
  138.         //dc.searchData();  
  139.         //dc.delete();  
  140.         //dc.update();  
  141.         dc.searchData();  
  142.         dc.printData();  
  143.         dc.closeDb2();  
  144.         }  
  145.     }  
原创粉丝点击