8:30-9:00 学习打卡
9:30-12:00 写qq项目
15:00-17:00 写qq项目
1.登录时调用数据库Mysql类
按下登录时将账号和密码发送到服务器,并将密码传到Mysql类中。
package qqserver.mode;
import java.sql.*;
//mysql的连接
public class Mysql {
private static String driver="com.mysql.cj.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/qq";
private static String user="root";
private static String pass="123456";
public Mysql() {
}
public static void main(String[] args) {
System.out.println(new Mysql().qun("1","01"));
}
public String inlt(String ok,String ss) {
Connection con = null;
Statement st = null;
String s =null;
try {
Class.forName(driver);//注册驱动
con = DriverManager.getConnection(url, "root", "123456");//获取连接
st = con.createStatement();
String s2 = "SELECT * FROM namber WHERE id=" + ss;
ResultSet res = st.executeQuery(s2);
res.next();
if(ok.equals("1"))
{
s = res.getString("passward");}//获取当前账号的密码
else if(ok.equals("2")){
s=res.getString("好友");//获取当前账号的好友
}else if(ok.equals("3")){
s=res.getString("群");//获取当前账号的群
}else if(ok.equals("4")){
s=res.getString("黑名单");//获取当前账号的黑名单
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (con != null)
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (st != null)
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return s;
}
public String qun(String ok,String ss) {
Connection con = null;
Statement st = null;
String s =null;
try {
Class.forName(driver);//注册驱动
con = DriverManager.getConnection(url, "root", "123456");//获取连接
st = con.createStatement();
String s2 = "SELECT * FROM qun WHERE 群id=" + ss;
ResultSet res = st.executeQuery(s2);
res.next();
if(ok.equals("1"))
{
s = res.getString("群成员");}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (con != null)
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (st != null)
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return s;
}
}
2.好友在线和群聊天发送
将信息发给服务器,服务器判断种类,为登录,调用数据库,然后取出信息,和好友,向好友发送在线包,自己也用线程得到好友的是否在线。如果为群聊天,从数据库的群的成员并判断是否在线然后在发送。
package qqserver.mode;
//qq服务器内部
import qqc.massage.Massage;
import qqc.mode.Mthreads;
import qqserver.view.qqserverv;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class qqserverm {
public qqserverm(){
try {
ServerSocket ss = new ServerSocket(8888);
while (true) {
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
Massage u1 = (Massage) ois.readObject();//收到消息
if(u1.getS().equals("1")) {//如果为登录消息
Massage m = new Massage();
System.out.println("账号为: " + u1.getMyid());
Mysql my = new Mysql();
String pass = my.inlt("1", u1.getMyid());
if (pass.equals(u1.getUserpassward())) {
m.setS("1");
m.setXiaoxi(my.inlt("2", u1.getMyid()));
m.setQun(my.inlt("3", u1.getMyid()));
m.setHei(my.inlt("4", u1.getMyid()));
String[] f1 = m.getXiaoxi().split("\\|");//数据库提取到的好友
ArrayList<String> zaix = new ArrayList<String>();
Massage ms = new Massage();
ms.setMyid(u1.getMyid());
ms.setS("11");
for (int i = 0; i < f1.length; i++) {
if (Mythreads.hm.containsKey(f1[i])) {//如果好友线程在线,加入。
zaix.add(f1[i]);
ObjectOutputStream oos2 = new ObjectOutputStream(Mythreads.getthread(f1[i]).s.getOutputStream());
ms.setOid(f1[i]);
oos2.writeObject(ms);//将自己在线提示提示好友
}
}
m.setZaix(zaix);//将结果传给登陆人
oos.writeObject(m);
System.out.println("密码正确");
Mythread m1 = new Mythread(s);
Mythreads.add(u1.getMyid(), m1);
m1.start();
System.out.println(u1.getMyid() + " " + m1 + ":加入线程");
} else {
m.setS("2");
oos.writeObject(m);
System.out.println("密码错误");
s.close();
}
} else if(u1.getS().equals("4")){//群消息
Mysql my=new Mysql();
String[] s1=my.qun("1",u1.getQun()).split("\\|");//取出群的成员
Massage ms=new Massage();
ms.setS("4");
for(int i=0;i<s1.length;i++){
if(Mthreads.hm.containsKey(s1[i])){//在线将发送消息
ObjectOutputStream oos2 = new ObjectOutputStream(Mythreads.getthread(s1[i]).s.getOutputStream());
ms.setOid(s1[i]);
oos2.writeObject(ms);
System.out.println(s1[i]);
}
}
}
}
} catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
public void inlt(){
}
public static void main(String argv[]){
new qqserverm();
}