private static final String CLASS_FORNAME = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
private static final String USER = "ROOT";
private static final String PASSWORD = "123456";
static{
try {
Class.forName(CLASS_FORNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn(){
try {
return DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection conn, Statement ps, ResultSet rs){
try {
conn.close();
ps.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean update(String sql,Object...obj){
Connection conn = getConn();
PreparedStatement ps = null;
try{
ps =conn.prepareStatement(sql);
if (Objects.nonNull(obj)) {
for (int i = 0; i < obj.length; i++) {
ps.setObject(i+1,obj[i]);
}
}
int b = ps.executeUpdate();
return b >0;
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(conn,ps,null);
}
return false;
}
Thinking: the JDBC database operation step cannot modify data set as a constant, then the access to database objects and close the resources as method, will be updated as method, namely the deletion operation, the operation of SQL to update for us, then the array as the incoming conditions, first of all determine whether array is empty, if the data is not empty
Read More:
- Spring boot thymeleaf crud implements simple functions of adding, deleting, modifying and querying
- Oracle can’t start the database due to deleting DBF file by mistake
- The problem of master-slave database synchronization stop caused by MySQL deleting data
- Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communicat
- MySQL error: error 1010 (HY000) when deleting database
- Error: invalid permission for adding an object to repository database
- Methods of modifying index and columns names by dataframe in pandas
- Method of modifying file and folder permission by Chmod command in Linux
- The processing method after deleting idea’s. IML file by mistake
- How to Fix Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
- In the XML configuration file of spring jdbc.properties The connection to the database failed due to garbled code
- java.lang.ClassNotFoundException : com.mysql.jdbc . driver project cannot connect to database, exception, lack of MySQL driver
- Transfer: completely solve the problem that the browser home page is tampered by deleting the script file
- After switching the tidb database, an error could not commit JDBC transaction appears from time to time
- solve com.mysql.jdbc . exceptions.jdbc4 .MySQLSyntaxErrorException:
- Three ways of adding cookie by scratch
- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure (How to Fix)
- Solution to error reporting on the client caused by adding fields on the CXF server
- When executing hive – f script com.mysql.jdbc . exceptions.jdbc4 .CommunicationsException: Communications link failure
- Adding prefixes to file names in batch by shell under mac