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

最新文章

  1. Linux WebServer WebRoot Path Identification
  2. 【python】队列
  3. Android进阶笔记12:Manymo(在线安卓系统模拟器工具)
  4. 工具类_java 数字转化为汉字大写
  5. [转]How To Use CSS3 Media Queries To Create a Mobile Version of Your Website
  6. _foreach
  7. TSC打印机使用教程终极版
  8. java调用c++函数的简单笔记
  9. 【shell点滴】参数变量
  10. CSS学习笔记1:基础知识
  11. hibernate 嵌套事务
  12. dos2unix 批量转化文件
  13. LoRa---sx1278的FIFO工作流程和应用注意事项
  14. 如何使你的Android应用记住曾经使用过的账户信息
  15. 什么是SQL游标?
  16. SQL SERVER2014 安装 Error code 0x858C001B.
  17. centos7 jenkins安装和使用
  18. JAVA 从头开始<二>
  19. SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
  20. 『NiFi 学习之路』使用 —— 主要组件的使用

热门文章

  1. 【洛谷1967】货车运输(最大生成树+倍增LCA)
  2. 二叉搜索树(BST)学习笔记
  3. CentOS替换系统自带JDK
  4. python 线程even
  5. 操作表单 -------JavaScrip
  6. MySQL-Xtrabackup备份还原
  7. JavaScript常用八种继承方案
  8. mysql四:数据操作
  9. 【CodeBase】通过层级键在多维数组中获取目标值
  10. java问题随笔