Java實(shí)現(xiàn)簡(jiǎn)單聊天機(jī)器人
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單聊天機(jī)器人的具體代碼,供大家參考,具體內(nèi)容如下
整個(gè)小案例:整合了Java socket編程、jdbc知識(shí)(ORM/DAO)
創(chuàng)建數(shù)據(jù)庫(kù)和表,準(zhǔn)備表數(shù)據(jù)(MySQL)
首先創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)android
然后創(chuàng)建一個(gè)表dictionary,字段:
id int
receive varchar(100)
response varchar(100)
receive 表示受到的信息
response 表示回應(yīng)的信息
create database android; use android; create table dictionary( id int AUTO_INCREMENT, receive varchar(100), response varchar(100), PRIMARY KEY (id) ) DEFAULT CHARSET=utf8; insert into dictionary values(null,'你好','好你妹!'); insert into dictionary values(null,'你叫什么','你想泡我?。?); insert into dictionary values(null,'你叫什么','同志,不約'); insert into dictionary values(null,'打你哦','來(lái)啊,來(lái)打我啊');

對(duì)象設(shè)置(ORM)
對(duì)象關(guān)系映射,設(shè)置對(duì)象對(duì)應(yīng)數(shù)據(jù)庫(kù)中的各字段。
package socket;
public class Dictionary {
public int id;
public String receive; // 接收
public String response; // 回應(yīng)
}
數(shù)據(jù)訪問(wèn)對(duì)象(DAO)
package socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DictionaryDAO {
public DictionaryDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/android?characterEncoding=UTF-8", "root",
"admin");
}
public List<Dictionary> query(String recieve) {
List<Dictionary> ds = new ArrayList<Dictionary>();
String sql = "select * from dictionary where receive = ? ";
try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, recieve);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Dictionary d = new Dictionary();
int id = rs.getInt(1);
String receive = rs.getString("receive");
String response = rs.getString("response");
d.id = id;
d.receive = receive;
d.response = response;
ds.add(d);
}
} catch (SQLException e) {
e.printStackTrace();
}
return ds;
}
}
此處,如果不是代碼編寫問(wèn)題,出現(xiàn)jdbc錯(cuò)誤,請(qǐng)注意是否打開(kāi)MySQL數(shù)據(jù)庫(kù)。
server端
主要是正常的socket入門編程。
通過(guò)輸入流收到Client端發(fā)來(lái)的數(shù)據(jù)(打?。?,然后查詢語(yǔ)料對(duì)話庫(kù),查到返回正常對(duì)話,查不到表示聽(tīng)不懂。
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Server {
private static List<String> cannotUnderstand= new ArrayList<>();
static{
cannotUnderstand.add("聽(tīng)求不懂啊");
cannotUnderstand.add("說(shuō)人話");
cannotUnderstand.add("再說(shuō)一遍?");
cannotUnderstand.add("大聲點(diǎn)");
cannotUnderstand.add("老子在忙,一邊玩兒去");
}
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
System.out.println("監(jiān)聽(tīng)在端口號(hào):8888");
Socket s = ss.accept();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
while (true) {
String msg = dis.readUTF();
System.out.println(msg);
List<Dictionary> ds= new DictionaryDAO().query(msg);
String response = null;
if(ds.isEmpty()){
Collections.shuffle(cannotUnderstand);
response = cannotUnderstand.get(0);
}
else{
Collections.shuffle(ds);
response = ds.get(0).response;
}
dos.writeUTF(response);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client端
正常的收發(fā)信息
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
// 連接到本機(jī)的8888端口
Socket s = new Socket("127.0.0.1", 8888);
//
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
while (true) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
dos.writeUTF(str);
String msg = dis.readUTF();
System.out.println(msg);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Catcher捕獲異常的實(shí)現(xiàn)
本文主要介紹了Java使用Catcher捕獲異常的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
JAVALambda表達(dá)式與函數(shù)式接口詳解
大家好,本篇文章主要講的是JAVALambda表達(dá)式與函數(shù)式接口詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Spring中HandlerMethod類源碼詳細(xì)解析
這篇文章主要介紹了Spring中HandlerMethod類源碼詳細(xì)解析,HandlerMethod類用于封裝控制器方法信息,包含類信息、方法Method對(duì)象、參數(shù)、注解等信息,具體的接口請(qǐng)求是可以根據(jù)封裝的信息調(diào)用具體的方法來(lái)執(zhí)行業(yè)務(wù)邏輯,需要的朋友可以參考下2023-11-11
去掉IntelliJ IDEA 中 mybatis 對(duì)應(yīng)的 xml 文件警告的教程圖解
本文通過(guò)圖文并茂的形式給大家介紹了去掉IntelliJ IDEA 中 mybatis 對(duì)應(yīng)的 xml 文件警告的教程,需要的朋友可以參考下2018-06-06
BigDecimal divide除法除不盡報(bào)錯(cuò)的問(wèn)題及解決
這篇文章主要介紹了BigDecimal divide除法除不盡報(bào)錯(cuò)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot配置內(nèi)存數(shù)據(jù)庫(kù)H2教程詳解
這篇文章主要介紹了springboot配置內(nèi)存數(shù)據(jù)庫(kù)H2的詳細(xì)教程,需要的朋友可以參考下2017-07-07
springboot中的多個(gè)application文件講解
這篇文章主要介紹了springboot中的多個(gè)application文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

