首页 > 代码库 > socket传输对象

socket传输对象

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;
    }
}

 

socket传输对象