数据库: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>
|