是一个简单的客户端收集数据再传送给服务器端的的程序
客户端:
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainFrame extends JFrame {
private String serverIP;
public Socket socket;
//主面板
private JPanel p;
//声明组件
private JLabel id,name,age,eclass,grade;
//声明五个文本框文本框
private JTextField tid,tname,tage,tclass,tgrade;
//声明两个按钮
private JButton btyes,btremove;
private JTextArea txt;
btyes.addActionListener(new RegisterListener());
btremove.addActionListener(new ResetListener());
//组件放入小面板小面板放在主面版
//监听类
//确定按钮的监听时间,当确定之后开始获取用户输入的数据,并创建客户端连接服务器发送数据
public class RegisterListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//获取用户输入数据
// 学生对象,放入数据,准备发送
Student stu = new Student();
stu.setId(Integer.parseInt(tid.getText().trim()));
stu.setName(tname.getText().trim());
stu.setAge(Integer.parseInt(tage.getText().trim()));
stu.setEclass(tclass.getText().trim());
stu.setGrade(tgrade.getText().trim());
try {
if (stu != null) {
//创建连接到本机端口号为28888的socket对象
Socket stusocket = new Socket(serverIP, 28888);
//创建用于序列化匹配日志信息对象的输出流
ObjectOutputStream outstream = new ObjectOutputStream(stusocket.getOutputStream());
//向流中写入学生信息
outstream.writeObject(stu);
outstream.flush();
outstream.close();
JOptionPane.showMessageDialog(null, "匹配的学生信息已经发送到服务器!");
} else {
JOptionPane.showMessageDialog(null, "信息填写错误!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public class ResetListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
tid.setText("");
tname.setText("");
tage.setText("");
tclass.setText("");
tgrade.setText("");
}
}
public static void main(String[] args) {
new MainFrame();
}
}
服务器端的代码:
public class Serverdemo {
public Serverdemo() {
new AcceptServer(28888).start();
System.out.println("服务器已开启....");
}
//接收数据的线程类
private class AcceptServer extends Thread {
private ServerSocket serverSocket;
private Socket socket;
private Student student2;
private ObjectInputStream ois;
public AcceptServer(int port) {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
//重写run方法,将数据保存到数据库中
public void run() {
while (this.isAlive()) {
try {
//接收来自客户端的套接字
socket = serverSocket.accept();
if (socket != null) {
ois=new ObjectInputStream(socket.getInputStream());
//反序列化,我要写代码写一万行我要写一万行把代码写烂!!!
ObjectInputStream outstream2=new ObjectInputStream(ois);
student2=(Student) outstream2.readObject();
if (saveStu(student2)) {
System.out.println("注册成功!");
} else {
System.out.println("注册失败!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean saveStu(Student stu) {
boolean r = false;
DBUtil db = new DBUtil();
try {
db.getConnection();
String sql = "INSERT INTO student VALUES(?,?,?,?,?)";
Object[] p = new Object[]{stu.getId(), stu.getName(), stu.getAge(), stu.getEclass(), stu.getGrade()};
if (db.executeUpdate(sql, p) > 0) {
r = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
db.closeAll();
}
return r;
}
}
public static void main(String[]args){
new Serverdemo();
}
}