JSP Link Database

Source: Internet
Author: User

Database table code:

/*
Navicat MySQL Data Transfer

Source server:localhost_3306
Source Server version:50528
Source host:localhost:3306
Source Database:bbs

Target Server Type:mysql
Target Server version:50528
File encoding:65001

date:2016-06-04 09:25:01
*/

SET foreign_key_checks=0;

-- ----------------------------
--Table structure for Tb_user
-- ----------------------------
DROP TABLE IF EXISTS ' Tb_user ';
CREATE TABLE ' Tb_user ' (
' id ' int (one) not NULL auto_increment,
' username ' varchar (255) is not NULL,
' Password ' varchar (255) is not NULL,
' Linkman ' varchar (255) DEFAULT NULL,
' Company ' varchar (255) DEFAULT NULL,
' Companytel ' varchar (255) DEFAULT NULL,
' QQ ' varchar (255) DEFAULT NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=3 DEFAULT Charset=utf8;

-- ----------------------------
--Records of Tb_user
-- ----------------------------
INSERT into ' tb_user ' VALUES (' 1 ', ' admin ', ' admin ', NULL, NULL, NULL, NULL);
INSERT into ' tb_user ' VALUES (' 2 ', ' User ', ' user ', NULL, NULL, NULL, NULL);

Registration page:

<%@ page language= "java" import= "java.util.*,java.sql.*" pageencoding= "Utf-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<%@ page import= "java.sql.*"%>
<%!
public static final String dbdriver = "Com.mysql.jdbc.Driver";
public static final String userName = "root"; Logged-In User name
public static final String userpasswd = "Taizhen"; Log in to MySQL password
public static final String dbName = "Test"; Database name
public static final String tablename= "user"; Table name
public static final String Dburl = "jdbc:mysql://localhost:3306/" +dbname+ "? user=" +username+ "&password=" + USERPASSWD;
%>

<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Boolean flag = false; Flag indicating success or failure of the login
%>

<%

String action = request.getparameter ("action");
if (null==action) {

}else{
%>

<%out.println (action);%>
<%
String name = Request.getparameter ("username"); Receive form parameters
String password = request.getparameter ("password");//Receive form Parameters
try{
Class.forName (dbdriver);
Co nn = drivermanager.getconnection (dburl);
String sql = "Select Name,password from user WHERE name=? and password=? ";
pstmt = conn.preparestatement (sql);
Pstmt.setstring (1,name);
Pstmt.setstring (2,password);
rs = Pstmt.executequery ();
while (Rs.next ()) {
//If there is content, it is executed here, indicating that the query came out, the legitimate user
flag = true;
}
}catch (Exception e) {
}finally{
try{
Conn.close ();//connection closed, all operations will be closed
}catch (Exception e) {}
}
%>
<%out.println (flag);%>
<%out.println (action);%>
<%

if (flag) {
Request.getsession (). SetAttribute ("Success", "Login");
%>
<jsp:forward page= "success.jsp"/>
<%
}else{//Login failed, jump to failure.jsp
Request.getsession (). SetAttribute ("error", "Login");

%>
<jsp:forward page= "error.jsp"/>
<%
}
}

%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>my JSP ' login.jsp ' starting page</title>


<body>
<form action= "login.jsp" method= "POST" >
<input type= "hidden" name= "action"/>
User name:
<input type= "text" name= "username" value= "username"/>
<br>
Password:
<input type= "password" name= "password" value= "password"/>
<br>
<input type= "Submit" value= "Login"/>
</form>

</body>

JSP Link Database code:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<%@ page import= "java.sql.*"%>
<%!
public static final String dbdriver = "Com.mysql.jdbc.Driver";
public static final String userName = "root"; Logged-In User name
public static final String userpasswd = "Taizhen"; Log in to MySQL password
public static final String dbName = "Test"; Database name
public static final String tablename= "user"; Table name
public static final String Dburl = "jdbc:mysql://localhost:3306/" +dbname+ "? user=" +username+ "&password=" + USERPASSWD;
%>

<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Boolean flag = false; Flag indicating success or failure of the login
%>

<%

String action = Request.getparameter ("username");
if (action!=null) {

%>

<%out.println (Request.getparameter ("password")); %>
<%
String name = Request.getparameter ("username"); Receive form Parameters
String Password = request.getparameter ("password"); Receive form Parameters
String linkman=request.getparameter ("Linkman");
String Qq=request.getparameter ("QQ");

try{
Class.forName (Dbdriver);
conn = Drivermanager.getconnection (Dburl);
String sql = "INSERT into user (name, password) VALUES (?,?)";

pstmt = conn.preparestatement (sql);
Pstmt.setstring (1,name);
Pstmt.setstring (2,password);

int sql_update = Pstmt.executeupdate ();
if (sql_update>0) {
Flag=true;
}
}catch (Exception e) {
}finally{
try{
Conn.close (); Once the connection is closed, all operations are closed
}catch (Exception e) {}
}
%>


<%

if (flag) {
Request.getsession (). SetAttribute ("Success", "registration");
%>
<jsp:forward page= "success.jsp"/>
<%
}else{//Login failed, jump to failure.jsp
Request.getsession (). SetAttribute ("Error", "registration");

%>
<jsp:forward page= "error.jsp"/>
<%
}


}else{}
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


<title>my JSP ' register.jsp ' starting page</title>


<body>

<div class= "Loginbox registbox" >

<div class= "Login-content reg-content" >
<div class= "Loginbox-title" >
</div>
<form id= "Signupform" action= "register.jsp" method= "POST" >

<div class= "Row" >
<label class= "field" for= "Email" > Username </label>
<input type= "text" class= "Input-text-user nopic input-click" name= "username" id= "email" >
</div>
<div class= "Row" >
<label class= "field" for= "password" > Password </label>
<input type= "password" value= "" class= "Input-text-password nopic input-click" name= "password" id= "password" >
</div>

<div class= "Row" >
<label class= "field" for= "Contact" > Contacts </label>
<input type= "text" value= "" class= "Input-text-user nopic input-click" name= "Linkman" id= "Contact >
</div>
<div class= "Row" >
<label class= "Field" For= "Company" > Corporate name </label>
<input type= "text" value= "" class= "Input-text-user nopic Input-click" Name= "Company" id= "Company" >
</div>
<div class= "Row" >
<label class= "Field" for= "Tel" > Company phone </label>
<input type= "text" value= "" class= "Input-text-user nopic input-click" name= "Companytel" id= "Tel" >
</div>
<div class= "Row" >
<label class= "field" for= "QQ" >QQ</label>
<input type= "text" value= "" class= "Input-text-user nopic input-click" name= "QQ" id= "QQ" >
</div>

<div class= "Row Btnarea" >
<button class= "login-btn" id= "submit" > Registration </button>
</div>
</form>
</div>
<div class= "Go-regist" >
Already have an account, please <a href= "/demo/login.jsp" class= "link" > Login </a>
</div>
</div>
</body>

Login success:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>my JSP ' success.jsp ' starting page</title>



<body>
${sessionscope.success} Success! <br>
</body>

Login failed:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>my JSP ' error.jsp ' starting page</title>


<body>
${sessionscope.error} failure <br>
</body>

JSP Link Database

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.