JDBC操作数据库

来源:互联网 发布:中国农大网络教育首页 编辑:程序博客网 时间:2024/05/22 06:31
  1. package cn.com.JDBC;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.sql.Statement;  
  7.   
  8. public class CRUD {  
  9.   
  10.     public static void main(String[] args) throws SQLException {  
  11.         // TODO Auto-generated method stub  
  12.         //create();  
  13.         //update();  
  14.         delete();  
  15.         read();  
  16.     }  
  17.     static void delete() throws SQLException  
  18.     {  
  19.         Connection conn=null;  
  20.         Statement st=null;  
  21.         ResultSet resultset=null;  
  22.           
  23.         try {  
  24.             //2.建立连接  
  25.             conn=JdbcUtils.getConnection();  
  26.             //单例设计模式  
  27.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  28.             //3.创建语句  
  29.             st=conn.createStatement();  
  30.             //4.执行语句  
  31.             String sql="delete from user where id>5";  
  32.             int i=st.executeUpdate(sql);  
  33.               
  34.             System.out.println("i="+i);  
  35.         } finally  
  36.         {  
  37.             JdbcUtils.free(resultset, st, conn);  
  38.         }  
  39.     }  
  40.     static void update() throws SQLException  
  41.     {  
  42.         Connection conn=null;  
  43.         Statement st=null;  
  44.         ResultSet resultset=null;  
  45.           
  46.         try {  
  47.             //2.建立连接  
  48.             conn=JdbcUtils.getConnection();  
  49.             //单例设计模式  
  50.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  51.             //3.创建语句  
  52.             st=conn.createStatement();  
  53.             //4.执行语句  
  54.             String sql="update user set money=money+20";  
  55.             int i=st.executeUpdate(sql);  
  56.               
  57.             System.out.println("i="+i);  
  58.         } finally  
  59.         {  
  60.             JdbcUtils.free(resultset, st, conn);  
  61.         }  
  62.     }  
  63.     static void create() throws SQLException  
  64.     {  
  65.         Connection conn=null;  
  66.         Statement st=null;  
  67.         ResultSet resultset=null;  
  68.           
  69.         try {  
  70.             //2.建立连接  
  71.             conn=JdbcUtils.getConnection();  
  72.             //单例设计模式  
  73.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  74.             //3.创建语句  
  75.             st=conn.createStatement();  
  76.             //4.执行语句  
  77.             String sql="insert into user(name,birthday,money) values('wy','2011-09-23','2894656')";  
  78.             int i=st.executeUpdate(sql);  
  79.               
  80.             System.out.println("i="+i);  
  81.         } finally  
  82.         {  
  83.             JdbcUtils.free(resultset, st, conn);  
  84.         }  
  85.     }  
  86.     static void read() throws SQLException  
  87.     {  
  88.         Connection conn=null;  
  89.         Statement st=null;  
  90.         ResultSet resultset=null;  
  91.           
  92.         try {  
  93.             //2.建立连接  
  94.             conn=JdbcUtils.getConnection();  
  95.             //单例设计模式  
  96.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  97.             //3.创建语句  
  98.             st=conn.createStatement();  
  99.             //4.执行语句  
  100.             resultset=st.executeQuery("select id,name,birthday,money from user");  
  101.             //5.处理结果  
  102.             while(resultset.next())  
  103.             {  
  104.                 System.out.println(resultset.getObject("id"));  
  105.                 System.out.println(resultset.getObject("name"));  
  106.                 System.out.println(resultset.getObject("birthday"));  
  107.                 System.out.println(resultset.getObject("money"));  
  108.             }  
  109.               
  110.         } finally  
  111.         {  
  112.             JdbcUtils.free(resultset, st, conn);  
  113.         }  
  114.     }  
  115. }  
  116. package cn.com.JDBC;  
  117.   
  118. import java.sql.Connection;  
  119. import java.sql.DriverManager;  
  120. import java.sql.ResultSet;  
  121. import java.sql.SQLException;  
  122. import java.sql.Statement;  
  123.   
  124. public class JdbcUtils   
  125. {  
  126.     private static String url="jdbc:mysql://localhost:3306/jdbc";  
  127.     private static String user="root";  
  128.     private static String password="123";  
  129.     private JdbcUtils()  
  130.     {  
  131.     }  
  132.     static  
  133.     {  
  134.         try  
  135.         {  
  136.             Class.forName("com.mysql.jdbc.Driver");  
  137.         }  
  138.         catch(ClassNotFoundException e)  
  139.         {  
  140.             throw new ExceptionInInitializerError(e);  
  141.         }  
  142.     }  
  143.     public static Connection getConnection() throws SQLException  
  144.     {  
  145.         return DriverManager.getConnection(url, userpassword);  
  146.     }  
  147.     public static void free(ResultSet resultset,Statement st,Connection conn)  
  148.     {  
  149.         //6.释放资源  
  150.         try{  
  151.         if(resultset!=null)  
  152.             resultset.close();  
  153.         } catch (SQLException e) {  
  154.             // TODO Auto-generated catch block  
  155.             e.printStackTrace();  
  156.         }  
  157.         finally  
  158.         {  
  159.             try  
  160.             {  
  161.                 if(st!=null)  
  162.                     st.close();  
  163.             } catch (SQLException e) {  
  164.                 // TODO Auto-generated catch block  
  165.                 e.printStackTrace();  
  166.             }  
  167.             finally  
  168.             {  
  169.                 if(conn!=null)  
  170.                     try {  
  171.                         conn.close();  
  172.                     } catch (SQLException e) {  
  173.                         // TODO Auto-generated catch block  
  174.                         e.printStackTrace();  
  175.                     }  
  176.             }  
  177.         }  
  178.     }  
  179. }  
0 0
原创粉丝点击