首先导入jar包,也可以maven导入
org.eclipse.paho.client.mqttv3-1.2.0.jar
mysql-connector-java-8.0.11.jar
mqtt-client-1.13.jar
hawtbuf-1.9.jar
gson-2.2.2.jar
sqljdbc42.jar
大概就这么几个啦!
下载点这里
https://download.csdn.net/download/wuchengyayo/12002829
上代码:
public class Paho {
private static MqttClient client;
private static int TotalNum = 0;
private static int time = 0;
public static void main(String[] args) throws MqttException, InterruptedException {
MemoryPersistence memoryPersistence = new MemoryPersistence();
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("用户名");
options.setPassword("密码".toCharArray());
options.setCleanSession(true);
options.setConnectionTimeout(10);
options.setKeepAliveInterval(20);
client = new MqttClient("连接的IP地址", "随意填", memoryPersistence);
client.setCallback(mqttCallbackExtended);
client.connect(options);
}
private static MqttCallbackExtended mqttCallbackExtended = new MqttCallbackExtended() {
@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
// 收到消息
System.out.println("receive 收到消息");
//收到消息并设置返回字符串格式,有时候会出现乱码,所以格式一定要设置
String string = new String(arg1.getPayload(), "UTF-8");
//解析数据
NewDataBean fromJson = new Gson().fromJson(string, NewDataBean.class);
//以下可以进行数据库操作
... ...
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// 交付完成
System.out.println("[isComplete]:" + arg0.isComplete() + " " + arg0.getTopics());
}
@Override
public void connectionLost(Throwable arg0) {
// 连接丢失
System.out.println("connectionLost:" + arg0.getMessage());
}
@Override
public void connectComplete(boolean arg0, String arg1) {
// 连接成功,上传客户端所有订阅关系
System.out.println("connectComplete:" + arg0 + "连接地址:" + arg1);
// 订阅
int[] Qos = { 1 };
String[] topic1 = { "/recog/senscape" };
try {
client.subscribe(topic1, Qos);
System.out.println("订阅了");
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
接入数据库,我这边接入的是sqlserver,大家可以根据自身情况自行选择数据库:
public class DbHelper {
private static String DRIVER_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String URL = "jdbc:sqlserver://ip地址:1433;databaseName=数据库名";
private static String USER = "数据库用户名";
private static String PASSWORD = "数据库密码";
public Connection mConnection = null;
public PreparedStatement mPreparedStatement = null;
// 连接数据库
public DbHelper(String sql) {
getInfo();
System.out.print(sql);
try {
Class.forName(DRIVER_NAME);
mConnection = DriverManager.getConnection(URL, USER, PASSWORD);
mPreparedStatement = mConnection.prepareStatement(sql);
System.out.print("连接了");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void getInfo() {
String path = System.getProperty("user.dir") + "\\MqttConnect.properties";
try {
FileInputStream fis = new FileInputStream(path);
Properties prop = new Properties();
prop.load(fis);
URL = prop.getProperty("Url");
USER = prop.getProperty("User");
PASSWORD = prop.getProperty("Password");
System.out.print("url:" + URL + "user:" + USER + "pwd:" + PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
}
// 关闭数据库连接
public void close() {
try {
if (!(null == mConnection && mConnection.isClosed())) {
this.mConnection.close();
}
if (!(null == mPreparedStatement && mPreparedStatement.isClosed())) {
this.mPreparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
以下写了一个简单的删除数据:
public class DbBusiness {
public static void deleteAll() {
String deleteSql = "delete from 表名";
DbHelper dbHelper = new DbHelper(deleteSql);
PreparedStatement mPreparedStatement = dbHelper.mPreparedStatement;
try {
//执行
mPreparedStatement.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbHelper.close();
}
}
OK啦!