java 向DB2插入数据

来源:互联网 发布:web of science数据库 编辑:程序博客网 时间:2024/05/21 06:59

PreparedStatement语句插入数据

标签: jdbcselect对象数据
3864人阅读 评论(0)收藏举报
分类:

 代码:

 1.直接调用插入:

DButil.java

[java] view plain copy
  1. import java.sql.DriverManager;  
  2. import java.sql.SQLException;  
  3. import java.util.Properties;  
  4.   
  5. import com.mysql.jdbc.Connection;  
  6.   
  7. public class DButil {  
  8.         /* 
  9.          * 打开数据库 
  10.          */  
  11.         private static String driver;//连接数据库的驱动  
  12.         private static String url;  
  13.         private static String username;  
  14.         private static String password;  
  15.           
  16.         static {  
  17.             driver="com.mysql.jdbc.Driver";//需要的数据库驱动  
  18.             url="jdbc:mysql://localhost:3306/test";//数据库名路径  
  19.             username="root";  
  20.             password="root";  
  21.         }  
  22.         public static Connection open()  
  23.         {  
  24.             try {  
  25.                 Class.forName(driver);  
  26.                 return (Connection) DriverManager.getConnection(url,username, password);  
  27.             } catch (Exception e) {  
  28.                 System.out.println("数据库连接失败!");  
  29.                 // TODO Auto-generated catch block  
  30.                 e.printStackTrace();  
  31.             }//加载驱动  
  32.   
  33.             return null;  
  34.         }  
  35.           
  36.         /* 
  37.          * 关闭数据库 
  38.          */  
  39.         public static void close(Connection conn)  
  40.         {  
  41.             if(conn!=null)  
  42.             {  
  43.                 try {  
  44.                     conn.close();  
  45.                 } catch (SQLException e) {  
  46.                     // TODO Auto-generated catch block  
  47.                     e.printStackTrace();  
  48.                 }  
  49.             }  
  50.         }  
  51.   
  52. }  

jdbcconnection.java

[java] view plain copy
  1. import java.sql.SQLException;  
  2.   
  3. import com.mysql.jdbc.Connection;  
  4. import com.mysql.jdbc.PreparedStatement;  
  5.   
  6. public class jdbcconnection {  
  7.   
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.        insert("yangxu","yangxu@qq.com");  
  11.     }  
  12.   
  13.     static void insert(String name,String email)  
  14.     {  
  15.         String sql="insert into Haige(name,email) value(?,?)";  
  16.         Connection conn=DButil.open();  
  17.         try {  
  18.             PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);  
  19.             pstmt.setString(1,name);  
  20.             pstmt.setString(2,email);  
  21.             pstmt.executeUpdate();  
  22.         } catch (SQLException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         }  
  26.         finally {  
  27.             DButil.close(conn);  
  28.         }  
  29.           
  30.     }  
  31. }  

2.面向对象的方式

Customer.java

[java] view plain copy
  1. public class Customer {  
  2.  int id;  
  3.  String name;  
  4.  String email;  
  5. public int getId() {  
  6.     return id;  
  7. }  
  8. public void setId(int id) {  
  9.     this.id = id;  
  10. }  
  11. public String getName() {  
  12.     return name;  
  13. }  
  14. public void setName(String name) {  
  15.     this.name = name;  
  16. }  
  17. public String getEmail() {  
  18.     return email;  
  19. }  
  20. public void setEmail(String email) {  
  21.     this.email = email;  
  22. }  
  23.   
  24. }  

jdbcconnection.java(插入数据)

[java] view plain copy
  1. import java.sql.SQLException;  
  2.   
  3. import com.mysql.jdbc.Connection;  
  4. import com.mysql.jdbc.PreparedStatement;  
  5.   
  6. public class jdbcconnection {  
  7.   
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.        //insert("yangxu","yangxu@qq.com");  
  11.         Customer c=new Customer();  
  12.         c.setName("zhangbing");  
  13.         c.setEmail("zhangbing@qq.com");  
  14.         insert(c);  
  15.     }  
  16.   
  17.     static void insert(Customer c)  
  18.     {  
  19.         String sql="insert into Haige(name,email) value(?,?)";  
  20.         Connection conn=DButil.open();  
  21.         try {  
  22.             PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);  
  23.             pstmt.setString(1,c.getName());  
  24.             pstmt.setString(2,c.getEmail());  
  25.             pstmt.executeUpdate();  
  26.         } catch (SQLException e) {  
  27.             // TODO Auto-generated catch block  
  28.             e.printStackTrace();  
  29.         }  
  30.         finally {  
  31.             DButil.close(conn);  
  32.         }  
  33.           
  34.     }  
  35. }  

jdbcconnection.java(修改数据)

[java] view plain copy
  1. import java.sql.SQLException;  
  2.   
  3. import com.mysql.jdbc.Connection;  
  4. import com.mysql.jdbc.PreparedStatement;  
  5.   
  6. public class jdbcconnection {  
  7.   
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.        //insert("yangxu","yangxu@qq.com");  
  11.         Customer c=new Customer();  
  12. //      c.setName("zhangbing");  
  13. //      c.setEmail("zhangbing@qq.com");  
  14.         //insert(c);  
  15.         c.setId(1001);  
  16.         c.setName("kaixin");  
  17.         Update(c);  
  18.     }  
  19.   
  20.     static void Update(Customer c)  
  21.     {  
  22.         String sql="update haige set name=? where id=?";  
  23.         Connection conn=DButil.open();  
  24.         try {  
  25.             PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);  
  26.             pstmt.setString(1,c.getName());  
  27.             pstmt.setInt(2,c.getId());;  
  28.             pstmt.executeUpdate();  
  29.         } catch (SQLException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         }  
  33.         finally {  
  34.             DButil.close(conn);  
  35.         }  
  36.           
  37.     }  
  38. }  

jdbcconnection.java(删除数据)

[java] view plain copy
  1. import java.sql.SQLException;  
  2.   
  3. import com.mysql.jdbc.Connection;  
  4. import com.mysql.jdbc.PreparedStatement;  
  5.   
  6. public class jdbcconnection {  
  7.   
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.        //insert("yangxu","yangxu@qq.com");  
  11. //      Customer c=new Customer();  
  12. ////        c.setName("zhangbing");  
  13. ////        c.setEmail("zhangbing@qq.com");  
  14. //      //insert(c);  
  15. //      c.setId(1001);  
  16. //      c.setName("kaixin");  
  17. //      Update(c);  
  18.         delete(1006);  
  19.     }  
  20.   
  21.     static void delete(int id)  
  22.     {  
  23.         String sql="delete from haige where id=?";  
  24.         Connection conn=DButil.open();  
  25.         try {  
  26.             PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);  
  27.             pstmt.setInt(1,id);;  
  28.             pstmt.executeUpdate();  
  29.         } catch (SQLException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         }  
  33.         finally {  
  34.             DButil.close(conn);  
  35.         }  
  36.           
  37.     }  
  38. }  

jdbcconnection.java(查询数据)

[java] view plain copy
  1. import java.sql.ResultSet;  
  2. import java.sql.SQLException;  
  3.   
  4. import com.mysql.jdbc.Connection;  
  5. import com.mysql.jdbc.PreparedStatement;  
  6.   
  7. public class jdbcconnection {  
  8.   
  9.     public static void main(String[] args) {  
  10.         // TODO Auto-generated method stub  
  11.        //insert("yangxu","yangxu@qq.com");  
  12. //      Customer c=new Customer();  
  13. ////        c.setName("zhangbing");  
  14. ////        c.setEmail("zhangbing@qq.com");  
  15. //      //insert(c);  
  16. //      c.setId(1001);  
  17. //      c.setName("kaixin");  
  18. //      Update(c);  
  19.         //delete(1006);  
  20.         Customer c=query(1005);  
  21.         System.out.println(c.getId()+","+c.getName()+","+c.getEmail());  
  22.     }  
  23.   
  24.     static Customer query(int id)  
  25.     {  
  26.         String sql="select * from haige where id=?";  
  27.         Connection conn=DButil.open();  
  28.         try {  
  29.             PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);  
  30.             pstmt.setInt(1,id);  
  31.             ResultSet rs=pstmt.executeQuery();  
  32.             if(rs.next())  
  33.             {  
  34.                 String name=rs.getString(2);  
  35.                 String email=rs.getString(3);  
  36.                 Customer c=new Customer();  
  37.                 c.setId(id);  
  38.                 c.setName(name);  
  39.                 c.setEmail(email);  
  40.                 return c;  
  41.             }  
  42.         } catch (SQLException e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.         finally {  
  47.             DButil.close(conn);  
  48.         }  
  49.         return null;  
  50.     }  

原创粉丝点击