import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**********************Server中与数据库通信的方法*********************************************/
//public Server()
//public void DBUpdate()
//public void DBInsert(UserInfo userinfo)
//public void DBInsert(PublicMessage pubmsg)
/**********************Server中与数据库通信的方法*********************************************/
public class Server implements Runnable{
//
private ServerLoginDiag SLoginDiag;
private String SerAdminPassword; //保存服务器端用户密码
private String AdminName; //保存服务器端用户名
private Hashtable AllUsers; //所有用户 UserInfo
private Hashtable CSThreads; //在线用户
private HashSet AllUserskeys; //登陆时会用到
private HashSet CSThreadskeys; //交给SwitchThread使用
private SwitchThread switchthread;
private File ServerInfo;
private DataOutputStream ToFile;
private UserInfo yourself; //临时存放当前登录者信息
private ServerThread serverthread; //临时存放当前登录者信息
private Timer UPDateTimer;
private static int CurConNum=0;
//队列
private Vector PublicMessagelist;
private LinkedList Messagelist;
private ServerSocket welcome;
private Socket Csocket;
//流
private ObjectOutputStream StreamOut;
private ObjectInputStream StreamIn;
//服务器端主界面
private ServerFrame serverframe;
//数据库相关
private Statement UserStatement,MsgStatement,PubMsgStatement,ServerLogStatement;
private Connection conDataBase;
private ResultSet UserDatas,MsgDatas,PubMsgDatas,ServerLogDatas;
public Server(){
AllUsers=new Hashtable(); //所有用户
CSThreads=new Hashtable(); //在线用户
AllUserskeys=new HashSet(AllUsers.keySet()); //登陆时会用到
//AllUserskeys=new HashSet(AllUsers.entrySet());
CSThreadskeys=new HashSet(CSThreads.keySet()); //交给SwitchThread使用
//CSThreadskeys=new HashSet(CSThreads.entrySet());
//队列
PublicMessagelist=new Vector();
Messagelist=new LinkedList();
try{
//加载数据库驱动程序
/**************************MYSQL**********************************************************/
// Class.forName("org.gjt.mm.mysql.Driver");//.newInstance();//throws ClassNotFoundException
// Class.forName("com.mysql.jdbc.Driver");//.newInstance();
// System.out.println("驱动加载成功");
// /*public static Connection getConnection(String url) throws SQLException*/
// String url="jdbc:mysql://localhost:3306/Com?user=root&password=root & useUnicode=true & characterEncoding=gb2312/";
// Com数据库的名称
// user=root 访问数据库的用户名
// password=root 访问数据库的密码
// gb2312 使用的字符集
//
// conDataBase =DriverManager.getConnection(url);
// System.out.println("数据库连接成功");
/**************************MYSQL**********************************************************/
/**************************MS Access**********************************************************/
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// System.out.println("驱动加载成功");
// //String url="jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ="+application.getRealPath("."+File.separator+"Communication.mdb");
// String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=."+File.separator+"Communication.mdb";
// //上述url中的Driver和(*.mdb)之间空格有且只能有一个
// //sun.jdbc.odbc.JdbcOdbcDriver驱动程序类名
// //DBQ=."+File.separator+"Communication.mdb"数据库文件
// conDataBase=DriverManager.getConnection(url,"root","root");
// System.out.println("数据库连接成功");
/**************************MS Access**********************************************************/
/**************************SQL Server 2000**********************************************************/
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("驱动加载成功");
String url="jdbc:odbc:JavaCom";
//conDataBase=DriverManager.getConnection("jdbc:microsoft:sqlserver://NUSTCS608\\NUSTSQLSERVER:1433;DatabaseName=JavaCommunication","sa","");
//conDataBase=DriverManager.getConnection("jdbc:odbc:JavaCom","sa","");
conDataBase=DriverManager.getConnection(url,"sa","");
System.out.println("数据库连接成功");
/**************************SQL Server 2000**********************************************************/
//******************************读数据库用到的变量*********************************************
String RegName,UserName,Password,IpAddr,Port,Job,Sex,EMail,Tel;
String lastDate,PubMsg,SendrName,PubMsgID;
String fromName,toName,msg;
UserStatement=conDataBase.createStatement();
PubMsgStatement=conDataBase.createStatement();
MsgStatement=conDataBase.createStatement();
ServerLogStatement=conDataBase.createStatement();
UserDatas=UserStatement.executeQuery("select * from UserInfo");//读取数据库中表userinfo用户信息
while(UserDatas.next()){
System.out.println("UserInfo 已经读出");
RegName=UserDatas.getString("RegName");
UserName=UserDatas.getString("UserName");
Password=UserDatas.getString("Password");
Sex=UserDatas.getString("Sex");
Job=UserDatas.getString("Job");
IpAddr=UserDatas.getString("IpAddr");
Port=UserDatas.getString("Port");
lastDate=UserDatas.getString("lastDate");
//UserName=CharConvert.GB2312toUnicode(UserName);//转换成正确的中文字符
//Job=CharConvert.GB2312toUnicode(Job);//转换成正确的中文字符
//EMail=UserDatas.getString("EMail");
//Tel=UserDatas.getString("Tel");
if(RegName.equals("admin")){
SerAdminPassword=Password;
AdminName=RegName;
continue;
}
yourself=new UserInfo(RegName,UserName,Password,lastDate,IpAddr,Integer.parseInt(Port));
yourself.setStrSex(Sex);
yourself.setJob(Job);
//yourself.setJob(EMail);
//yourself.setJob(Tel);
AllUsers.put(yourself.getRegName(),yourself);
System.out.println(RegName+" "+UserName
+" "+Password+" "+IpAddr
+" "+Port+" "+Job
+" "+Sex+" "+lastDate);
}
PubMsgDatas=PubMsgStatement.executeQuery("select * from PublicMessage");//读取数据库中表userinfo用户信息
System.out.println("公告已经读出");
while(PubMsgDatas.next()){
PubMsg=PubMsgDatas.getString("PubMsg");
PubMsg=CharConvert.GB2312toUnicode(PubMsg);//转换成正确的中文字符
SendrName=PubMsgDatas.getString("SenderName");
lastDate=PubMsgDatas.getString("lastDate");
PublicMessagelist.add(new PublicMessage(PubMsg,lastDate,SendrName));
System.out.println(PubMsg+" "+SendrName+" "+lastDate);
}
MsgDatas=MsgStatement.executeQuery("select * from Message");//读取数据库中表userinfo用户信息
System.out.println("留言已经读出");
while(MsgDatas.next()){
fromName=MsgDatas.getString("fromName");
toName=MsgDatas.getString("toName");
msg=MsgDatas.getString("msg");
msg=CharConvert.GB2312toUnicode(msg);//转换成正确的中文字符
lastDate=MsgDatas.getString("lastDate");
Messagelist.add(new Message(fromName,toName,msg,lastDate));
System.out.println(fromName+" "+toName+" "+msg+" "+lastDate);
}
/**************************MS Access**********************************************************/
//监听3000号端口
welcome=new ServerSocket(3000);
//将服务器所在IP和Port写入文件,方便客户端使用
ServerInfo=new File("."+File.separator+"SerIPort.dat");
ToFile=new DataOutputStream(new FileOutputStream(ServerInfo));//会自动删除原来的文件新建一个同名文件
ToFile.write((getIPAddr()+":"
+Integer.toString(welcome.getLocalPort())).getBytes());
ToFile.close();
//服务器端主界面
serverframe=new ServerFrame(this);
评论0