1. Introduction to JDBC – JDBC (Java DataBase Connectivity) is a Java API for executing SQL statements. It provides unified access to multiple relational databases. It consists of a set of classes and interfaces written in the Java language. JDBC provides a benchmark against which more advanced tools and interfaces can be built to enable database developers to write database applications – Java, with its rugged, secure, easy-to-use, easy-to-understand, and automatic download from the network, is the preeminent language for writing database applications. All you need is a way for your Java application to talk to various databases. – JDBC can be used with Java on a variety of platforms, such as Windows, Mac OS and various versions of UNIX. – The JDBC library includes apis for each of the tasks mentioned below that are generally related to database use.


  1. JDBCImplementation of verification login code ideas

* Enter the user name and password. Compare the user information in the database to determine whether the login is successful. * 1

* MyJDBCUtils.getConnection()

* 2. Obtain the request object STMT

* conn.createStmtement()

* 3. Create a keyboard object and obtain the username and password

* 3.1 Create keyboard entry objects

* 3.2 Prompt the user for input

* 3.3 Obtaining user input

* 4, create SQL statement, put username and password into SQL statement

* 5. Execute the query and obtain the query result

* stmt.executeQuery(sql);

* 6. Determine whether the login is successful based on the query result. * 7

  1. JavaUtility class

In the process of Java development, some classes like Scanner and Random are often used in the code. They are classes for keyboard input and generation of Random numbers. They are called tool classes in Java, just like a tool.

When we write our own code, some code functions are similar to Java tools, such as connecting database and verifying login, which will take a long time. It is too troublesome to write once every time. We can try to write our own tool class, which can be directly used to guide the package to call, which can improve our development efficiency.

  1. Encapsulate JDBCUtility class
    1. Adds a method to get a database connection object
    2. Add a method to release the connection

The code is as follows:

Utility class code:

package com.qianfeng.util;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


/ * *

* JDBC utility classes

* There are methods to get connections

* @author dushine

* /

public class JDBCUtil {

/ * *

* The method to get the database connection

* @return Connection conn

* @throws SQLException

* /

public static Connection getConnection() throws SQLException {

String url = “jdbc:mysql://localhost:3306/class? useSSL=false”;

String user = “root”;

String password = “root”;

Connection conn = DriverManager.getConnection(url,user,password);

return conn;

}

/ * *

* The method to release the connection

* @param conn

* @throws SQLException

* /

public static void releaseSourse(Connection conn) throws SQLException {

if (conn ! = null) {

conn.close();

}

}

/ * *

* The method to release the connection

* @param conn Database connection object

* @param STMT The object that executes the SQL statement

* @throws SQLException

* /

public static void releaseSourse(Connection conn,Statement stmt) throws SQLException {

if (stmt ! = null) {

stmt.close();

}

if (conn ! = null) {

conn.close();

}

}

/ * *

* The method to release the connection

* @param conn Database connection object

* @param STMT The object that executes the SQL statement

* @param resultSet the resultSet returned by executing SQL statements

* @throws SQLException

* /

public static void releaseSourse(Connection conn,Statement stmt,ResultSet resultSet) throws SQLException {

if (resultSet ! = null) {

resultSet.close();

}

if (stmt ! = null) {

stmt.close();

}

if (conn ! = null) {

conn.close();

}

}

}



Test class code:

package com.qianfeng.demos;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Scanner;


import com.qianfeng.util.JDBCUtil;


public class Demo04 {

public static void main(String[] args) throws Exception {

/ * *

* Login and registration

* Get user input

* Query the contents of the database as a condition

* /

Scanner sc = new Scanner(System.in);

System.out.println(” Please enter username :”);

String name = sc.nextLine();

System.out.println(” Please enter password :”);

String pwd = sc.nextLine();

// Register the driver

Class.forName(“com.mysql.jdbc.Driver”);

/ *

String url = “jdbc:mysql://localhost:3306/class? useSSL=false”;

String user = “root”;

String password = “root”;

// Get the connection to the database

Connection conn = DriverManager.getConnection(url, user, password); * /

Connection conn = JDBCUtil.getConnection();

// Use the connection object to get the object executing the SQL

Statement stmt = conn.createStatement();

// Write an SQL statement

String sql = “select * from userinfo where username='”+name+”‘ and password='”+pwd+”‘”;

System.out.println(sql);

// Execute the SQL statement and get the result

ResultSet resultSet = stmt.executeQuery(sql);

if (resultSet.next()) {

System.out.println(” login successful! );

} else {

System.out.println(” Wrong username or password!” );

}

JDBCUtil.releaseSourse(conn, stmt, resultSet);

sc.close();

}

}