配置JDBC工具类
public class JdbcUtils {
static{
private static String driver;
private static String url;
private static String user;
private static String password;
try {
InputStream is=BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
Properties params=new Properties();
params.load(is);
driver=params.getProperty("driver");
url=params.getProperty("url");
user=params.getProperty("user");
password=params.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
Class.forName(driver);
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,username,password);
}
public static void release(Connection con,Statement st,ResultSet rs){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(st!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(con!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
调用工具类实现SQL语句功能
public class Delete {
public static void main(String[] args){
Connection con = null;
ResultSet rs = null;
Statement st = null;
try{
con = JdbcUtils.getConnection();
st = con.createStatement();
String sql = "...";
int i = st.executeUpdate(sql);
if(i>0) System.out.println("删除成功!");
}
catch(Exception e){
e.printStackTrace();
}
finally{
JdbcUtils.release(com,st,rs);
}
}
}