Java学习

在学java的过程中去利用相应的知识做做小项目,可以加深对知识的掌握。

步骤:

  1. 创建相应的机器人语料数据库
  2. 创建服务端,在服务端将数据库连接起来,并根据客户端发送的消息去数据库获得相应的回复,然后发送给客户端
  3. 创建客户端,连接上服务端后与服务端进行通信

代码

数据库部分

dao:

package Study.socket.talktorobot;


public class RobotMsg {

  private long id;
  private String receive;
  private String response;


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public String getReceive() {
    return receive;
  }

  public void setReceive(String receive) {
    this.receive = receive;
  }


  public String getResponse() {
    return response;
  }

  public void setResponse(String response) {
    this.response = response;
  }

}

数据库连接部分:

package Study.socket.talktorobot;

import java.sql.*;

/**
 * @description:
 * @author : Qing Zhang
 * @time: 08
 */
public class DataBaseOfMsg {

    String driver_Name = "com.mysql.jdbc.Driver";
    String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT";
    String USER_NAME = "root";
    String PASSWORD = "";
    Connection con = null;
    Statement s = null;


    public static void main(String[] args) {
        DataBaseOfMsg msg = new DataBaseOfMsg();
        msg.initial();

        System.out.println(msg.query("你好"));
    }


    /**
    * @Description: 初始化连接数据库
    * @Param: []
    * @return: void
    */
    public void initial() {
        this.initialDriver();
        this.connection();
        this.getStatement();
    }


    /**
     * @Description: 加载数据库
     * @Param: []
     * @return: void
     */
    private void initialDriver() {
        try {
            Class.forName(driver_Name);
            System.out.println("数据库驱动加载成功 !");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 获取连接
     * @Param: []
     * @return: void
     */
    private void connection() {
        try {
            con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            System.out.println("连接成功,获取连接对象: " + con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 获取操作对象
     * @Param: []
     * @return: void
     */
    private void getStatement() {
        try {
            s = con.createStatement();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @Description: 创建msg表
     * @Param: []
     * @return: void
     */
    public void createTable() {
        String sql = "create table robot_msg(" +
                "id int," +
                "receive varchar(100)," +
                "response varchar(100)" +
                ")";
        try {
            s.execute(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    /**
    * @Description: 插入数据
    * @Param: [paraMsg]
    * @return: void
    */
    public void insertOfS(RobotMsg paraMsg) {
        String sql = "insert into robot_msg values(null," + "'" + paraMsg.getReceive() + "'" + "," + "'" + paraMsg.getResponse() + "'" + ")";
        try {
            s.execute(sql);
            System.out.println("执行插入语句成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }



    public void close() {
        if (s != null) {
            try {
                s.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    public void query() {
        String sql = "select * from robot_msg";
        try {
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                int id = rs.getInt("id");// 可以使用字段名
                String receive = rs.getString(2);// 也可以使用字段的顺序
                String response = rs.getString(3);
                System.out.printf("%d\t%-16s\t%-16s\n", id, receive, response);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public String query(String paraReceive) {
        String sql = "select * from robot_msg where receive = '" + paraReceive+"'";
        try {
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                String response = rs.getString(3);
                return response;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "不知道怎么说";
    }
}

服务端

package Study.socket.talktorobot;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @description: 服务端
 * @author : Qing Zhang
 * @time: 08
 */
public class SeverOfRobot {

    public static void main(String[] args) {

        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务端开启!!!");
            Socket s = serverSocket.accept();
            System.out.println("有客户端连接进来!!!");


            DataBaseOfMsg responseData = new DataBaseOfMsg();
            responseData.initial();

            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        InputStream is = s.getInputStream();
                        DataInputStream dis = new DataInputStream(is);
                        OutputStream os = s.getOutputStream();
                        DataOutputStream dos = new DataOutputStream(os);
                        while (true) {
                            String receive = dis.readUTF();
                            if (receive != null) {
                                String response = responseData.query(receive);
                                dos.writeUTF(response);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
            thread.join();
            System.out.println("服务器关闭");
            s.close();
            serverSocket.close();
            responseData.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

客户端

package Study.socket.talktorobot;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @description:客户端
 * @author : Qing Zhang
 * @time: 08
 */
public class ClientOfRobot {

    public static void main(String[] args) throws IOException {

        //建立连接
        Socket socket = new Socket("localhost", 8888);
        System.out.println("开启客户端!!!");

        Thread thread = new Thread(){
            @Override
            public void run() {
                try {
                    //将输入流和输出流都加载
                    InputStream is = socket.getInputStream();
                    OutputStream os = socket.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(os);
                    DataInputStream dis = new DataInputStream(is);
                    Scanner scanner = new Scanner(System.in);
                    while (true){
                        dos.writeUTF(scanner.nextLine());
                        System.out.println(dis.readUTF());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        thread.start();
        try {
            thread.join();
            System.out.println("客户端退出!!!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

效果

java 对话机器人 java实现人机交互_java