Java小項目之:相親網(wǎng)站,來一起非誠勿擾!
今天給大家分享的java小項目是一個做非常有意思的網(wǎng)站,相親網(wǎng)站。有前臺系統(tǒng)和后臺管理系統(tǒng)。
部分代碼展示:
package dao;
創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的夏河網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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
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;
}
}
喜歡這樣文章的可以關(guān)注我,我會持續(xù)更新,你們的關(guān)注是我更新的動力!需要更多java學(xué)習(xí)資料的也可以私信我!
祝關(guān)注我的人都:身體健康,財源廣進,福如東海,壽比南山,早生貴子,從不掉發(fā)!