jsp連結資料庫

來源:互聯網
上載者:User

標籤:

資料庫表代碼:

/*
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(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) 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);

 

註冊頁面:

<%@ 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"; //登入的使用者名稱
public static final String userPasswd = "taizhen"; //登入mysql密碼
public static final String dbName = "test"; //資料庫名
public static final String tableName="user"; //表名
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 ; // 表示登陸成功或失敗的標記
%>

<%

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

}else{
%>

<%out.println(action); %>
<%
String name = request.getParameter("username") ; // 接收表單參數
String password = request.getParameter("password") ; // 接收表單參數
try{
Class.forName(DBDRIVER) ;
conn = 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()){
// 如果有內容,則此處執行,表示查詢出來,合法使用者
flag = true ;
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 串連一關閉,所有的操作都將關閉
}catch(Exception e){}
}
%>
<%out.println(flag); %>
<%out.println(action); %>
<%

if(flag){
request.getSession().setAttribute("success", "登入");
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陸失敗,跳轉到failure.jsp
request.getSession().setAttribute("error", "登入");

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

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘login.jsp‘ starting page</title>

</head>

<body>
<form action="login.jsp" method="post">
<input type="hidden" name="action" />
使用者名稱:
<input type="text" name="username" value="username"/>
<br>
密碼:
<input type="password" name="password" value="password"/>
<br>
<input type="submit" value="login" />
</form>

</body>
</html>

 

jsp連結資料庫代碼:

<%@ 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"; //登入的使用者名稱
public static final String userPasswd = "taizhen"; //登入mysql密碼
public static final String dbName = "test"; //資料庫名
public static final String tableName="user"; //表名
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 ; // 表示登陸成功或失敗的標記
%>

<%

String action = request.getParameter("username");
if(action!=null){

%>

<%out.println(request.getParameter("password")); %>
<%
String name = request.getParameter("username") ; // 接收表單參數
String password = request.getParameter("password") ; // 接收表單參數
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() ; // 串連一關閉,所有的操作都將關閉
}catch(Exception e){}
}
%>


<%

if(flag){
request.getSession().setAttribute("success", "註冊");
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陸失敗,跳轉到failure.jsp
request.getSession().setAttribute("error", "註冊");

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


}else{}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP ‘register.jsp‘ starting page</title>

</head>

<body>

<div class="loginbox registbox">

<div class="login-content reg-content">
<div class="loginbox-title">
<h3>註冊</h3>
</div>
<form id="signupForm" action="register.jsp" method="post">

<div class="row">
<label class="field" for="email">使用者名稱</label>
<input type="text" class="input-text-user noPic input-click" name="username" id="email">
</div>
<div class="row">
<label class="field" for="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">連絡人</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">公司名</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">公司電話</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">註冊</button>
</div>
</form>
</div>
<div class="go-regist">
已有帳號,請<a href="/Demo/login.jsp" class="link">登入</a>
</div>
</div>
</body>
</html>

 

登入成功:

<%@ 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">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘success.jsp‘ starting page</title>


</head>

<body>
${sessionScope.success }成功! <br>
</body>
</html>

 

登入失敗:

<%@ 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">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘error.jsp‘ starting page</title>

</head>

<body>
${sessionScope.error }失敗 <br>
</body>
</html>

jsp連結資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.