2.UserFriendsDAO类[code=java]package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Vector;
import dbutil.ConnectionManager;
public class UserFriendsDAO {
private static ResultSet rs = null;
private static PreparedStatement ps = null;
private static ResultSet rs1 = null;
private static PreparedStatement ps1 = null;
private static Connection conn = null;
private static String userName;
private static String friendName;
public UserFriendsDAO(){}
public static int addFriend(String userName,String friendName,String state){
UserFriendsDAO.userName = userName;
UserFriendsDAO.friendName = friendName;
String friendState=null;
String sql2 = "SELECT STATE FROM CHAT_USERINFO WHERE USERNAME =?";
int num = 0;
if(validate()) return num;
if(find())return num;
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql2);
ps.setString(1, friendName);
rs = ps.executeQuery();
if(rs.next()){
friendState = rs.getString(1);
ps.close();
}
String sql = "INSERT INTO CHAT_USERFRIENDS VALUE ('"+userName+"','"+friendName+"',default,'"+friendState+"',default)";
String sql1 = "INSERT INTO CHAT_USERFRIENDS VALUE ('"+friendName+"','"+userName+"',default,'"+state+"',default)";
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
ps.close();
ps = conn.prepareStatement(sql1);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(rs);
ConnectionManager.closeDatabseObj(ps);
ConnectionManager.closeDatabseObj(conn);
}
return num;
}
public static boolean validate(){
String sql = "SELECT * FROM CHAT_USERINFO WHERE USERNAME =?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1,friendName);
rs = ps.executeQuery();
if(rs.next()) return false;
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(rs);
ConnectionManager.closeDatabseObj(ps);
ConnectionManager.closeDatabseObj(conn);
}
return true;
}
public static boolean find(){
String sql = "SELECT * FROM CHAT_USERFRIENDS WHERE USERNAME ='"+userName+"' AND FRIENDNAME = '"+friendName+"'";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next())return true;
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(rs);
ConnectionManager.closeDatabseObj(ps);
ConnectionManager.closeDatabseObj(conn);
}
return false;
}
public static void setLogOut(String userName){
String sql = "UPDATE CHAT_USERFRIENDS SET SETSTATE=? WHERE USERNAME = ?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, "离线");
ps.setString(2, userName);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(ps);
ConnectionManager.closeDatabseObj(conn);
}
}
public static Vector<HashMap<String,String>> getFriend(String username){
Vector allFriend= new Vector<HashMap<String,String>>();
String sql ="SELECT * FROM CHAT_USERfRIENDS WHERE USERNAME =?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1,username);
rs = ps.executeQuery();
while(rs.next()){
HashMap<String,String> h = getFriendInfo(rs.getString(2),false);
h.put("setVisible",rs.getString(3));
h.put("state",rs.getString(4));
h.put("stopful", rs.getString(5));
String friendName = rs.getString(2);
h.put("friendSetVisible",getVisible(friendName,username));
allFriend.add(h);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(rs);
ConnectionManager.closeDatabseObj(ps);
ConnectionManager.closeDatabseObj(conn);
}
return allFriend;
}
public static String getVisible(String friendName,String userName){
String temp="否";
String sql ="SELECT * FROM CHAT_USERFRIENDS WHERE USERNAME =? AND FRIENDNAME=?";
conn = ConnectionManager.getConnection();
try {
ps1 = conn.prepareStatement(sql);
ps1.setString(1, friendName);
ps1.setString(2, userName);
rs1 = ps1.executeQuery();
if(rs1.next()){
return rs1.getString(3);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(rs1);
ConnectionManager.closeDatabseObj(ps1);
ConnectionManager.closeDatabseObj(conn);
}
return temp;
}
public static HashMap getFriendInfo(String name,boolean bool){
HashMap friend = new HashMap<String,String>();
String sql = "SELECT * FROM CHAT_USERINFO WHERE USERNAME=?";
try {
if(bool)conn = ConnectionManager.getConnection();
ps1 = conn.prepareStatement(sql);
ps1.setString(1,name);
rs1 = ps1.executeQuery();
if(rs1.next()){
friend.put("friendName", rs1.getString(1));
friend.put("nickName", rs1.getString(3));
friend.put("friendstate", rs1.getString(11));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionManager.closeDatabseObj(rs1);
ConnectionManager.closeDatabseObj(ps1);
}
return friend;
}
public static int setVisible(String userName,String friendName){
int num=0;
String sql = "UPDATE CHAT_USERFRIENDS SET SETVISIBLE ='是' WHERE USERNAME=? AND FRIENDNAME=?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2,friendName);
num = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
public static int setDisvisible(String userName,String friendName){
int num=0;
String sql = "UPDATE CHAT_USERFRIENDS SET SETVISIBLE ='否' WHERE USERNAME=? AND FRIENDNAME=?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2,friendName);
num = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
public static int setStopful(String userName,String friendName){
int num=0;
String sql = "UPDATE CHAT_USERFRIENDS SET STOPFUL ='是' WHERE USERNAME=? AND FRIENDNAME=?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2,friendName);
num = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
public static int setStopless(String userName,String friendName){
int num=0;
String sql = "UPDATE CHAT_USERFRIENDS SET STOPFUL ='否' WHERE USERNAME=? AND FRIENDNAME=?";
conn = ConnectionManager.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2,friendName);
num = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}
}[/code] |
|