实现效果
(1) 在client端可以实现对数据库的操作(Select,Insert,Update,Delete)
(2)数据库中创建一个考试表和学生表,考试表中问题是四项选择题(10道以上),client端做题,成绩存入学生表
(3)实现多个Client程序对server端的访问 Hint:Java程序中会使用多个class,server端使用thread监听多个client 。
一、创建数据库demo,数据表student、question。
其中student表存储了学生的姓名以及成绩记录、question表中存储了单项选择题的有关内容。
二、创建Student、Question 两个实体类
包含属性,Getter,Setter方法
Student.java
package com.jxnu.demo;/*** @author xiao*/
public class Student {//主键IDprivate Integer id;//用户名private String username;//分数private String grade;//生日public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}@Overridepublic String toString() {return "Student{" +"id=" + id +", username='" + username + '\'' +", grade=" + grade +'}';}
}
Question.java
package com.jxnu.demo;/*** @author xiao*/
public class Question {private Integer id;private String Content;private String OptionA;private String OptionB;private String OptionC;private String OptionD;private String answer;private String grade;public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getContent() {return Content;}public void setContent(String content) {Content = content;}public String getOptionA() {return OptionA;}public void setOptionA(String optionA) {OptionA = optionA;}public String getOptionB() {return OptionB;}public void setOptionB(String optionB) {OptionB = optionB;}public String getOptionC() {return OptionC;}public void setOptionC(String optionC) {OptionC = optionC;}public String getOptionD() {return OptionD;}public void setOptionD(String optionD) {OptionD = optionD;}public String getAnswer() {return answer;}public void setAnswer(String answer) {this.answer = answer;}@Overridepublic String toString() {return "Question{" +"id=" + id +", Content='" + Content + '\'' +", OptionA='" + OptionA + '\'' +", OptionB='" + OptionB + '\'' +", OptionC='" + OptionC + '\'' +", OptionD='" + OptionD + '\'' +", answer='" + answer + '\'' +", grade='" + grade + '\'' +'}';}
}
三、导入jar包,编写三个与数据库相关的Java Class,DbManage、
StudentDao和QuestionDao。
其中DbManage有三个方法,initDB()负责打开驱动,两个close()方法分别关闭有结果集与无结果集。
DBManage.java
package com.jxnu.demo;import java.sql.*;/*** @author xiao*/
public class DbManage {//打开驱动public Connection initDB() {Connection conn = null;final String DRIVER_NAME = "com.mysql.jdbc.Driver";//数据库连接地址final String URL = "jdbc:mysql://localhost:3306/socket?useUnicode=true&characterEncoding=utf-8&useSSL=FALSE&serverTimezone=UTC";//用户名final String USER_NAME = "root";//密码final String PASSWORD = "";try {Class.forName(DRIVER_NAME);conn = DriverManager.getConnection(URL,USER_NAME,PASSWORD);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return conn;}//关闭无结果集public void closeDB(PreparedStatement pstmt, Connection con) {try {if(pstmt!=null) {pstmt.close();}if(con!=null) {con.close();}} catch (SQLException e) {e.printStackTrace();}}//关闭有结果集public void closeDB(ResultSet rs, PreparedStatement pstmt, Connection con) {try {if(pstmt!=null) {pstmt.close();}if(con!=null) {con.close();}if(rs!=null){rs.close();}} catch (SQLException e) {e.printStackTrace();}}
}
StudentDao.java
StudentDao负责有关学生信息的增删查改(insert、update、delete、findAll)
package com.jxnu.demo;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;/*** @author xiao*/
public class StudentDao {/*** 添加* @param student* @return*/public boolean insert(Student student) {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;String sql = "insert into student(username,grade) VALUES(?,?)";boolean res = false;try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);pstmt.setString(1,student.getUsername());pstmt.setString(2,student.getGrade());res = (pstmt.executeUpdate() == 1);}catch (SQLException e) {e.printStackTrace();}finally {dbmanage.closeDB(pstmt, con);}return res;}/*** 删除* @param id* @return*/public boolean delete(Integer id) {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;String sql = "delete from student where id = ?";boolean flag = false;try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);pstmt.setInt(1, id);int result = pstmt.executeUpdate();if (result > 0) {flag = true;}} catch (Exception e) {e.printStackTrace();}finally {dbmanage.closeDB(pstmt, con);}return flag;}/*** 修改信息* @param student* @return*/public boolean update(Student student) {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;String sql = "update student set username=? where id= ?";boolean res = false;try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);pstmt.setString(1,student.getUsername());pstmt.setInt(2,student.getId());res = (pstmt.executeUpdate()==1);}catch (SQLException e) {e.printStackTrace();return false;}finally {dbmanage.closeDB(pstmt, con);}return res;}public boolean updateG(Student student) {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;String sql = "update student set grade=? where id= ?";boolean res = false;try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);pstmt.setString(1,student.getGrade());pstmt.setInt(2,student.getId());res = (pstmt.executeUpdate()==1);}catch (SQLException e) {e.printStackTrace();return false;}finally {dbmanage.closeDB(pstmt, con);}return res;}/*** 查找所有数据* @return*/public List<Student> findAll() {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;ResultSet rs = null;List<Student> list = new ArrayList<Student>();String sql = "select * from student";try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);rs = pstmt.executeQuery();while (rs.next()) {Student student = new Student();student.setId(rs.getInt("id"));student.setUsername(rs.getString("username"));student.setGrade(rs.getString("grade"));list.add(student);}}catch (Exception e) {e.printStackTrace();}finally {dbmanage.closeDB(rs,pstmt, con);}return list;}/*** 根据ID查询* @param id* @return*/public List<Student> findById(Integer id) {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;ResultSet rs = null;//MessageEntity messageEntity = new MessageEntity();List<Student> list = new ArrayList<Student>();String sql = "select * from student where id=?";try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);pstmt.setInt(1, id);rs = pstmt.executeQuery();while(rs.next()){Student student = new Student();student.setId(rs.getInt("id"));student.setUsername(rs.getString("username"));student.setGrade(rs.getString("grade"));list.add(student);}}catch (Exception e) {e.printStackTrace();}finally {dbmanage.closeDB(rs,pstmt, con);}return list;}}
QuestionDao.java
QuestionDao负责有关题目的增删改查
package com.jxnu.demo;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;/*** @author xiao*/
public class QuestionDao {public List<Question> findAll() {DbManage dbmanage = new DbManage();Connection con = null;PreparedStatement pstmt =null;ResultSet rs = null;List<Question> list = new ArrayList<Question>();String sql = "select * from question";try {con = dbmanage.initDB();pstmt = con.prepareStatement(sql);rs = pstmt.executeQuery();while (rs.next()) {Question question = new Question();question.setId(rs.getInt("id"));question.setAnswer(rs.getString("answer"));question.setContent(rs.getString("content"));question.setOptionA(rs.getString("optionA"));question.setOptionB(rs.getString("optionB"));question.setOptionC(rs.getString("optionC"));question.setOptionD(rs.getString("optionD"));list.add(question);}}catch (Exception e) {e.printStackTrace();}finally {dbmanage.closeDB(rs,pstmt, con);}return list;}
}
三、编写StudentService类,用于处理增删改查以及答题的逻辑
package com.jxnu.demo;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;/*** @author xiao*/
public class StudentService{public void show(Socket socket) throws IOException {DataOutputStream out = new DataOutputStream(socket.getOutputStream());StudentDao dao = new StudentDao();List<Student> list = dao.findAll();out.writeUTF(String.valueOf(list));}public void insert(Socket socket) throws IOException {DataInputStream input = new DataInputStream(socket.getInputStream());DataOutputStream out = new DataOutputStream(socket.getOutputStream());out.writeUTF("请输入新增用户名");String username = input.readUTF();out.writeUTF("请输入用户成绩");String grade = input.readUTF();StudentDao dao = new StudentDao();Student student = new Student();student.setUsername(username);student.setGrade(grade);if(dao.insert(student))out.writeUTF("添加成功");elseout.writeUTF("添加失败");}public void delete(Socket socket) throws IOException {DataInputStream input = new DataInputStream(socket.getInputStream());DataOutputStream out = new DataOutputStream(socket.getOutputStream());out.writeUTF("请输入要删除的ID号");String id = input.readUTF();StudentDao dao = new StudentDao();if(dao.delete(Integer.valueOf(id)))out.writeUTF("删除成功");elseout.writeUTF("删除失败,请确认ID是否存在");}public void update(Socket socket) throws IOException {DataInputStream input = new DataInputStream(socket.getInputStream());DataOutputStream out = new DataOutputStream(socket.getOutputStream());out.writeUTF("请输入要修改的用户ID号");String id = input.readUTF();out.writeUTF("请输入修改后的用户名");String username = input.readUTF();StudentDao dao = new StudentDao();Student student = new Student();student.setId(Integer.valueOf(id));student.setUsername(username);if(dao.update(student))out.writeUTF("更新成功");elseout.writeUTF("更新失败");}public void answer(Socket socket) throws IOException {DataInputStream input = new DataInputStream(socket.getInputStream());DataOutputStream out = new DataOutputStream(socket.getOutputStream());QuestionDao questionDao = new QuestionDao();List<Question> questionList = questionDao.findAll();Question question = new Question();out.writeUTF(String.valueOf(questionList.size()));int n = questionList.size();//out.writeUTF("请输入题号");int sum = 0;String[] answers = new String[20];for(int i=0;i<n;i++){out.writeUTF("第"+(i+1)+"题"+": "+questionList.get(i).getContent());out.writeUTF("A. " + questionList.get(i).getOptionA() + "\t"+ "B. " + questionList.get(i).getOptionB() + "\t"+ "C. " + questionList.get(i).getOptionC() + "\t"+ "D. " + questionList.get(i).getOptionD());answers[i]=input.readUTF();System.out.println(answers[i]);if(answers[i].equals(questionList.get(i).getAnswer())){out.writeUTF("回答正确,+10 分\n");sum += 10;}elseout.writeUTF("回答错误\n");}String id = input.readUTF();out.writeUTF("总成绩为"+sum);StudentDao dao = new StudentDao();Student student = new Student();student.setId(Integer.valueOf(id));student.setGrade(String.valueOf(sum));if (dao.updateG(student))out.writeUTF("成绩保存成功");elseout.writeUTF("成绩保存失败");}
}
四、编写Server类。
其中server端使用thread监听多个client,根据Client端的指令调用不同的方法并执行。收到指令“1”,执行StudentService中的show()方法,查询出学生表中所有的记录并返回给Client;收到指令“2”,执行StudentService中的insert()方法,获取客户端传来的姓名与成绩并插入student表中。收到指令“3”,执行update()方法,可以实现根据client端传来的ID号和新姓名实现修改姓名的功能;收到指令“4”,执行delete()方法,可以根据Client端传来的ID号实现删除学生记录的功能;收到指令“5”,实现答题功能,用户每回答一个题目,Server端判断正误并将结果返回给Client端,最后计算出总成绩存入student表;收到指令“0”,Client端将断开连接。
package com.jxnu.demo;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;/*** @author xiao*/
public class Server {public static final int PORT = 12345;//监听的端口号public static void main(String[] args) {System.out.println("服务器启动...\n");Server server = new Server();server.init();}public void init() {try {ServerSocket serverSocket = new ServerSocket(PORT);while (true) {// 一旦有堵塞, 则表示服务器与客户端获得了连接Socket client = serverSocket.accept();// 处理这次连接new HandlerThread(client);}} catch (Exception e) {System.out.println("服务器异常: " + e.getMessage());}}public static class HandlerThread implements Runnable {private Socket socket;public HandlerThread(Socket client) {socket = client;new Thread(this).start();}public void run() {try {// 读取客户端数据DataInputStream input = new DataInputStream(socket.getInputStream());String clientInputStr = input.readUTF();//这里要注意和客户端输出流的写方法对应,否则会抛 EOFException// 处理客户端数据System.out.println("客户端:" + clientInputStr);// 向客户端回复信息DataOutputStream out = new DataOutputStream(socket.getOutputStream());action(clientInputStr,socket);out.close();input.close();} catch (Exception e) {System.out.println("服务器 run 异常: " + e.getMessage());} finally {if (socket != null) {try {socket.close();} catch (Exception e) {socket = null;System.out.println("服务端 finally 异常:" + e.getMessage());}}}}public void action(String clientInputStr,Socket socket) throws IOException {DataOutputStream out = new DataOutputStream(socket.getOutputStream());StudentService studentService = new StudentService();switch (clientInputStr){case "1":studentService.show(socket);break;case "2":studentService.insert(socket);break;case "3":studentService.delete(socket);break;case "4":studentService.update(socket);break;case "5":studentService.answer(socket);break;case "0":out.writeUTF("OK");break;default:out.writeUTF("输入指令有误,请重新输入");}}}
}
六、编写Client类
package com.jxnu.demo;import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;/*** @author xiao*/
public class Client {public static final String IP_ADDR = "192.168.19.133";//服务器地址public static final int PORT = 12345;//服务器端口号public static void main(String[] args) {System.out.println("客户端启动...");System.out.print("【提示】 1 表示查看所有学生信息, 2 表示添加学生信息, 3 表示删除学生信息, 4表示修改学生信息, 5表示做题, 0表示结束\n ");while (true) {Socket socket = null;try {//创建一个流套接字并将其连接到指定主机上的指定端口号socket = new Socket(IP_ADDR, PORT);//读取服务器端数据DataInputStream input = new DataInputStream(socket.getInputStream());//向服务器端发送数据DataOutputStream out = new DataOutputStream(socket.getOutputStream());System.out.println("请输入指令: \t");String str = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(str);String ret = input.readUTF();System.out.println("服务器端: " + ret);if("请输入要删除的ID号".equals(ret)) {String id = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(id);String result = input.readUTF();System.out.println(result);}if("请输入要修改的用户ID号".equals(ret)) {String id = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(id);System.out.println("服务器端: "+input.readUTF());String username = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(username);String result = input.readUTF();System.out.println(result);}if("请输入新增用户名".equals(ret)) {String username = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(username);System.out.println("服务端: " + input.readUTF());String grade = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(grade);String result = input.readUTF();System.out.println(result);}// 如接收到 "OK" 则断开连接if ("OK".equals(ret)) {System.out.println("客户端将关闭连接");Thread.sleep(500);break;}if (ret.length() <=3 ) {Integer n = Integer.valueOf(ret);System.out.println("总共" + n + "道题,每题10分,总分100分");for(int i=0;i<n;i++) {System.out.println(input.readUTF());System.out.println(input.readUTF());System.out.println("请输入答案: ");String answer = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(answer);System.out.println(input.readUTF());}System.out.println("请输入你的用户ID号");String id = new BufferedReader(new InputStreamReader(System.in)).readLine();out.writeUTF(id);String sum = input.readUTF();System.out.println(sum);String result = input.readUTF();System.out.println(result);}out.close();input.close();} catch (Exception e) {System.out.println("客户端异常:" + e.getMessage());} finally {if (socket != null) {try {socket.close();System.out.println("socket is closed");} catch (IOException e) {socket = null;System.out.println("客户端 finally 异常:" + e.getMessage());}}}}}
}
效果图
这里我还弄了点花里胡哨的颜色,有点好看嘿嘿。