No nonsense, on the code
1 PackageCom.ningmeng;2 3 ImportJava.sql.*;4 5 /**6 * 1: Change the data in the database7 * @authorBiexiansheng8 *9 */Ten Public classTest04 { One A Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub - Try { theClass.forName ("Com.mysql.jdbc.Driver");//Load Database driver -SYSTEM.OUT.PRINTLN ("Load Database driver succeeded"); -String url= "Jdbc:mysql://localhost:3306/test";//declaring the URL of the database test -String user= "root";//Database Account +String password= "123456";//Database Password - //establish a database connection and get the connection object Conn +Connection conn=drivermanager.getconnection (URL, user, password); ASYSTEM.OUT.PRINTLN ("Connection database succeeded"); atString sql= "Update users set age=20 where id=1";//generate a MySQL statement -Statement stmt=conn.createstatement ();//Create a statement object -Stmt.executeupdate (SQL);//Execute SQL statement -SYSTEM.OUT.PRINTLN ("Modify Database Succeeded"); - conn.close (); -SYSTEM.OUT.PRINTLN ("Close Database Success"); in}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block to e.printstacktrace (); +}Catch(SQLException e) { - //TODO auto-generated Catch block the e.printstacktrace (); * } $ Panax Notoginseng - } the +}
The results are as follows
The contrast represents a successful change, OK.
Attention:
Modifying a database is an essential part of database operations, using the Excuteupdate () method in the statement interface to modify data in a data table. You can also modify the tables in the database by using the Excuteupdate method in the PreparedStatement interface.
1 PackageCom.ningmeng;2 3 ImportJava.sql.*;4 5 /**6 * @authorBiexiansheng7 *8 */9 Public classTest05 {Ten One Public Static voidMain (string[] args) { A //TODO auto-generated Method Stub - Try { -Class.forName ("Com.mysql.jdbc.Driver");//Load Database driver theSYSTEM.OUT.PRINTLN ("Load Database driver succeeded"); -String url= "Jdbc:mysql://localhost:3306/test";//declaring the URL of the database test -String user= "root";//Database Account -String password= "123456";//Database Password + //establish a database connection and get the connection object Conn -Connection conn=drivermanager.getconnection (URL, user, password); +SYSTEM.OUT.PRINTLN ("Connection database succeeded"); A atString sql= "Update users set password=?" where sex=? ";//generate a MySQL statement -PreparedStatement ps=conn.preparestatement (SQL);//Create a PreparedStatement object -Ps.setstring (1, "admin");//assigns a value to the first question mark -Ps.setint (2, 0);//assign a value to the second question mark - intCount=ps.executeupdate ();//Execute SQL statement -SYSTEM.OUT.PRINTLN ("Modify Database Succeeded"); in conn.close (); -SYSTEM.OUT.PRINTLN ("Close Database Success"); to +}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block the e.printstacktrace (); *}Catch(SQLException e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); - } the + } A the}
Contrast, I know that has been modified
Modify the data as shown above is modified according to certain conditions, this condition can be fixed, or can be a range, respectively, is the first, the second case.
The second case modifies the data in the Database users table using the Executeupdate () method in the PreparedStatement interface. (To change all gender 0 user password to admin, it should be noted that I have to create a data table when the gender is an int type, only 0,1,2 three to represent, so the reference case needs to pay attention to the code changes)
Java changes data in the database