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-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to achieve authentication login with JDBC". In daily operation, I believe many people have doubts about how to achieve authentication login with JDBC. 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 about "how to achieve authentication login with JDBC". 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 to build more advanced tools and interfaces that enable database developers to write database applications-Java is an excellent language for writing database applications because it is robust, secure, easy to use, easy to understand, and automatically downloadable 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.

JDBC implements authentication login

-create a Scanner object, prompt and get the user name and password entered by the user

-connect to the database. -write the user name and password entered by the user into the sql statement as query conditions-execute the query and obtain the query results-determine whether the account number and password entered by the user are correct according to the query results

Idea of verifying login code

* 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.

JDBC implements login verification case code

Package com.qf.demos

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 Demo05 {

Public static void main (String [] args) throws SQLException {

String url = "jdbc:mysql://localhost:3306/class"

String user = "root"

String password = "root"

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

Statement stmt = conn.createStatement ()

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 ()

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

/ / String sql = "select * from user where username='zhangsan' and password='lisi' or'1"

ResultSet resultSet = stmt.executeQuery (sql)

System.out.println (resultSet)

If (resultSet.next ()) {

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

} else {

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

}

ResultSet.close ()

Stmt.close ()

Conn.close ()

Sc.close ()

}

}

After running the code, we enter something like this:

User name: zhangsan password: sanzhang

With such a user name and password, the content obtained can be compared with the content in the database, and the corresponding result can be obtained.

If there is user input similar to:

User name: zhangsan password: sansan'or '1password

When it comes to such content, even the wrong result will be verified, because the SQL statement is written as: select * from userinfo where username='' and password='' or'1.

The result of this verification is always correct, which is called SQL injection.

At this point, the study on "how to achieve authentication login with JDBC" 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