📄 dbmanager.java
字号:
package qianqian.pooledb;
import qianqian.p2pchat.constant.Const;
import qianqian.p2pchat.control.Peer;
import qianqian.p2pchat.tools.Tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBManager {
private DBManager() {
}
// 加载数据库驱动
public static void LoadDriver() throws ClassNotFoundException {
Class.forName(Const.driver);
}
// 获取数据库连接
public static Connection getConnection() throws SQLException {
Connection con = null;
con = DriverManager.getConnection(Const.URL, Const.DBA, Const.PWD);
return con;
}
// 查询数据库 登录
public static Peer checkLogin(Connection con, String id, String pwd,
String ip) throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.CheckLogin);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
ResultSet rst = pstmt.executeQuery();// 查询用户
if (rst.next()) {
Peer peer = new Peer(rst.getString("UserId"), rst
.getString("NickName"), rst.getString("Gender").charAt(0),
rst.getString("Email"), rst.getString("UserIp"));
pstmt = con.prepareStatement(Const.SetIp);
pstmt.setString(1, ip);
pstmt.setString(2, id);
pstmt.executeUpdate();// 更新IP
pstmt.close();
return peer;
}
pstmt.close();
return null;
}
// 查询密码
public static boolean findPasswd(Connection con, String id, String pwd,
String email) throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.CheckEmail);
pstmt.setString(1, id);
pstmt.setString(2, email);
ResultSet rst = pstmt.executeQuery();// 查询用户
if (rst.next()) {
pstmt = con.prepareStatement(Const.SetPasswd);
pstmt.setString(1, Tools.MD5(pwd));
pstmt.setString(2, id);
pstmt.executeUpdate();// 更新密码
pstmt.close();
return true;
}
pstmt.close();
return false;
}
// 用户离线
public static void offLine(Connection con, String id, String ip)
throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.ResetIp);
pstmt.setString(1, id);
pstmt.setString(2, ip);
pstmt.executeUpdate();// 清除IP
pstmt.close();
}
// 查询数据库 获取好友
public static java.util.LinkedList<Peer> getFriends(Connection con,
String id) throws SQLException {
java.util.LinkedList<Peer> friends = new java.util.LinkedList<Peer>();
PreparedStatement pstmt = con.prepareStatement(Const.GetFrnd);
pstmt.setString(1, id);
ResultSet rst = pstmt.executeQuery();// 通过id查询好友
while (rst.next()) {// 获取好友id,昵称,性别
Peer peer = new Peer(rst.getString("UserId"), rst
.getString("NickName"), rst.getString("Gender").charAt(0),
rst.getString("Email"), rst.getString("UserIp"));
friends.add(peer);
}
pstmt.close();
return friends;
}
// 注册新用户
public static String getNewUserId(Connection con, String pwd, String nick,
String email, char sex) throws SQLException {
return null;
}
// 添加新用戶
public static String addNewUser(Connection con, String pwd, String nick,
String email, char sex) throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.NewUser);
String newId = null;
pstmt.setString(1, pwd);
pstmt.setString(2, nick);
pstmt.setString(3, sex + "");
pstmt.setString(4, email);
pstmt.executeUpdate();// 添加用户
ResultSet rst = pstmt.getGeneratedKeys();
if(rst.next()) {
newId = rst.getString(1);
}
pstmt.close();
return newId;
}
// 查找用户
public static java.util.LinkedList<Peer> searchUser(Connection con,
String id, String nick) throws SQLException {
java.util.LinkedList<Peer> friends = new java.util.LinkedList<Peer>();
PreparedStatement pstmt = con.prepareStatement(Const.SrchUser);
if (!id.isEmpty()) {// 根据id查找
pstmt.setString(1, id);
pstmt.setString(2, "%");
} else {// 根据昵称查找
pstmt.setString(1, "%");
pstmt.setString(2, "%" + nick + "%");
}
ResultSet rst = pstmt.executeQuery();
while (rst.next()) {
Peer peer = new Peer(rst.getString("UserId"), rst
.getString("NickName"), rst.getString("Gender").charAt(0),
rst.getString("Email"), rst.getString("UserIp"));
friends.add(peer);
}
pstmt.close();
return friends;
}
// 确认好友关系存在
public static boolean checkFriend(Connection con, String userId,
String frndId) throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.CheckFrnd);
pstmt.setString(1, userId);
pstmt.setString(2, frndId);
ResultSet rst = pstmt.executeQuery();
if (rst.next()) {
pstmt.close();
return true;
}
pstmt.close();
return false;
}
// 添加好友
public static void addNewFrnd(Connection con, String id1, String id2)
throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.NewFrnd);
pstmt.setString(1, id1);
pstmt.setString(2, id2);
pstmt.executeUpdate();// 添加好友关系
pstmt.close();
}
// 修改信息
public static boolean modifyUser(Connection con, String id, String pwd,
String newNick, String newPwd, char newGender, String email)
throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.MdfyUser);
pstmt.setString(1, newNick);
pstmt.setString(2, newPwd);
pstmt.setString(3, newGender + "");
pstmt.setString(4, email);
pstmt.setString(5, id);
pstmt.setString(6, pwd);
int rst = pstmt.executeUpdate();// 更新个人信息
pstmt.close();
return (rst > 0);
}
// 删除好友
public static void delFriend(Connection con, String userId, String frndId)
throws SQLException {
PreparedStatement pstmt = con.prepareStatement(Const.DelFrnd);
pstmt.setString(1, userId);
pstmt.setString(2, frndId);
pstmt.executeUpdate();
pstmt.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -