l JDBC

Introduction to the



(
Java DataBase Connectivity,java
Database connection) is a type used for execution
SQL
The statement
Java API
Can provide unified access to multiple relational databases, which are used by a group
Java
Classes and interfaces written by the language.
JDBC
Provides a benchmark against which more advanced tools and interfaces can be built to enable database developers to write database applications





It is robust, secure, easy to use, easy to understand, and automatically downloadable from the network, making it an outstanding language for writing database applications. All it takes is
Java
A way for an application to talk to various databases.





Available on a variety of platforms
Java
, such as
Windows
.
Mac OS
And various versions of it
UNIX
.





Libraries include each of the tasks mentioned below that are generally related to database use
API
.

l JDBC

Implementation of verification login code ideas

*

Enter the user name and password and check whether the login is successful by comparing the user information in the database


Connect to the database

* MyJDBCUtils.getConnection()

* 2

And get the request object
stmt

* conn.createStmtement()

* 3

Create a keyboard object and get the user name and password

* 3.1

Create a keyboard entry object

* 3.2

Prompt the user for input

* 3.3

Gets user input

* 4

, write,
SQL
Statement to put the username and password in
SQL
In the statement

* 5

, execute query
.
Obtaining query results

* stmt.executeQuery(sql);

* 6

. Determine whether the login is successful based on the query result


, close the connection

l Java

Utility class

in
In development
.
You use a few of them in your code
,
In the same class
.
They are keyboard inputs
.
Class that generates random numbers
.
It’s like a tool
.
in
Is called a utility class
.

When we write our own code
.
Some code functions and
Tools like
For example, it will take a long time to connect the database and verify login, so 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 and call each time we use it, which can improve our development efficiency.

l

encapsulation

JDBC

Utility class


n

Adds a method to get a database connection object

n

Add a method to release the connection

The following code

:


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 class

*

There are ways to get connections

* @author dushine

* /

public class JDBCUtil {

/ * *

*

Method to get a 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;

}

/ * *

*

A method to release the connection

* @param conn

* @throws SQLException

* /

public static void releaseSourse(Connection conn) throws SQLException {

if (conn ! = null) {

conn.close();

}

}

/ * *

*

A method to release the connection

* @param conn

Database connection object

* @param stmt

perform
Object of statement

* @throws SQLException

* /

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

if (stmt ! = null) {

stmt.close();

}

if (conn ! = null) {

conn.close();

}

}

/ * *

*

A method to release the connection

* @param conn

Database connection object

* @param stmt

perform
Object of statement

* @param resultSet

perform
Statement returns the result set

* @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 {

/ * *

*

Log in to register

*

Gets user input

*

Query the contents of the database using the input as a condition

* /

Scanner sc = new Scanner(System.in);

System.out.println(“

Please enter a user name
: “);

String name = sc.nextLine();

System.out.println(“

Please enter your password
: “);

String pwd = sc.nextLine();

//

Registration drive

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

/ *

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

String user = “root”;

String password = “root”;

//

Gets a connection to the database

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

Connection conn = JDBCUtil.getConnection();

//

Use connection objects to get execution
sql
The object of

Statement stmt = conn.createStatement();

//

write
SQL
statements

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

System.out.println(sql);

//

perform
SQL
Statement to get the return 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();

}

}