Java网络编程技术-10.ppt
第10章 Java Socket网络编程案例Java SWing聊天程序设计210.110.1 Java SWing聊天程序设计需求分析1).应用程序架构:Client/Server2).通信协议:TCP协议3).使用数据库保存用户信息MS SQL Server 2005 或 MySql、MS Access 10.2 界面与业务逻辑普通用户聊天-公聊,私聊管理员用户聊天 -公聊,私聊,踢用户注册新用户管理-删除用户10.3系统用例图普通用户管理员用户10.4系统类图11 10.5 数据库操作类数据库操作类 DBHandlerDBHandlerimport java.sql.*;import java.util.*;public class DBHandler private static Connection con=getConn();private static Statement smt=null;exeQuery()方法private static ResultSet exeQuery(String query)throws SQLException if(con=null)con=getConn();if(smt=null)smt=con.createStatement();return smt.executeQuery(query);exeUpdateQuery(String query)方法private static void exeUpdateQuery(String query)throws SQLException if(smt=null)smt=con.createStatement();smt.executeUpdate(query);Connection getConn()方法private static Connection getConn()String url=(jdbc:odbc:chatroom);try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);String name=sa;String pw=admin;return(DriverManager.getConnection(url,name,pw);catch(SQLException e)e.printStackTrace();catch(ClassNotFoundException e)e.printStackTrace();return null;isAuthorized(String name,String pw)方法public static boolean isAuthorized(String name,String pw)String query=select*from userInfo where name=+name+and password=+pw+;try ResultSet rs=exeQuery(query);return rs.next();catch(SQLException e)e.printStackTrace();return false;deleteUser(String name)方法public static boolean deleteUser(String name)String query=delete from userInfo where name=+name+;try exeUpdateQuery(query);return true;catch(SQLException e)e.printStackTrace();return false;getPrio(String name,String password)方法public static int getPrio(String name,String password)String query=select role from userInfo where name=+name+and password=+password+;ResultSet rs;try rs=exeQuery(query);rs.next();return rs.getInt(1);catch(SQLException e)e.printStackTrace();return 0;regUser(String name,String password)方法public static int regUser(String name,String password)String query1=insert into userInfo(name,password,role)values(+name+,+password+,1);String query2=select count(*)from userInfo where name=+name+;ResultSet rs;try rs=exeQuery(query2);rs.next();if(rs.getInt(1)0)return 1;exeUpdateQuery(query1);return 0;catch(SQLException e)e.printStackTrace();return-1;getAllUsersNum()方法public static int getAllUsersNum()String query=select count(*)from userInfo;try ResultSet rs=exeQuery(query);rs.next();return rs.getInt(1);catch(SQLException e)e.printStackTrace();return-1;List getAllUsers()方法public static List getAllUsers()String query=select name from userInfo where role0;List names=new ArrayList();try ResultSet rs=exeQuery(query);while(rs.next()names.add(rs.getString(1);catch(SQLException e)e.printStackTrace();return names;isAdmin(String name,String pw)方法public static boolean isAdmin(String name,String password)String query=select*from userInfo where name=+name+and password=+pw+and role=0;ResultSet rs;try rs=exeQuery(query);return rs.next();catch(SQLException e)e.printStackTrace();return false;10.6 JDBC 数据库连接 1.MS SQL 2005private static Connection getConn()String url=jdbc:sqlserver:/localhost:1433/chatroom;try Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);String name=root;String password=11111111;return(DriverManager.getConnection(url,name,password);catch(SQLException e)return null;Access 数据库(ODBC DSN)private static Connection getConn()String url=(jdbc:odbc:chatroom);/chatroom DSN try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);return(DriverManager.getConnection(url);catch(SQLException e)Access 数据库(File Name)private static Connection getConn()String url=jdbc:odbc:driver=Microsoft Access Driver(*.mdb);DBQ=chatroom.mdb;try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);return(DriverManager.getConnection(url);catch(SQLException e)