dbinfo.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day06
username=root
password=123456

DBUtil.java

 package songyan.jdbc.utiltest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
 public class DBUtil{
     private static String driverClass;
     private static String username;
     private static String url;
     private static String password;
     
     static {
         ResourceBundle rb= ResourceBundle.getBundle("dbinfo");
         driverClass=rb.getString("driverClass");
         username=rb.getString("username");
         url=rb.getString("url");
         password=rb.getString("password");
         
         try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
     }
     
     public static  Connection getConnection() throws SQLException
     {
         return DriverManager.getConnection(url,username,password);
     }
     
     public static  void closeAll(Connection conn,Statement sta,ResultSet rs)
     {
         if(rs!=null)
         {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs=null;
         }
         
         if(sta!=null)
         {
            try {
                sta.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            sta=null;
         }
         
         if(conn!=null)
         {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn=null;
         }
     }
    
 
 }

CRUDTest.java

 package songyan.jdbc.utiltest;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import songyan.jdbc.utiltest.DBUtil;
import songyan.jdbc.entity.*;

 
 public class CRUDTest{
     
     public static void selectTest() throws Exception
     {
         Connection conn=null;
         Statement sta=null;
         ResultSet rs=null;
         
         conn=DBUtil.getConnection();
         
         sta=conn.createStatement();
         
         rs=sta.executeQuery("select * from users");
         
         List<User> l= new ArrayList<User>();
         while(rs.next())
         {
            User u= new User();
            u.setId(rs.getInt("id"));
            u.setName(rs.getString("name"));
            u.setPassword(rs.getString("password"));
            u.setEmail(rs.getString("email"));
            u.setBirthday(rs.getDate("birthday"));
            l.add(u);
         }
         
         DBUtil.closeAll(conn, sta, rs);
         
     }
     
     public static void insertTest() throws SQLException
     {
         Connection conn=null;
         Statement sta=null;
         ResultSet rs=null;
         
         conn=DBUtil.getConnection();
         
         sta=conn.createStatement();
         
         System.out.println(sta.executeUpdate("insert into users values(7,'aaa','aaa','a@163.com','1981-12-04')"));        
         
         DBUtil.closeAll(conn, sta, rs);
     }
     
     public static void updateTest() throws SQLException
     {
         Connection conn=null;
         Statement sta=null;
         ResultSet rs=null;
         
         conn=DBUtil.getConnection();
         
         sta=conn.createStatement();
         
         System.out.println("影响了"+sta.executeUpdate("update users set name='bbb' where id='6'")+"行。");        
         
         DBUtil.closeAll(conn, sta, rs);
     }
     
     public static void deleteTest() throws SQLException
     {
         Connection conn=null;
         Statement sta=null;
         ResultSet rs=null;
         
         conn=DBUtil.getConnection();
         
         sta=conn.createStatement();
         
         System.out.println(sta.executeUpdate("delete from users where id='7'"));        
         
         DBUtil.closeAll(conn, sta, rs);
     }
     
     public static void main(String[] args) throws Exception
     {
         deleteTest();
     }
 }

 

posted on 2018-03-11 15:46  song.yan  阅读(215)  评论(0编辑  收藏  举报