Java小项目之:相亲网站,来一起非诚勿扰!
今天给大家分享的java小项目是一个做非常有意思的网站,相亲网站。有前台系统和后台管理系统。
部分代码展示:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.DBUtil;
import entity.User;
public class UserDAO {
public List list() throws Exception{
List users =
new ArrayList();
Connection conn = DBUtil.getConnection();
Statement stat = conn.createStatement();
ResultSet rst = stat.executeQuery(“select * from friend_user2”);
while(rst.next()){
User user = new User(rst.getString(“username”),
rst.getString(“password”),rst.getString(“name”),
rst.getInt(“age”),rst.getString(“gender”),
rst.getString(“phone”));
user.setId(rst.getLong(“id”));
users.add(user);
}
DBUtil.close(conn);
return users;
}
public long save(User user) throws Exception{
long id = -1;
Connection conn = DBUtil.getConnection();
PreparedStatement prep =
conn.prepareStatement(
"insert into friend_user2(username,password,name,age,gender,phone) " +
“values(?,?,?,?,?,?)”,java.sql.Statement.RETURN_GENERATED_KEYS);
prep.setString(1, user.getUsername());
prep.setString(2, user.getPwd());
prep.setString(3, user.getName());
prep.setInt(4,user.getAge());
prep.setString(5, user.getGender());
prep.setString(6, user.getPhone());
prep.executeUpdate();
ResultSet rst = prep.getGeneratedKeys();
if(rst.next()){
id = rst.getLong(1);
}
DBUtil.close(conn);
return id;
}
public User findByUsername(String username) throws Exception{
User user = null;
Connection conn = DBUtil.getConnection();
PreparedStatement prep =
conn.prepareStatement(“select * from friend_user2 where username=?”);
prep.setString(1, username);
ResultSet rst = prep.executeQuery();
if(rst.next()){
user = new User(rst.getString(“username”),
rst.getString(“password”),rst.getString(“name”),
rst.getInt(“age”),rst.getString(“gender”),
rst.getString(“phone”));
user.setId(rst.getLong(“id”));
}
DBUtil.close(conn);
return user;
}
public User findById(Long id) throws Exception{
User user = null;
Connection conn = DBUtil.getConnection();
PreparedStatement prep =
conn.prepareStatement(
“select * from friend_user2 where id =?”);
prep.setLong(1, id);
ResultSet rst = prep.executeQuery();
if(rst.next()){
user = new User(rst.getString(“username”),
rst.getString(“password”),rst.getString(“name”),
rst.getInt(“age”),rst.getString(“gender”),
rst.getString(“phone”));
user.setId(id);
}
DBUtil.close(conn);
return user;
}
}
喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!
祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!