This paper introduces the implementation of Statement interface to query data and add data. In the basic application of JDBC, the steps of querying and adding data using the Statement interface are introduced. The key point is to use the getConnection() method to connect to the database, create the Statement object, and call the createStatement() method of the Connection object to create the MySQL Statement object. |
1, Foreword
In JDBC technology, different databases need different drivers. First load the driver, then connect the database, and then use SQL statements to execute the database. This article introduces you how to use the Statement interface to query and add data. Next, Xiaobian will take you to learn!
2, Basic application of JDBC
After connecting with a database in the program, you can use SQL statements to interact with the tables in the database. For example, you can operate the records in the table by adding, deleting, modifying and querying. These interactions are implemented through an API interface of JDBC. The Statement interface provided by JDBC sends the SQL statement to the database, executes the SQL statement, returns a result stored in a ResultSet object, and calls the next() method of the object to get the data.
The steps to query data using the Statement interface are as follows:
- First import the expansion package "mysql-connector-java-5.1.7-bin.jar", right-click the current project of Ecilpse editing software, select "Bulid Path", and then select "Configure Build Path...", Select Libraies, and there is an "Add External JARs..." on the right Button to add this expansion package, and then click "OK". The specific operation is shown in the figure below:
- Use class The forname() method to load the driver.
- After successfully loading the driver, class The forname () method registers itself with the DriverManager, then uses the getConnection() method to connect with the database and return a Connection object.
- Create a Statement object using the createStatement() method of the Connection object.
- Use the Statement object to call the executeQuery() method to query the database table, and store the query results in a ResultSet object.
- Use the next() method of the ResultSet object to get the data in the table.
3, Understand the usage of the Statement interface to query data through a case
The above describes the specific steps of querying data by the Statement interface. Next, Xiaobian will take you to understand the usage of querying data by the Statement interface. The data and code in the student table are as follows:
Data in student table:
code:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class Example30 { public static void main(String[] args) { System.out.println("Please enter the information you want to query ID:"); Scanner sc=new Scanner(System.in); String input=sc.next(); String driver="com.mysql.jdbc.Driver"; try { //Load driver Class.forName(driver); //Database address, local machine, port number 3306, database name test String url="jdbc:mysql://localhost:3306/test"; //user name String user="root"; //password String pwd="168168"; //Connect database Connection conn=DriverManager.getConnection(url,user,pwd); //Create Statement object Statement stmt=conn.createStatement(); String sql="select * from student where id='"+input+"'"; //Execute SQL statement ResultSet rs=stmt.executeQuery(sql); //Obtain data according to the ID value entered by the user if(rs.next()){ System.out.println("id:"+rs.getString("id")+" name:"+rs.getString("name")+" age:"+rs.getInt("age")); }else{ System.out.println("You entered ID non-existent!"); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
The renderings are as follows:
In the above code, first load the driver, then use the getConnection() method to connect to the database, create the Statement object, call the createStatement() method of the Connection object to create the MySQL Statement object, and call the executeQuery() method on this object to process the query results.
4, Implementation steps of adding data to Statement interface
- First import the expansion package "mysql-connector-java-5.1.7-bin.jar", right-click the current project of Ecilpse editing software, select "Bulid Path", and then select "Configure Build Path...", Select Libraies, and there is an "Add External JARs..." on the right Button to add this expansion package, and then click "OK". The specific operation is shown in the figure below:
- Use class The forname() method to load the driver.
- After successfully loading the driver, class The forname () method registers itself with the DriverManager, then uses the getConnection() method to connect with the database and return a Connection object.
- Create a Statement object using the createStatement() method of the Connection object.
- Use the Statement object to call the executeUpdate() method to query the database table and store the query results in a ResultSet object.
- Use the next() method of the ResultSet object to get the data in the table.
5, Understand the usage of adding data to the Statement interface through a case
The above describes the implementation steps of adding data to the Statement interface. Next, Xiaobian will take you to understand the usage of adding data to the Statement interface. The code is as follows:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class Example31 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Please enter the you want to add ID:"); String id=sc.next(); System.out.println("Please enter the you want to add Name:"); String name=sc.next(); System.out.println("Please enter the you want to add Age:"); int age=sc.nextInt(); String driver="com.mysql.jdbc.Driver"; try { //Load driver Class.forName(driver); //Database address, local machine, port number 3306, database name test String url="jdbc:mysql://localhost:3306/test"; //user name String user="root"; //password String pwd="168168"; //Connect to database Connection conn=DriverManager.getConnection(url,user,pwd); //Create Statement object Statement stmt=conn.createStatement(); String sql="insert into student values('"+id+"','"+name+"',"+age+")"; //Execute SQL statement stmt.executeUpdate(sql); sql="select * from student where id='"+id+"'"; //Execute SQL statement ResultSet rs=stmt.executeQuery(sql); //Get data according to ID value if(rs.next()){ System.out.println("id:"+rs.getString("id")+" name:"+rs.getString("name")+" age:"+rs.getInt("age")); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
The renderings are as follows:
Data in the table:
In the above code, first load the driver, then use the getConnection() method to connect to the database, create the Statement object, call the createStatement() method of the Connection object to create the MySQL Statement object, and call the executeUpdate method on this object to process.
6, Summary
This paper introduces the implementation of Statement interface to query data and add data.
In the basic application of JDBC, the steps of querying and adding data using the Statement interface are introduced. The key point is to use the getConnection() method to connect to the database, create the Statement object, and call the createStatement() method of the Connection object to create the MySQL Statement object.
For the interface query data, call the executeQuery() method on this object to process the query results; Add data to the interface and call the executeUpdate method on this object to process it. And through a specific case to help you understand its usage.
I hope you can help you through the study of this article!