温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java小项目之:相亲网站,来一起非诚勿扰!

发布时间:2020-04-23 07:15:45 来源:网络 阅读:269 作者:qq5dda6ecead5a8 栏目:编程语言

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<User> list() throws Exception{
List<User> users =
new ArrayList<User>();
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学习资料的也可以私信我!
祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!
Java小项目之:相亲网站,来一起非诚勿扰!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI