1024手机基地看电影,午夜福利视频导航,国产精品福利在线一区,亚洲欧美日韩另类成人,在线观看午夜日本理论片,成年超爽免费网站,国产精品成人免费,精品动作一级毛片,成人免费观看网站,97精品伊人久久大香蕉

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2107|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

JSP基于MVC模式的注冊(cè)登陸

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:344635 發(fā)表于 2018-6-4 15:00 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
數(shù)據(jù)庫(kù):login
用戶(hù)表:users
SQL語(yǔ)句:
create database login; //創(chuàng)建login數(shù)據(jù)庫(kù)
use login;//使用login數(shù)據(jù)庫(kù)
Create table users(username varchar(20),password varchar(20),email varchar(20));//創(chuàng)建名為users的表,字段為username,password,email
//插入3條記錄
Insert into users(username,password,email) values(‘123’,‘zz’,’sa’);
Insert into users(username,password,email) values(‘12313’,‘123’,’123’);
Insert into users(username,password,email) values(‘zzz’,‘we’,’wewe’);
截圖:
//連接數(shù)據(jù)庫(kù)
package com.Mysql;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class DBconnectionTest {
public static Connection getconnection(){
Connection con = null;
        //驅(qū)動(dòng)程序名
        String driver = "com.mysql.jdbc.Driver";
        //URL指向要訪問(wèn)的數(shù)據(jù)庫(kù)名mydata
        String url = "jdbc:mysql://localhost:3306/login";
        //MySQL配置時(shí)的用戶(hù)名
        String user = "root";
        //MySQL配置時(shí)的密碼
        String password = "123456";
   
    try {
        //加載驅(qū)動(dòng)程序
        Class.forName(driver);
        //1.getConnection()方法,連接MySQL數(shù)據(jù)庫(kù)!!
        con = (Connection) DriverManager.getConnection(url,user,password);
    } catch(ClassNotFoundException e) {   
        //數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)異常處理
        System.out.println("不能找到驅(qū)動(dòng)!");   
        e.printStackTrace();   
        } catch(SQLException e) {
        //數(shù)據(jù)庫(kù)連接失敗異常處理
        e.printStackTrace();  
        }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
return con;
    }
}


登錄部分:
//判斷輸入框的用戶(hù)名和密碼是否和數(shù)據(jù)庫(kù)的相匹配,如果匹配,返回ture,否則返回false
public boolean FindUser(String username,String password){            
try{
conn = DBconnectionTest.getconnection();
stmt = (Statement) conn.createStatement();
String sql = ("select * from users where username="+"'"+username+"'"+" and password="+"'"+password+"'");
rs = (ResultSet) stmt.executeQuery(sql);
while(rs.next()){
flag1 = true;
}
rs.close();
conn.close();
stmt.close();
        }
catch(Exception e ){
e.printStackTrace();
}
return flag1;
}
注冊(cè)部分:
  public boolean exsits(String username){   //查找用戶(hù)是否存在,存在為true,不存在為false
try{
conn = DBconnectionTest.getconnection();
stmt = (Statement) conn.createStatement();
String sql=("select username from users where username="+"'"+username+"'");
rs1 = (ResultSet) stmt.executeQuery(sql);
while(rs1.next()){
flag2=true;
}
}catch(Exception e){e.printStackTrace();}
  
return flag2;
  }
  
          public int AddUser(JavaBean person){    //增加用戶(hù)(注冊(cè))
  
          try{
  
          conn = DBconnectionTest.getconnection();
          stmt = (Statement) conn.createStatement();
  
          String username=person.getUsername();
          String password=person.getPassword();
          String confirmpassword=person.getConfirmpassword();
          String email=person.getEmail();
          if(exsits(username)){
          System.out.println("user exist");
          }
          else if(!(password.equals(confirmpassword))) {
          System.out.println("兩次密碼必須一致");
          }
          else{
          String sql="insert into users(username,password,email) values('"+username+"','"+password+"','"+email+"')";
                    int count=stmt.executeUpdate(sql);            
          }
          rs1.close();
          conn.close();
          stmt.close();
          }catch(Exception e){
          e.printStackTrace();
          }
  
return 0;
  
          }
  Login.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄界面</title>
</head>
<body>
<script type="text/javascript">
function mycheck(){
if(form1.name.value==""){
window.alert("用戶(hù)名不能為空");
form1.name.focus();
return false;
}
if(form1.password.value==""){
window.alert("密碼不能為空");
form1.password.focus();
return false;
}
if(form1.yanzheng.value!=form1.vcode.value){
window.alert("請(qǐng)輸入正確的驗(yàn)證碼");
form1.yanzheng.focus();  <!--將光標(biāo)移動(dòng)到y(tǒng)anzheng的輸入框 -->
return false;
}
form1.submit();
}
</script>
<form name="form1" method="post" action="Servlet">
用戶(hù)名:<input name="name" type="text" style="width:120px"><br><br>
密    碼:<input name="password" type="text" style="width:120px"><br><br>
驗(yàn)證碼:<input name="yanzheng" type="text" size="8">
<%
int intmethod=(int) ((((Math.random())*11))-1);
int intmethod2=(int) ((((Math.random())*11))-1);
int intmethod3=(int) ((((Math.random())*11))-1);
int intmethod4=(int) ((((Math.random())*11))-1);
String intsum=intmethod+""+intmethod2+intmethod3+intmethod4;
%>
<input type="hidden" name="vcode" value="<%=intsum %>">
<img  src="num/<%=intmethod%>.gif">
<img  src="num/<%=intmethod2%>.gif">
<img  src="num/<%=intmethod3%>.gif">
<img  src="num/<%=intmethod4%>.gif">
<br>
<br><br>
<input type="submit" name="submit" value="確定">
<input type = "button" value = "注冊(cè)">
<input type="reset" name="submit1" value="重置">
</form>
<a href="SelectServlet">查詢(xún)用戶(hù)信息</a>
</body>
</html>

Register.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注冊(cè)</title>
<style type="text/css">
ul{
list-style:none;
margin:0px;
padding:5px;
}
li{
padding:5px;
}
</style>
</head>
<body>
<center>
<h1>user register</h1>
</center>
<form action="RegisterProperty.jsp" method="post"  >
<ul>
<li>用  戶(hù)  名:<input name="username" type="text" size="20"></li>
<li>密        碼:<input name="password" type="text" size="20"></li>
<li>確認(rèn)密碼:<input name="confirmpassword" type="text" size="20"></li>
<li>郵        箱:<input name="email" type="text" size="20"></li>
</ul>
    <input type="submit" name="submit" value="確定">
    <input  type="reset" name="submit1" value="重置">
</form>
</body>
</html>

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表