51,407
社区成员
发帖
与我相关
我的任务
分享
。如果您对这方面了解的话可以加qq细说一下感谢:954939176
可以仔细说下嘛,我刚了解这方面不是很会,可以的话可以加qq细说:954939176
感谢感谢,麻烦了。
import com.google.gson.Gson;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public static void main(String[] args) {
try {
/** 建表语句
* CREATE TABLE `test` (
* `id` int NOT NULL AUTO_INCREMENT,
* `json_text` text COLLATE utf8mb4_bin,
* PRIMARY KEY (`id`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
*/
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/ssy";//如果是其他数据库,只需要更改MySQL 为其他数据库即可
String user = "root";
String password="xxx";
Connection connection= DriverManager.getConnection(url,user,password);
String sql="INSERT INTO `test`(json_text) VALUES(?)";
PreparedStatement ps=connection.prepareStatement(sql);
Map<String,String> map=new HashMap<>();
map.put("id","1");
map.put("name","sunsy");
ps.setString(1,new Gson().toJson(map));
ps.execute();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}