master
parent
281ab6a65c
commit
0c3c7ac7ce
@ -0,0 +1,112 @@
|
||||
package org.use.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.use.entity.Use;
|
||||
|
||||
public class UseDao {
|
||||
private final String URL="jdbc:mysql://localhost:3306/test";
|
||||
private final String USERNAME ="root" ;
|
||||
private final String PASSWORD ="123456" ;
|
||||
|
||||
public boolean isExist(String username) {
|
||||
return queryUseByusername(username)==null? false:true ;
|
||||
}
|
||||
|
||||
public boolean addUse(Use use) {
|
||||
Connection connection = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs= null;
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
connection = DriverManager.getConnection(URL,USERNAME,PASSWORD );
|
||||
String sql = "inset into use(username,password,phonenum,email) vaule(?,?,?,?) " ;
|
||||
pstmt =connection.prepareStatement( sql);
|
||||
pstmt.setString(1, use.getUsername() );
|
||||
pstmt.setString(2, use.getPassword() );
|
||||
pstmt.setInt(3, use.getPhonenum() );
|
||||
pstmt.setString(4, use.getEmail() );
|
||||
int count = pstmt.executeUpdate() ;
|
||||
if(count>0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if(rs!=null)rs.close();
|
||||
if(pstmt!=null)pstmt.close();
|
||||
if(connection!=null)connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//查询
|
||||
public Use queryUseByusername(String username) {
|
||||
Use use= null;
|
||||
Connection connection = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs= null;
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
connection = DriverManager.getConnection(URL,USERNAME,PASSWORD );
|
||||
String sql = "select * from use where username = ? ";
|
||||
pstmt =connection.prepareStatement( sql);
|
||||
pstmt.setString(1, username);
|
||||
rs = pstmt.executeQuery() ;
|
||||
if(rs.next()) {
|
||||
String uname= rs.getString("username");
|
||||
String password= rs.getString("password");
|
||||
int phonenum= rs.getInt("phonenum");
|
||||
String email= rs.getString("email");
|
||||
use = new Use(uname,password,phonenum,email);
|
||||
|
||||
}
|
||||
return use;
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if(rs!=null)rs.close();
|
||||
if(pstmt!=null)pstmt.close();
|
||||
if(connection!=null)connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package org.use.entity;
|
||||
|
||||
public class Use {
|
||||
private String username;
|
||||
private String password;
|
||||
private int phonenum;
|
||||
private String email;
|
||||
|
||||
public Use() {
|
||||
}
|
||||
|
||||
public Use(String username, String password, int phonenum, String email) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.phonenum = phonenum;
|
||||
this.email = email;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public int getPhonenum() {
|
||||
return phonenum;
|
||||
}
|
||||
public void setPhonenum(int phonenum) {
|
||||
this.phonenum = phonenum;
|
||||
}
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package org.use.service;
|
||||
|
||||
import org.use.dao.UseDao;
|
||||
import org.use.entity.Use;
|
||||
|
||||
public class UseService {
|
||||
UseDao useDao = new UseDao();
|
||||
|
||||
public boolean regUse(Use use) {
|
||||
if(!useDao.isExist(use.getUsername())) {
|
||||
useDao.addUse(use);
|
||||
return true;
|
||||
}else {
|
||||
System.out.println("此人已经存在!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package org.use.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.use.entity.Use;
|
||||
import org.use.service.UseService;
|
||||
|
||||
/**
|
||||
* Servlet implementation class RegUseServlet
|
||||
*/
|
||||
public class RegUseServlet extends HttpServlet {
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String username= request.getParameter("username");
|
||||
String password= request.getParameter("password");
|
||||
int phonenum= Integer.parseInt(request.getParameter("phonenum"));
|
||||
String email= request.getParameter("email");
|
||||
Use use =new Use(username,password,phonenum,email);
|
||||
|
||||
UseService useService = new UseService();
|
||||
boolean result = useService.regUse(use);
|
||||
PrintWriter out = response.getWriter() ;
|
||||
if(result) {
|
||||
out.println("注册成功") ;
|
||||
}else {
|
||||
out.println("注册失败");
|
||||
}
|
||||
}
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
a:link {color:#87CEFA;}
|
||||
#section{
|
||||
width: 450px;
|
||||
height: 600px;
|
||||
padding: 10px;
|
||||
float: left;
|
||||
background-color: white;
|
||||
transform: translateY(50px);
|
||||
position: relative;
|
||||
left:540px;
|
||||
}
|
||||
#section1{
|
||||
width:240px;
|
||||
height: 60px;
|
||||
padding: 10px;
|
||||
float:right
|
||||
|
||||
}
|
||||
#name{
|
||||
display: inline-block;
|
||||
margin-left: 20px;
|
||||
|
||||
}
|
||||
#name1{
|
||||
display: inline-block;
|
||||
margin-left: 20px;
|
||||
|
||||
}
|
||||
#button{
|
||||
width:350px;
|
||||
height: 50px;
|
||||
background: #DC143C;
|
||||
display: inline-block;
|
||||
transform: translateY(450px);
|
||||
position: relative;
|
||||
left:-223px;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body style="background-image: url( 图片/QQ截图20190110093949.png);background-repeat:no-repeat">
|
||||
<div id="section">
|
||||
|
||||
</div>
|
||||
<div id="section1" style="background:url(图片/QQ截图20190110095831.png);transform: translateY(80px);position: relative;right:610px;"></div>
|
||||
<div id="name">
|
||||
<input style="transform: translateY(200px);position: relative;left: 115px; width: 350px;height:40px;" type="text" id="username" name="username" placeholder=" 请输入用户名" />
|
||||
</div>
|
||||
<form action="RegUseServlet" method="post">
|
||||
<div id="name1">
|
||||
<input style="transform: translateY(268px);position: relative;left: -265px; width: 350px;height:40px;" type="text" id="password" name="password" placeholder=" 请输入密码" />
|
||||
</div>
|
||||
<input style="transform: translateY(288px);position: relative;left: 134px; width: 350px;height:40px;" type="text" id="repassword" name="repassword" placeholder=" 确认密码" />
|
||||
<input style="transform: translateY(356px);position: relative;left: -225px; width: 350px;height:40px;" type="text" id="phonenum" name="phonenum" placeholder=" 请输入手机号" />
|
||||
<input style="transform: translateY(380px);position: relative;left: 134px; width: 350px;height:40px;" type="text" id="email" name="email" placeholder=" 请输入邮箱" />
|
||||
<input id="button" type="submit" value="注册">
|
||||
</form>
|
||||
<a href="登录.html" style="text-decoration: none;position: relative;left:-350px;bottom: -520px">已有账号,立即登录</a>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in new issue