J2EE开发初步,MyEclipse+SQLServer2000插入、删除、更新、查询步骤+代码(改进版)

来源:互联网 发布:十八剁开山刀淘宝网 编辑:程序博客网 时间:2024/05/16 12:14

上一篇中代码冗余太多,不符合Java代码重用的特点,特此改进。本次改进定义两个类:public class DBConnection和public class CRUTestByDBConnection,前者主要用于加载驱动、创建连接对象和关闭资源对象;后者用于定义sql语句、遍历结果集(如果用得到的话),来实现对数据库的各种操作。

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBConnection {
 private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
 private static String url="jdbc:sqlserver://localhost:1433;DataBaseName=jdbc_test";
 private static String user="sa";
 private static String password="sa";
 
    private DBConnection(){
     
    }
    static{
     try{
      Class.forName(driverName);
     }catch(ClassNotFoundException e){
      e.printStackTrace();
      throw new ExceptionInInitializerError(e.getMessage());
     }
    }
   
    public static Connection getConnection()throws SQLException{
     return DriverManager.getConnection(url,user,password);
    }
   
    public static void close(ResultSet rs,Statement st,Connection conn){
     try{
      if(rs!=null){
       rs.close();
      }
     }catch(SQLException e){
      e.printStackTrace();
     }finally{
      try{
       if(st!=null){
        st.close();
       }
      }catch(SQLException e){
       e.printStackTrace();
      }finally{
       if(conn!=null){
        try{
         conn.close();
        }catch(SQLException e){
         e.printStackTrace();
        }
       }
      }
     }
    }
}

 


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;


public class CRUTestByDBConnection {
 private static Connection conn=null;
 private static Statement st=null;
 private static ResultSet rs=null;
 
 public static void main(String[]args)throws SQLException{
  CRUTestByDBConnection.add();
  CRUTestByDBConnection.get();
  CRUTestByDBConnection.update();
  CRUTestByDBConnection.delete();
 }
 
 public static void add()throws SQLException{
  try{
   conn=DBConnection.getConnection();
   String sql="insert into [user](name,password,email,age,birthday,money)values('zhangsan','123','zhangsan@126.com',22,'1988-12-31',500.0)";
   st=conn.createStatement();
   st.executeUpdate(sql);
  }finally{
   DBConnection.close(rs, st, conn);
  }
 }
 
 public static void get()throws SQLException{
  try{
   conn=DBConnection.getConnection();
   String sql="select id,name,password,email,age,birthday,money from [user]";
   st=conn.createStatement();
   rs=st.executeQuery(sql);
   while(rs.next()){
    int id=rs.getInt("id");
    String name=rs.getString("name");
    String password=rs.getString("password");
    String email=rs.getString("email");
    int age=rs.getInt("age");
    Date birthday=rs.getDate("birthday");
    float money=rs.getFloat("money");
    System.out.println("id="+id+";name="+name+";password="+password+";email="+email+";age="+age+";birthday="+birthday+";money="+money);
    
   }
   }finally{
    DBConnection.close(rs, st, conn);
   }
 }
 
 public static void update()throws SQLException{
  try{
   conn=DBConnection.getConnection();
   String sql="update [user] set name='lisi',password='abcd',email='lisi@163.com',age=22,birthday='1988-12-21',money=1000.0 where id=4";
   st=conn.createStatement();
   st.executeUpdate(sql);
  }finally{
      DBConnection.close(rs,st,conn);
  }
 }
 
 public static void delete()throws SQLException{
  try{
   conn=DBConnection.getConnection();
   String sql="delete from [user] where id=9";
   st=conn.createStatement();
   st.executeUpdate(sql);
  }finally{
   DBConnection.close(rs, st, conn);
  }
 }

}