數據庫:login
用戶表:users
SQL語句:
create database login; //創建login數據庫
use login;//使用login數據庫
Create table users(username varchar(20),password varchar(20),email varchar(20));//創建名為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’);
截圖:
//連接數據庫
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;
//驅動程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要訪問的數據庫名mydata
String url = "jdbc:mysql://localhost:3306/login";
//MySQL配置時的用戶名
String user = "root";
//MySQL配置時的密碼
String password = "123456";
try {
//加載驅動程序
Class.forName(driver);
//1.getConnection()方法,連接MySQL數據庫!!
con = (Connection) DriverManager.getConnection(url,user,password);
} catch(ClassNotFoundException e) {
//數據庫驅動類異常處理
System.out.println("不能找到驅動!");
e.printStackTrace();
} catch(SQLException e) {
//數據庫連接失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
}
登錄部分:
//判斷輸入框的用戶名和密碼是否和數據庫的相匹配,如果匹配,返回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;
}
注冊部分:
public boolean exsits(String username){ //查找用戶是否存在,存在為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){ //增加用戶(注冊)
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("用戶名不能為空");
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("請輸入正確的驗證碼");
form1.yanzheng.focus(); <!--將光標移動到yanzheng的輸入框 -->
return false;
}
form1.submit();
}
</script>
<form name="form1" method="post" action="Servlet">
用戶名:<input name="name" type="text" style="width:120px"><br><br>
密 碼:<input name="password" type="text" style="width:120px"><br><br>
驗證碼:<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 = "注冊">
<input type="reset" name="submit1" value="重置">
</form>
<a href="SelectServlet">查詢用戶信息</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>注冊</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>用 戶 名:<input name="username" type="text" size="20"></li>
<li>密 碼:<input name="password" type="text" size="20"></li>
<li>確認密碼:<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>
|