今天是我们计算机专业实训的最后一天了,也是暑假开始的一天,挺高兴的,接下来就展示我们的学习成果
1.首先应该在数据库中建立一个与项目相匹配的表
2.项目链接数据库
package cn.edu.aust.game.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 作者:陈雪晨
* 时间:2021/7/16 8:45
* 描述:工具类:封装jdbc的驱动加载和连接和关闭
*/
public class JdbcUtil {
//成员变量
private static String className;
private static String url;
private static String user;
private static String password;
//加载驱动
static {
try {
//读取属性文件工具类
Properties prop=new Properties();
//加载属性文件数据到内存中
prop.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
//通过key值获取value值
className=prop.getProperty("jdbc.className");
url=prop.getProperty("jdbc.url");
user=prop.getProperty("jdbc.user");
password=prop.getProperty("jdbc.password");
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//创建连接
public static Connection getConn() throws SQLException {
Connection conn=DriverManager.getConnection(url,user,password);
return conn;
}
//关闭连接
public static void release(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//测试
public static void main(String[] args) throws SQLException {
Connection conn=JdbcUtil.getConn();
System.out.println(conn);
}
}
别忘了在文档中输入数据库的地址,用户名,密码等
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/game?useSSL=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=root
3.创建项目信息与数据库信息的匹配
package cn.edu.aust.game.entity;
import java.sql.Timestamp;
import java.util.Objects;
/**
* 作者:陈雪晨
* 时间:2021/7/18 19:40
* 描述:
*/
public class GameUser {
private Integer id;
private String username;
private String account;
private String password;
private String nickname;
private String email;
private String phone;
private String IDcard;
private Timestamp created_time;
public GameUser(){}
public GameUser(Integer id, String username, String account, String password, String nickname, String email, String phone, String IDcard, Timestamp created_time) {
this.id = id;
this.username = username;
this.account = account;
this.password = password;
this.nickname = nickname;
this.email = email;
this.phone = phone;
this.IDcard = IDcard;
this.created_time = created_time;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getIDcard() {
return IDcard;
}
public void setIDcard(String IDcard) {
this.IDcard = IDcard;
}
public Timestamp getCreated_time(Timestamp created_time) {
return this.created_time;
}
public void setCreated_time(Timestamp created_time) {
this.created_time = created_time;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GameUser)) return false;
GameUser gameUser = (GameUser) o;
return id.equals(gameUser.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "GameUser{" +
"id=" + id +
", username='" + username + '\'' +
", account='" + account + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", IDcard='" + IDcard + '\'' +
", created_time=" + created_time +
'}';
}
}
4.分别创建注册信息、登录和修改信息与网页的连接
package cn.edu.aust.game.web;
import cn.edu.aust.game.dao.GameDao;
import cn.edu.aust.game.entity.GameUser;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 作者:陈雪晨
* 时间:2021/7/20 14:04
* 描述:
*/
@WebServlet("/registe.do")
public class RegisteServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
req.setCharacterEncoding("UTF-8");
String username=req.getParameter("uname");
String password=req.getParameter("upsd");
String nickname=req.getParameter("unickname");
String email=req.getParameter("uemail");
String phone=req.getParameter("uphone");
String IDcard=req.getParameter("uIDcard");
GameUser user=new GameUser();
GameDao dao=new GameDao();
user.setUsername(username);
user.setPassword(password);
user.setNickname(nickname);
user.setEmail(email);
user.setPhone(phone);
user.setIDcard(IDcard);
System.out.println(user);
try {
dao.Register(user);
//响应处理结果
resp.sendRedirect("web/login.jsp");
} catch (IOException e) {
e.printStackTrace();
//响应处理结果
resp.sendRedirect("web/500.html");
}
}
}
package cn.edu.aust.game.web;
import cn.edu.aust.game.dao.GameDao;
import cn.edu.aust.game.entity.GameUser;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* 作者:陈雪晨
* 时间:2021/7/21 8:14
* 描述:
*/
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
String phone=req.getParameter("phone");
String password=req.getParameter("psd");
GameDao dao=new GameDao();
String psd = null;
try {
psd = dao.Search_Password(phone);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (password.equals(psd)) {
//响应处理结果
resp.sendRedirect("web/index.jsp");
}else{
resp.sendRedirect("web/de_login.jsp");
}
} catch (IOException e) {
e.printStackTrace();
//响应处理结果
}
}
}
package cn.edu.aust.game.web;
import cn.edu.aust.game.entity.GameUser;
import cn.edu.aust.game.dao.GameDao;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* 作者:陈二胖
* 时间:2021/7/21 14:47
* 目的:TODO
*/
@WebServlet("/change.do")
public class ChangeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取浏览器用户输入的数据
String phone = req.getParameter("phone");
String password = req.getParameter("password");
System.out.println(phone+","+password);
GameUser user = new GameUser();
user.setPhone(phone);
user.setPassword(password);
GameDao dao = new GameDao();
dao.updatePassword(phone,password);
//响应结果处理
resp.sendRedirect("web/login.jsp");
}
}
HTML的编写就不一一展示了,以后有时间我会把项目成果展示出来