Code example for implementing the user registration function in jdbc, jdbc User Registration

Source: Internet
Author: User

Code example for implementing the user registration function in jdbc, jdbc User Registration

The main problem studied in this article is that jdbc implements the user registration function and learns the JSP + MySQL database connection, access method, and addition, deletion, query, and modification operations through the specific instance Code, as follows.

The client register. jsp interface is as follows:

  • "Check" button: checks whether the user name exists.
  • "Register" function: only after all the verification is passed can the insert. jsp be submitted. insert. jsp is used to save the account and password to the user table.

// Register. jsp

<Head> <link rel = "stylesheet" type = "text/css" href = "style.css" rel = "external nofollow"> <script src = "scripts/jquery-1.5.1.js" type = "text/javascript"> </script> <script type = "text/javascript" >$ (document ). ready (function () {$ (": input. required "). each (function () {// a red star ID is required. var $ required = $ ("<strong class = 'high'> * </strong>"); // create an element $ (this ). parent (). append ($ required); // Add a *} Behind the div); $ (': input '). blur (Function () {// Add the event var $ parent =$ (this) for all inputs ). parent (); $ parent. find (". formtips "). remove (); // Delete the previous prompt element if ($ (this ). is ('# username') {// is method to determine whether the user name is if (this. value = "" | this. value. length <6 | this. value. length> 18) {var errorMsg = 'enter a user name of 6 to 18 characters. '; $ parent. append ('<span class = "formtips onError">' + errorMsg + '</span>');} else {var okMsg = 'the input is correct. '; $ parent. append ('<span class = "formtips o NSuccess "> '+ okMsg +' </span> ') ;}} if ($ (this ). is ('# password') {// whether the password meets the format if (this. value = "" | (this. value! = ""&&! (/^ \ D {1, 6} $ /). test (this. value) {// verify the regular expression var errorMsg = 'the password must be 1-6 digits. '; $ parent. append ('<span class = "formtips onError">' + errorMsg + '</span>');} else {var okMsg = 'the input is correct. '; $ parent. append ('<span class = "formtips onSuccess">' + okMsg + '</span>') ;}} if ($ (this ). is ('# re_password') {// obtain the node with the id re_password. When the cursor leaves the input box, run the function method var password =$ ("# password "). val (); // get the content var repasswo in the input box with the id as password Rd = $ ("# re_password"). val (); if (password! = Repassword) {// determine whether two variables are equal var errorMsg = 'the two passwords are inconsistent. '; $ parent. append ('<span class = "formtips onError">' + errorMsg + '</span>');} else {var okMsg = 'is verified. '; $ parent. append ('<span class = "formtips onSuccess">' + okMsg + '</span>') ;}}); // end blur $ (': input '). keyup (function () {// code of the keyup event function of the input element in this example is the same as that of blur $ (this ). triggerHandler ("blur"); // triggers the blur event}); $ (': input '). focus (function () {// In this example, the focus event function code of the input element is the same as that of blur $ (this ). triggerHandler ("blur"); // triggers the blur event}); $ ('# send '). click (function () {// register button $ (": input. required "). trigger ('blur'); var numError = $ ('form. onerror '). length; // class = "onError" count if (numError> 0) {// return false;}); $ ('# res '). click (function () {// reset button $ (". formtips "). remove () ;}); </script> 

// Insert. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" gb2312 "%> <% @ page import =" javaBean. userbean; "%> 

Userbean. java

Package javaBean; import java. SQL. *; public class userbean {private String username; private String password; public void setUsername (String username) {this. username = username;} public void setPassword (String password) {this. password = password;} public String getUsername () {return username;} public String getPassword () {return password;} public String insert () {try {String url = "jdbc: mysql: // localhost: 3306/mysq L "; // database connection string Class. forName ("org. gjt. mm. mysql. driver "). newInstance (); // load the driver Connection conn = DriverManager. getConnection (url, "root", "dba"); // create a connection String SQL = "select * from login_user where username =? "; PreparedStatement pStmt = conn. prepareStatement (SQL); pStmt. setString (1, username); ResultSet rs1_pstmt.exe cuteQuery (); if (rs. next () {return "this user name already exists! ";} Else {SQL =" insert into login_user values (?,?) "; PStmt = conn.prepareStatement( SQL );pStmt.setString(1,username);pStmt.setString(2,password);pStmt.exe cuteUpdate (); return" registration successful! ";}} Catch (Exception e) {return" registration failed! ";}}}

Summary

The above is all the sample code for implementing the user registration function in jdbc. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.