粗心的代价
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
import java.sql.*;
public class MysqlJdbc {
private String url = "jdbc:mysql://localhost:3306/lunchclub";
private Connection conn = null;
private ResultSet rs = null;
private Statement stmt = null;
private PreparedStatement pstmt = null;
public MysqlJdbc{
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(url,"root","abc123");
}catch(Exception eb){
eb.printStackTrace();
}
}
public ResultSet getRs(String sql){
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(Exception eb){
eb.printStackTrace();
}
return rs;
}
public PreparedStatement getPstmt(String sql){
try{
pstmt = conn.prepareStatement(sql);
}catch(Exception ec){
ec.printStackTrace();
}
return pstmt;
}
public void closeRs(){
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception ed){
ed.printStackTrace();
}
}
public void closePstmt(){
try{
pstmt.close();
conn.close();
}catch(Exception ef){
ef.printStackTrace();
}
}
}
这个是主要是跑例子,其中那个驱动因为错了一个字母,害我吓忙了办天。 :cry:
不用连接池的话,使用PreparedStatement好像无法缓存的.