packagecom.dao;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassGSYDao{
//定义对象
privatePreparedStatementpstmt;
privateConnectionconn;
privateResultSetrs;
//加载驱动
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
//方法卸载构造方法内,开始就会调用。
publicGSYDao(Stringsql){
try{
//获取数据库连接
conn=DriverManager.getConnection("jdbc:mysql://localhost/test",
"root","admin");
//存入pstmt对象,以供使用
pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidexecuteUpdate(Stringsql)throwsSQLException{
System.out.println(sql);
pstmt.executeUpdate(sql);
}
publicResultSetexecuteQuery(Stringsql)throwsSQLException{
System.out.println(sql);
rs=pstmt.executeQuery(sql);
returnrs;
}
publicvoidclose(){
if(rs!=null){
try{
rs.close();
}catch(Exceptione){
//TODO:handleexception
}
}
if(pstmt!=null){
try{
pstmt.close();
}catch(Exceptione){
//TODO:handleexception
}
}
if(conn!=null){
try{
conn.close();
}catch(Exceptione){
//TODO:handleexception
}
}
}
//main方法
publicstaticvoidmain(String[]args){
}
}