最近看了TCP和UDP协议之后,写了个聊天机器人的练习。
首先是在mysql中新建一个名叫android的库,以及名叫dictionary的表,使用的是命令行
#登录mysql
mysql -u pabo -p
CREATE DATABASE android;
use android;
CREATE TABLE dictionary(
id INT,
receive varchar(100),
response varchar(100)
);
再往表里添加数据
INSERT INTO dictionary(id,receive,response) VALUES(1,'hi','你好'),
(2,'你叫什么','我是xx同学');
这里要注意表的默认编码不支持中文,需要将表的编码格式改成utf8
alter table dictionary modify receive varchar(100) set utf8;
alter table dictionary modify response varchar(100) set utf8;
然后就可以编写服务端和客户端程序了。
新建一个dictionary类,来保存数据库中获取到的数据。
public class Dictionary {
int id;
String receive;
String response;
}
接着编写DAO类,辅助数据库连接
public class SQLDao {
static String driver="com.mysql.jdbc.Driver";
static String url="jdbc:mysql://localhost:3306/android?useUnicode=true&characterEncoding=utf8&useSSL=false";
static String user="pabo";
static String password="pabo";
static Connection connection=null;
public static Connection getConnection() throws SQLException{
if(connection==null) {
connection=DriverManager.getConnection(url,user,password);
}
return connection;
}
//加载驱动
public static void Driver() throws ClassNotFoundException {
Class.forName(driver);
}
public static List<Dictionary> query(String receive) throws SQLException {
List<Dictionary> dictionaries=new ArrayList<Dictionary>();
String sql= "select * from dictionary where receive = ?";
Connection connection1 = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//建立连接
connection1 = SQLDao.getConnection();
preparedStatement=connection1.prepareStatement(sql);
preparedStatement.setString(1, receive);
//获取查询结果集
resultSet=preparedStatement.executeQuery();
while(resultSet.next()) {
Dictionary dictionary=new Dictionary();
int id=resultSet.getInt("id");
String receive1=resultSet.getString("receive");
String response=resultSet.getString("response");
dictionary.id=id;
dictionary.receive=receive1;
dictionary.response=response;
//存储获取的结果到list中
dictionaries.add(dictionary);
}
}catch (Exception e) {
e.printStackTrace();
}
return dictionaries;
}
}
在使用Statement语句执行带有中文的SQL语句时,时常遇到乱码的问题,需要设置SQL语句编码为UTF-8,在使用PreparedStatement没有遇到编码的问题
statement=connection.createStatement();
sql="select * from dictionary where receive= '你好'";
//将sql编码设置为utf8,没有中文乱码的问题
sql=new String(sql.getBytes(),"UTF-8");
ResultSet resultSet=statement.executeQuery(sql);
服务端使用ServerSocket创建ServerSocket对象,端口号是1000
ServerSocket serverSocket=new ServerSocket(10000);
System.out.println("监听端口号:10000");
Socket client=null;
client=serverSocket.accept();
InputStream inputStream=client.getInputStream();
OutputStream outputStream=client.getOutputStream();
//把输入流封装在DataInputStream
DataInputStream dataInputStream=new DataInputStream(inputStream);
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
//1.加载驱动
SQLDao.Driver();
String msg=null;
String sql="select * from dictionary where receive = ?";
while(!"exit".equals(msg)) {
//接收客户端数据
msg=dataInputStream.readUTF();
System.out.println("收到客户端数据:"+msg);
//数据库中查询
List<Dictionary> ds=new SQLDao().query(msg);
String response=null;
if(ds.isEmpty()) {
System.out.println("未找到");
}else {
response=ds.get(0).response;
}
dataOutputStream.writeUTF(response);
System.out.println("数据库查询到回应数据:"+response);
}
dataInputStream.close();
dataOutputStream.close();
client.close();
serverSocket.close();
客户端
Socket client=new Socket("127.0.1.1", 10000);
InputStream inputStream=client.getInputStream();
OutputStream outputStream=client.getOutputStream();
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
DataInputStream dataInputStream=new DataInputStream(inputStream);
Scanner scanner=new Scanner(System.in);
String string=null,string2 =null;
while(!"bye".equals(string)) {
string=scanner.nextLine();
dataOutputStream.writeUTF(string);
string2=dataInputStream.readUTF();
System.out.println(string2);
}
dataInputStream.close();
dataOutputStream.close();
outputStream.close();
执行结果
执行效果
网友评论