socket传输对象
2024-09-05 08:40:55
server
public class Server{ private static int port = 8888;
private static ServerSocket serverSocket;
private static Socket socket; public static void main(String args[]) throws IOException { serverSocket = new ServerSocket(port, 2);
socket = serverSocket.accept();
System.out.println("客户端已链接"); try {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
JDBCService jdbcService = new JDBCService(); while(true){ Employee employee = (Employee) ois.readObject();
String s= employee.getName();
if(s.equals("bye")){
ois.close();
socket.close();
System.out.println("关闭服务器的 socket");
break;
} jdbcService.add(employee.getName(),employee.getEmail(),employee.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
client
public class Client { private static int port = 8888;
private static String host = "localhost";
private static Socket socket; public static void main(String args[]) throws UnknownHostException,
IOException {
socket = new Socket(host, port);
ObjectOutputStream oos= new ObjectOutputStream(socket.getOutputStream());
Employee employee =new Employee(); while(true){
employee.setName("王小东");
employee.setEmail("qq@.com");
employee.setPwd("12334");
// 发送键盘输入的一行
String s = new BufferedReader(new InputStreamReader(System.in)).readLine(); if(s.equals("bye")){
employee.setName("bye");
oos.writeObject(employee);
oos.flush(); oos.close();
socket.close();
System.out.println("关闭客户端的 socket");
break;
}else if(s.equals("send")){
oos.writeObject(employee);//写入数据
oos.flush();//发送数据
}else{
System.out.println("无效指令,请重新输入");
}
}
}
}
JDBCService.java
public class JDBCService { //插入操作
public int add(String uname,String uemail,String upwd) {
int i=0;
String sql="insert into employee (name,email,pwd) values (?,?,?)";
DBConnection db = new DBConnection();
try {
PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
preStmt.setString(1, uname);
preStmt.setString(2, uemail);
preStmt.setString(3, upwd);
preStmt.executeUpdate();
//Statement statement = (Statement) db.conn.createStatement();
//statement.executeUpdate(sql); preStmt.close();
db.close();//关闭连接
} catch (Exception e) {
e.printStackTrace();
}
return i;//返回影响的行数,1为执行成功
}
//查找操作
public void show(){
String sql ="select * from employee";
DBConnection db = new DBConnection(); System.out.println("-----------------");
System.out.println("姓名" +"\t"+ "邮箱" +"\t"+ "日期");
System.out.println("-----------------"); try {
Statement stmt = (Statement) db.conn.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery(sql);
while(rs.next()){
String uname = rs.getString("name");
String uemail = rs.getString("email");
String uhiredate = rs.getString("hiredate");
//可以将查找到的值写入类,然后返回相应的对象
//这里 先用输出的端口显示一下
System.out.println(uname +"\t"+ uemail +"\t"+ uhiredate);
}
rs.close();
db.close();//关闭连接
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新操作
public int update(String uname,String uemail,String upwd) {
int i =0;
String sql="update employee set email=?,pwd=? where name=?";
DBConnection db = new DBConnection(); try {
PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
preStmt.setString(1, uemail);
preStmt.setString(2, upwd);
preStmt.setString(3, uname);
preStmt.executeUpdate(); preStmt.close();
db.close();//关闭连接
} catch (SQLException e) {
e.printStackTrace();
}
return i;//返回影响的行数,1为执行成功
}
//删除操作
public int del(String uname) {
int i=0;
String sql="delete from employee where name=?";
DBConnection db = new DBConnection();
try {
PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
preStmt.setString(1, uname);
preStmt.executeUpdate(); preStmt.close();
db.close();//关闭连接
} catch (SQLException e){
e.printStackTrace();
}
return i;//返回影响的行数,1为执行成功
}
}
Employee.java
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String pwd;
private Integer grade;
private String email;
private java.util.Date hiredate;
private Float salary; public Employee() { } public Employee(String name, String pwd, Integer grade, String email, Date hiredate, Float salary) {
this.name = name;
this.pwd = pwd;
this.grade = grade;
this.email = email;
this.hiredate = hiredate;
this.salary = salary;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public java.util.Date getHiredate() {
return hiredate;
}
public void setHiredate(java.util.Date hiredate) {
this.hiredate = hiredate;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
}
最新文章
- Linux WebServer WebRoot Path Identification
- 【python】队列
- Android进阶笔记12:Manymo(在线安卓系统模拟器工具)
- 工具类_java 数字转化为汉字大写
- [转]How To Use CSS3 Media Queries To Create a Mobile Version of Your Website
- _foreach
- TSC打印机使用教程终极版
- java调用c++函数的简单笔记
- 【shell点滴】参数变量
- CSS学习笔记1:基础知识
- hibernate 嵌套事务
- dos2unix 批量转化文件
- LoRa---sx1278的FIFO工作流程和应用注意事项
- 如何使你的Android应用记住曾经使用过的账户信息
- 什么是SQL游标?
- SQL SERVER2014 安装 Error code 0x858C001B.
- centos7 jenkins安装和使用
- JAVA 从头开始<;二>;
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
- 『NiFi 学习之路』使用 —— 主要组件的使用