public class mail {
private Integer id;
private String Sender;
private String addressee;
private String title;
private String content;
private String time;
private Integer state;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSender() {
return Sender;
}
public void setSender(String sender) {
Sender = sender;
}
public String getAddressee() {
return addressee;
}
public void setAddressee(String addressee) {
this.addressee = addressee;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
@Override
public String toString() {
return "mail{" +
"id=" + id +
", Sender='" + Sender + '\'' +
", addressee='" + addressee + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
", time='" + time + '\'' +
", state=" + state +
'}';
}
}
public class MailUsers {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "MailUsers{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
import java.sql.*;
public class mail_BaseDao {
protected Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mailbox", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class mail_UserDao extends mail_BaseDao{
//--------------------用户登录---------------------
public boolean login(String name, String pwd) {
boolean f = false;
Connection conn = getConnection();
String sql = "select * from tb_user where username=? and password=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
rs = ps.executeQuery();
if (rs.next())
f = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, ps, rs);
}
return f;
}
//--------------------用户注册--------------------
public void reg(String username, String password) {
Connection conn = getConnection();
PreparedStatement ps = null;
try {
String sql = "insert into tb_user(username,password) values(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, ps, null);
}
}
}
import com.own.mywebdemo.Week5_Login_connecor.BaseDao;
import org.jetbrains.annotations.NotNull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MailDao extends mail_BaseDao {
//--------------------查询所有邮件--------------------
public List<mail> getMailByReceiver(String name){
List<mail> list=new ArrayList<mail>();
Connection conn=getConnection();
String sql="select * from tb_mail where addressee=?";
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
rs=ps.executeQuery();
while(rs.next()){
mail m=new mail();
m.setId(rs.getInt(1));
m.setSender(rs.getString(2));
m.setAddressee(rs.getString(3));
m.setTitle(rs.getString(4));
m.setContent(rs.getString(5));
m.setTime(rs.getString(6));
m.setState(rs.getInt(7));
list.add(m);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, rs);
}
return list;
}
// --------------------插入邮件--------------------
public void addMail(mail m) {
Connection conn = getConnection();
String sql = "insert into tb_mail(Sender,addressee,title,content,time,state) values(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, m.getSender());
ps.setString(2, m.getAddressee());
ps.setString(3, m.getTitle());
ps.setString(4, m.getContent());
ps.setDate(5, new java.sql.Date(new Date().getTime()));// 系统当前时间
ps.setInt(6, 0);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(conn, ps, null);
}
}
// --------------------删除邮件--------------------
public void deleteMail(int id) {
Connection conn = getConnection();
String sql = "delete from tb_mail where id=?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, null);
}
}
//--------------------更新邮件状态--------------------
public void updataMail(int id){
Connection conn = getConnection();
String sql = "update tb_mail set state=1 where id=?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, null);
}
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<script type="text/javascript">
function check() {
if (login.account.value==""){
alert("请输入账号!");
return;
}else if(login.psw.value=="") {
alert("请输入密码!");
return;
}
login.submit();
}
</script>
<form action="checkLog.jsp" method="post" name="login">
账号:<input type="text" name="account"><br>
密码:<input type="password" name="psw"><br>
<input type="button" value="登录" onclick="check()">
</form><br>
<form action="Register.jsp" method="post">
<input type="submit" value="立即注册账户">
</form>
</body>
</html>