溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

jsp鏈接數據庫源代碼

發(fā)布時間:2020-07-30 18:36:27 來源:網絡 閱讀:453 作者:lCarrieLiul 欄目:數據庫

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

  </head>

  

  <body>

    This is my JSP page. <br>

  </body>

</html>



注冊頁面代碼:


<%@ 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()){

   // 如果有內容,則此處執(zhí)行,表示查詢出來,合法用戶

       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>



鏈接數據庫代碼:


<%@ 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">

<h4>注冊</h4>

</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">聯(lián)系人</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 'error.jsp' starting page</title>

    


  </head>

  

  <body>

    ${sessionScope.error }失敗 <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 'success.jsp' starting page</title>

    

  </head>

  

  <body>

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

  </body>

</html>


數據庫表:

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);


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI