importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;public classDB {private String className; //驱动名
private String url; //连接数据库的URL地址
private String username; //数据库的用户名
private String password; //数据库的密码
private Connection con; //数据库连接对象
private PreparedStatement pstm; //数据库预编译处理对象
publicDB(){
className="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/test";
username="root";
password="201321173083";try{
Class.forName(className);
}catch(ClassNotFoundException e){
System.out.println("加载数据库驱动程序失败!");
e.printStackTrace();
}
}public voidgetCon(){try{
con=DriverManager.getConnection(url,username,password);
}catch(SQLException e) {
System.out.println("获取数据库连接失败!");
e.printStackTrace();
}
}//对象数组。如:String[] obj = new String[]{"宾桀锋","201321173083"};
public voiddoPstm(String sql,Object[] params){if(sql!=null && !sql.equals("")){
System.out.println(sql);if(con==null)
getCon();try{
pstm=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);if(params==null){
params=new Object[0];
}for(int i=0;i
pstm.setObject(i+1,params[i]);
}
pstm.execute();
}catch(SQLException e) {
System.out.println("调用DB类中doPstm方法时出错!");
e.printStackTrace();
}
}
}publicResultSet getRs(){try{returnpstm.getResultSet();
}catch(SQLException e) {
System.out.println("DB类中的getRs()方法出错!");
e.printStackTrace();return null;
}
}public intgetUpdate(){try{returnpstm.getUpdateCount();
}catch(SQLException e) {
e.printStackTrace();return -1;
}
}public voidclosed(){try{if(pstm!=null)
pstm.close();
}catch(Exception e){
System.out.println("关闭pstm对象失败!");
}try{if(con!=null)
con.close();
}catch(Exception e){
System.out.println("关闭con对象失败!");
}
}
}