jdbc_释放资源与CRUD的操作封装

dbinfo.properties


通过建立一个properties存储数据库的配置信息
driverClass=com.mysql.jdbc.Drive
url=jdbc:mysql://localhost:3306/users
username=root
password=123


User.java


建立一个实体类存储users表的数据类型
package cw_entity;
import java.util.Date;
public class User {
 private int id;
 private String name;
 private String password;
 private String email;
 private Date birthday;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 @Override
 public String toString() {
  return "User [id=" + id + ", name=" + name + ", password=" + password
    + ", email=" + email + ", birthday=" + birthday + "]";
 } 
}


DBUtils.java


通过建一个DBUtils.java,实现加载properties文件数据,并且封装了得到连接和关闭资源的方法

//CRUD
package cw_DbUtil;
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 DBUtils {
 private static String driverClass;
 private static String url;
 private static String username;
 private static String password;
 
 static{
  //次对象是用于加载properties文件数据的
  ResourceBundle rb=ResourceBundle.getBundle("dbinfo");
  driverClass=rb.getString("driverClass");
  url=rb.getString("url");
  username=rb.getString("username");
  password=rb.getString("password");
  try{
   Class.forName(driverClass);
  }catch(ClassNotFoundException e){
   e.printStackTrace();
  }
 }
 //得到连接的方法
 public static Connection getConnection() throws Exception{
  return DriverManager.getConnection("url,username,password");
 }
 //关闭资源的方法
 public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
  //关闭资源
  if(rs!=null){
   try {
    rs.close();
   }catch(Exception e){
    e.printStackTrace();
   }
   rs=null;
  }
  if(stmt!=null){
   try{
    stmt.close();
   }catch(Exception e){
    e.printStackTrace();
   }
   stmt=null;
  }
  if(conn!=null){
   try{
    conn.close();
   }catch(Exception e){
    e.printStackTrace();
   }
   conn=null;
  }
 }
}



封装过后的   TestCRUD.java

对users表进行增删改查操作

package cw_CRUD;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import cw_entity.User;
import org.junit.Test;
import cw_DbUtil.DBUtils;
public class TestCRUD {
 @Test
 public void testSelect(){
  Connection conn=null;
  Statement stmt=null;
  ResultSet rs=null;
  
  try {
   conn=DBUtils.getConnection();
   stmt=conn.createStatement();
   rs=stmt.executeQuery("select *from users");
   List list=new ArrayList();
   while(rs.next()){
    User u=new User();
    u.setId(rs.getInt(1));
    u.setName(rs.getString(2));
    u.setPassword(rs.getString(3));
    u.setEmail(rs.getString(4));
    u.setBirthday(rs.getDate(5));
    list.add(u);
   }
   for(User user:list){
    System.out.println(user);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   DBUtils.closeAll(rs, stmt, conn);
  }
  
 }
 @Test
 public void testInsert(){
  Connection conn=null;
  Statement stmt=null; 
  try {
   conn=DBUtils.getConnection();
   stmt=conn.createStatement();
   int i=stmt.executeUpdate("insert into users values(4,'abc','123456')");
   if(i>0){
    System.out.println("Success");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   DBUtils.closeAll(null, stmt, conn);
  }
  
 }
 @Test
 public void testUpdate(){
  Connection conn=null;
  Statement stmt=null; 
  try {
   conn=DBUtils.getConnection();
   stmt=conn.createStatement();
   int i=stmt.executeUpdate("update users set name='abcd',password='234567' where id=4");
   if(i>0){
    System.out.println("Success");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   DBUtils.closeAll(null, stmt, conn);
  }
 }
 public void testDelete(){
  Connection conn=null;
  Statement stmt=null; 
  try {
   conn=DBUtils.getConnection();
   stmt=conn.createStatement();
   int i=stmt.executeUpdate("delete from users where id=4");
   if(i>0){
    System.out.println("Success");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   DBUtils.closeAll(null, stmt, conn);
  }
 }
}



你可能感兴趣的:(Javaweb_)