The main problem studied in this article is that jdbc implements user registration function, and learns JSP+MySQL database connection, access methods, and addition, deletion, and retrieval and modification operations through specific example codes. The details are as follows.
The client register.jsp interface is as follows
//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(){ //Required to be filled with red star logo. var $required = $("<strong class='high'> *</strong>"); //Create element $(this).parent().append($required); //Add a * } after the div; $(':input').blur(function(){ //Add a lost focus event for all inputs var $parent = $(this).parent(); $parent.find(".formtips").remove(); //Delete the previous prompt element if( $(this).is('#username') ){ //The is method determines whether it is the username if( this.value=="" || this.value.length < 6||this.value.length > 18 ){ var errorMsg = 'Please enter a username of 6-18 digits.'; $parent.append('<span>'+errorMsg+'</span>'); } else{ var okMsg = 'Input correctly.'; $parent.append('<span>'+okMsg+'</span>'); } } if( $(this).is('#password') ){ //Does the password meet the format if( this.value=="" || ( this.value!="" && !(/^/d{1,6}$/).test(this.value) ) ){ //Regular verification var errorMsg = 'The password must be 1-6 digits.'; $parent.append('<span>'+errorMsg+'</span>'); } else{ var okMsg = 'The input is correct.'; $parent.append('<span>'+okMsg+'</span>'); } } if( $(this).is('#re_password') ){ //Get the node with id re_password and listen when the cursor leaves the input box, run the function method var password = $("#password").val(); //Get the content in the input box with id password var repassword = $("#re_password").val(); if(password!=repassword) { //Judge whether the two variables are equal var errorMsg = 'The password input is inconsistent between the two times.'; $parent.append('<span>'+errorMsg+'</span>'); } else{ var okMsg = 'pass verification.'; $parent.append('<span>'+okMsg+'</span>'); } } }); //end blur $(':input').keyup(function(){ //The keyup event function code in this example is the same as that of blur $(this).triggerHandler("blur"); //Trigger event}); $(':input').focus(function(){ //The function code of this input element focus event is the same as that of blur$(this).triggerHandler("blur"); //Trigger event}); $('#send').click(function(){ //Register button$(":input.required").trigger('blur'); var numError = $('form .onError').length; //class="onError" number if(numError>0){ //There is a mistake and return false; } }); $('#res').click(function(){ //Reset button$(".formtips").remove(); }); });</script></head><body> <form method="post" action="insert.jsp"> <div> <label>Username:</label> <input type="text" name="username" id="username" /> </div> <div> <label>Password:</label> <input type="text" name="password" id="password" /> </div> <div> <label>Enter password again:</label> <input type="text" id="re_password" /> </div> <div> <input type="submit" value="register" id="send" /> <input type="reset" value="return" id="res" /> </div> </form></body>//insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%@page import="javaBean.userbean;"%><html><body> <jsp:useBean id="user" scope="session" /> <jsp:setProperty name="user" property="*" /> Username: <jsp:getProperty name="user" property="username"/> <br><br> Password: <jsp:getProperty name="user" property="password"/> <br><br> <% out.println(user.insert()); %></body></html>
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/mysql";//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 rs=pStmt.executeQuery();if(rs.next()) {return "This username already exists!";} else {sql="insert into login_user values(?,?)";pStmt = conn.prepareStatement(sql);pStmt.setString(1,username);pStmt.setString(2,password);pStmt.executeUpdate();return "Register successful!";}}catch(Exception e){return "Register failed!";}}} Summarize
The above is all the content of this article about the code example of jdbc implementing user registration function, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!