JDBC access database steps

  I have recently started to review JAVASE. Let's take a look at some of the points that I forgot before.

  JDBC access to the database is quite confusing before, and now it is time to review it. The database connection must first have a JDBC driver and a database. The two here do not belong to the knowledge of JAVA and will not be introduced. Just an introduction to JDBC programming.

  There are 6 steps in JDBC programming, which are 1. Load the sql driver, 2. Use the DriverManager to obtain the database connection, 3. Use the Connecttion to create a Statement object The Statement object is used to execute SQL statements, 4. Execute SQL statements, 5. Operate the result set , 6. Recycling database resources. The above are the steps of most JDBC programming, you can also reduce your own steps according to your own situation. Because it is a review, there are still many comments in the code. No more exhausting description here. code show as below:

package JDBCtrain;

import java.sql.*;

public class demo1 {
	public static void main(String[] args) throws Exception {
		// 1. Load the sql driver
		// Load the driver and use reflection knowledge
		Class.forName("com.mysql.jdbc.Driver");
		try (
		/*
		 * 2. Use the DriverManager to get the database connection. The conn returned represents the connection between the java program and the database. Pay attention to the imported package.
		 * It should be noted here that root@localhost and root@'127.0.0.1' are two different users. There is also the issue of account and password.
		 * Access denied for user '123'@'localhost' (using password: YES) is a password error
		 */
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://127.0.0.1:3306/javatrain", "123", "123456");
		/*
		 * 3. Use Connecttion to create a Statement object Statement object is used to execute SQL statements
		 */
		Statement stmt = conn.createStatement();
		/*
		 * 4. Execute the SQL statement
		 * Statement has three ways to execute SQL statement
		 * (1) executeQuery(sql) executes the select statement and returns the result set from the query
		 * (2)execute(sql) can execute any SQL statement and return a boolean value
		 * If the first result of execution is a ResultSet, return true, otherwise return false
		 * (3)executeUpdate(sql) executes the DML statement and returns an integer (representing the number of rows affected by the SQL statement)
		 */
		ResultSet rs=stmt.executeQuery("select * from demo1");
		)
		/*
		 * The feature of java7, you can use the try statement to automatically close various resources of the database
		 */
		{
			//5. Operation result set
			/*
			 * ResultSet is the returned result set, get() is to get the value of a specific column next is the next row
			 */
			while (rs.next())
			{
				System.out.println("Name is: "+rs.getInt(1)+", Password is: "+rs.getInt(2)+".");
			}
		}
	}

}

  Let’s talk about what to pay attention to in the code below. Because of the different databases, the code when getting the database connection is not necessarily the same. Here I have been stuck for a long time because of the database password (be sure to remember the password of my own database), and the sixth Step, the code is not written, it is actually a feature of JAVA7, allowing database resources to be placed in the try block, and the system will automatically recycle database resources at the end. This is also the recommended way of writing in the enterprise. There is the third step, we must pay attention that Statement cannot prevent SQL injection (the common way of Cracker intrusion, exploiting the vulnerability of SQL statement to intrusion), PreparedStatement can be used in login. The above is a simple example. end.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325377856&siteId=291194637