Java updates database methods summary

Source: Internet
Author: User
Tags stmt

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 ();
            }
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.