Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Authentication Login with JDBC

2025-05-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use JDBC to achieve authentication login". In daily operation, I believe that many people have doubts about how to use JDBC to achieve authentication login. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use JDBC to achieve authentication login". Next, please follow the editor to study!

JDBC (Java DataBase Connectivity,java Database connection) is a Java API used to execute SQL statements, which can provide unified access to a variety of relational databases. It consists of a set of classes and interfaces written in Java language. JDBC provides a benchmark from which more advanced tools and interfaces can be built to enable database developers to write database applications

-Java is an excellent language for writing database applications because it is sturdy, secure, easy to use, easy to understand and can be downloaded automatically from the network. All that is needed is a way for Java applications to talk to a variety of different databases.

-JDBC can use Java on various platforms, such as Windows,Mac OS and various versions of UNIX.

The-JDBC library includes the API for each of the tasks mentioned below, which is usually associated with database usage.

The idea of verifying Login Code by lJDBC

* enter the user name and password on the keyboard to compare the user information in the database to determine whether the login is successful or not

* 1. Connect to the database

* MyJDBCUtils.getConnection ()

* 2. Get the request object stmt

* conn.createStmtement ()

* 3. Create a keyboard object to obtain the user name and password

* 3.1 create a keyboard entry object

* 3.2 prompt the user for input

* 3.3 get user input

* 4. Write a SQL statement and put the user name and password in the SQL statement

* 5. Execute the query to get the query result

* stmt.executeQuery (sql)

* 6. Judge whether the login is successful according to the query result.

* 7. Close the connection

LJava utility class

In the process of java development, some classes like Scanner and Random are often used in the code. They are keyboard input classes that generate random numbers. Like a tool, they are called tool classes in java.

When we write our own code, some code functions are similar to java tools, such as connecting to the database, verifying login, the length of time will be used, every time it is too troublesome to write our own tool class, we can try to write our own tool class, and guide the package call directly every time we use it, which can improve our development efficiency.

L encapsulates the JDBC utility class

N join the method of getting database connection objects

N add the method of releasing the connection

The code is as follows:

Tool 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 tool class

* there is a way to obtain a connection

* @ author dushine

, /

Public class JDBCUtil {

/ * *

* method of obtaining 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

}

/ * *

* ways to release connections

* @ param conn

* @ throws SQLException

, /

Public static void releaseSourse (Connection conn) throws SQLException {

If (conn! = null) {

Conn.close ()

}

}

/ * *

* ways to release connections

* @ param conn database connection object

* @ param stmt 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 ()

}

}

/ * *

* ways to release connections

* @ param conn database connection object

* @ param stmt object that executes the SQL statement

* @ param resultSet the result set returned by executing the SQL statement

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

/ * *

* sign in and register

* get user input

* use the input as the content in the conditional query database

, /

Scanner sc = new Scanner (System.in)

System.out.println ("Please enter user name:")

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"

/ / obtain the connection to the database

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

Connection conn = JDBCUtil.getConnection ()

/ / use the connection object to get the object that performs sql

Statement stmt = conn.createStatement ()

/ / write SQL statements

String sql = "select * from userinfo where username='" + name+ "'and password='" + pwd+ "'"

System.out.println (sql)

/ / execute the SQL statement to get the returned result

ResultSet resultSet = stmt.executeQuery (sql)

If (resultSet.next ()) {

System.out.println ("login succeeded!")

} else {

System.out.println ("wrong username or password!")

}

JDBCUtil.releaseSourse (conn, stmt, resultSet)

Sc.close ()

}

}

At this point, the study on "how to use JDBC to achieve authentication login" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report