March 9 (Use DBHelper tool to connect MySQL database for login verification)

Source: Internet
Author: User

A Use DBHelper with MySQL connection for the simplest login verification.

(1) Create a new Web project-----> select the SRC Import dbhelper toolkit--------> select Create a form (username, password, and button) from index.jsp under Web root--------> Use the action to submit this page to login.jsp for validation of the user name and password correctness. The specific operation code is as follows:

1. index.jsp

<body>

<form action= "login.jsp" method= "POST" >

User name: <input type= "text" name= "username" ><br>

Secret &nbsp; code: <input type= "password" name= "pwd" ><br>

<input type= "Submit" name= "commit" value= "Login" >

</form>

</body>

2. login.jsp

(In this link, you need to pay attention to import Dbhelper,resultset Toolkit, otherwise will be error; The table name "users" in the database is consistent with the table name in SQL, and the field name "username" "Password" is also unified, And pass these two parameters to the result set resultset, finally determine whether the RS exists in the database)

<% @page import= "Java.sql.ResultSet"%>

<%@ page language= "java" import= "java.util.*,dbhelper.*" pageencoding= "UTF-8"%>

<body>

<%

String sql= "SELECT * from users where username =? and password=? ";

String User=request.getparameter ("username");

String pwd=request.getparameter ("pwd");

Object[] Obj=new object[]{user,pwd};

ResultSet rs = dbhelper.getresultset (sql, obj);

if (Rs.next ())

Out.print ("Login Successful! ");

Else

Out.print ("Login Failed");

Rs.close ();

%>

</body>

3.users table

Username password

Admin 123

Note: The Users table has username, password two field names, and the name matches the parameter name obtained in the request method in login.jsp. And there is a record: "username": admin; Password ": 123.

(2) Start running (commissioning)

1. When you enter a field name such as "Jack", "Linda" in the text box of "user name", or enter "AAA", "111" in the Password box, the "Login failed" prompt statement will appear;

2. Only the user name "admin" and password "123" are entered in the database, the system will appear "Login successful!" "It also means that the verification is successful!"

March 9 (Use DBHelper tool to connect MySQL database for login verification)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.