There are many ways to update a database in java. We often use rs. updateRow () to update a database. This is a method. Of course there are other methods. The following describes how to update a database.
I learned how to use java to connect to the mysql database and read the value of a field in the database. This time, I need to update the database. For entry-level learners, there are two simplest methods. Let's take a look at how to read the database:
The Code is as follows: |
Copy code |
Statement st = con. createStatement (); ResultSet rs = st.exe cuteQuery ("SELECT * FROM user where name = '" + name + "'"); |
Yes, we use the select statement, while the update database uses the update Statement (who threw the stone, nnd I said this is an entry-level tutorial ).
The Code is as follows: |
Copy code |
Statement st = con. createStatement (); ResultSet rs = st.exe cuteQuery ("UPDATE user set password = '" + password + "'" where name = '"+ name + "'"); |
In fact, there is another way to use select to first find the record that the data matches, and then directly use updateRow ()
To update records:
The Code is as follows: |
Copy code |
Statement st = con. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE ); ResultSet rs = st.exe cuteQuery ("SELECT * FROM user where name = '" + name + "'"); Rs. updateString ("password", password ); Rs. updateRow (); |
I personally think that the advantage of the 2nd method is that it is not easy to get syntax errors due to a few quotation marks. Note that the createStatement () of the 2nd methods has parameters and must be updatable. Otherwise, re. updateRow () will report an error.
The delete syntax is as follows:
The Code is as follows: |
Copy code |
Statement st = con. createStatement (); St.exe cute ("delete from user where name = '" + name + "'"); |
Another method
Create a simple database as follows:
The Code is as follows: |
Copy code |
Import java. SQL .*; Public class GetConnection { Public static void main (String [] args ){ Try { // Call the Class. forName () method to load the driver Class. forName ("com. mysql. jdbc. Driver "); System. out. println ("MySQL driver loaded successfully! "); } Catch (ClassNotFoundException e1 ){ System. out. println ("MySQL driver not found! "); E1.printStackTrace (); }
String url = "jdbc: mysql: // localhost: 3306/mysql"; // JDBC URL // Call the getConnection () method of the DriverManager object to obtain a Connection object Connection conn; Try { Conn = DriverManager. getConnection (url, "root ",""); // Create a Statement object Statement stmt = conn. createStatement (); // create a Statement object System. out. print ("successfully connected to the database! "); Stmt. close (); Conn. close (); } Catch (SQLException e ){ E. printStackTrace (); } } }
|
2. query data tables
When querying a data table, you need to use the ResultSet interface, which is similar to a data table. The instance of this interface can obtain the retrieval result set and the interface information of the corresponding data table.
The Code is as follows: |
Copy code |
Import java. SQL .*;
Public class SelectTable {
Public static void main (String [] args ){ Try { // Call the Class. forName () method to load the driver Class. forName ("com. mysql. jdbc. Driver "); System. out. println ("MySQL driver loaded successfully! ");
String url = "jdbc: mysql: // localhost: 3306/aniu"; // URL of JDBC Connection conn; Conn = DriverManager. getConnection (url, "root ",""); Statement stmt = conn. createStatement (); // create a Statement object System. out. println ("successfully connected to the database! "); String SQL = "select * from stu"; // SQL to be executed ResultSet rs = stmt.exe cuteQuery (SQL); // create a Data Object System. out. println ("no." + "t" + "name" + "t" + "Age "); While (rs. next ()){ System. out. print (rs. getInt (1) + "t "); System. out. print (rs. getString (2) + "t "); System. out. print (rs. getInt (3) + "t "); System. out. println (); } Rs. close (); Stmt. close (); Conn. close (); } Catch (Exception e) { E. printStackTrace (); } } } |
Modify and delete Databases
The Code is as follows: |
Copy code |
// Modify and delete data Import java. SQL .*; Public class UpdateDeleteDemo { Public static void main (String [] args) throws Exception { Try { // Call the Class. forName () method to load the driver Class. forName ("com. mysql. jdbc. Driver "); System. out. println ("MySQL driver loaded successfully! ");
String url = "jdbc: mysql: // localhost: 3306/aniu"; // URL of JDBC Connection conn; Conn = DriverManager. getConnection (url, "root ",""); Statement stmt = conn. createStatement (); // create a Statement object System. out. println ("successfully connected to the database! "); // Code for querying data String SQL = "select * from stu"; // SQL to be executed ResultSet rs = stmt.exe cuteQuery (SQL); // create a Data Object System. out. println ("no." + "t" + "name" + "t" + "Age "); While (rs. next ()){ System. out. print (rs. getInt (1) + "t "); System. out. print (rs. getString (2) + "t "); System. out. print (rs. getInt (3) + "t "); System. out. println (); }
// Modify the data code String sql2 = "update stu set name =? Where number =? "; PreparedStatement pst = conn. prepareStatement (sql2 ); Pst. setString (1, "8888 "); Pst. setInt (2,198 ); Pst.exe cuteUpdate ();
// Data deletion code String sql3 = "delete from stu where number =? "; Pst = conn. prepareStatement (sql3 ); Pst. setInt (1,701 ); Pst.exe cuteUpdate ();
ResultSet rs2 = stmt.exe cuteQuery (SQL); // create a Data Object System. out. println ("no." + "t" + "name" + "t" + "Age "); While (rs. next ()){ System. out. print (rs2.getInt (1) + "t "); System. out. print (rs2.getString (2) + "t "); System. out. print (rs2.getInt (3) + "t "); System. out. println (); }
Rs. close (); Stmt. close (); Conn. close (); } Catch (Exception e) { E. printStackTrace (); } } } |