DBUtil
public class DBUtil {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql:///数据库名字";
private static final String USERNAME = "root";
private static final String PASSWORD = "1234";
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static int curd(String sql,Object...objects) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConn();
ps = conn.prepareStatement(sql);
if(null != objects) {
for(int i=0;i<objects.length;i++) {
ps.setObject(i+1, objects[i]);
}
}
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(conn,ps,null);
}
return 0;
}
public static void close(Connection conn,Statement st,ResultSet rs) {
try {
if(null != rs) {
rs.close();
}
if(null != st) {
st.close();
}
if(null != conn) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}