当前位置:网站首页>The most basic JDBC template and database garbled code processing
The most basic JDBC template and database garbled code processing
2022-04-21 23:51:00 【RhineHe_ hjs】
Beginners JDBC
public class JDBCDemo1 {
public static void main(String[] args) throws Exception {
//1. The load driver
Class.forName("com.mysql.jdbc.Driver");
//2、 Get the connection object
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
//3. Access to perform SQL object
Statement statement = connection.createStatement();
//4. perform SQL sentence , And receive the results
int result = statement.executeUpdate("insert into user(userid,username,sex)values(1,' Zhang San ',' male ');");
/*// Above statement There is injection risk , The execution efficiency is not very high // The following methods can effectively avoid SQL The risk of Injection , To compile the first , Post run String sql="insert into user(userid,username,sex)values(5,' Sun Qi ',' male ')"; PreparedStatement statement = connection.prepareStatement(sql); int result =statement.executeUpdate();*/
// Processing results
if(result == 1){
System.out.println(" Registered successfully !");
}else {
System.out.println(" Registration failed !");
}
// Release resources
statement.close();
connection.close();
}
}
If the Chinese characters inserted into the database appear garbled , You can change the code above to get the connection to :
Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8”, “root”, “123456”);
explain : The problem of garbled code is not the problem of this code , But when you install the database, you choose mysql Default characters provided latin1, When using the modified character set , Even if the character set you choose when creating the database is utf-8, Use JDBC There will still be garbled code when inserting data about Chinese , You can open your own mysql Check the installation directory my.ini The configuration file : Disk character :\Program Files\MySQL\MySQL Server 5.1


Tips
The above is just my own opinion , Any similarity is purely accidental . If there is a mistake , Please understand , Don't spray ! If there is gain or doubt , Comments are welcome on thumb up !
版权声明
本文为[RhineHe_ hjs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211801130228.html
边栏推荐
- Go language learning -- Process Control II of go language foundation
- 7.10 线程条件变量
- How to make knowledge map assist multi round dialogue
- 【網絡協議】DHCP和PXE
- Classified summary of series articles (second issue)
- 7.9 线程 互斥锁
- JDBC concept steps of creating JDBC project in idea
- SolidWorks怎么设置运动行程和角度
- QT custom control 01 simple timer
- Why don't MySQL use select * as query criteria? (continuously updated)
猜你喜欢

C language for complete square

7.9 thread mutex

Click, walk and move of characters in 3D sandbox game

JDBC概念 在idea里创建JDBC项目步骤

SolidWorks hold down Ctrl and drag to copy entities

There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server

Multi table view creation problem: modify view data times 1062

ascript基础:事件流以及常用的事件属性

AVL tree deletion, detailed illustration

山洪灾害监测预警系统解决方案
随机推荐
Storage group physical quantity entity path
管理员登录及token相关
Wechat server configuration
Judging by JS in IE and edge, you can only enter numbers, letters and date types.
Solution of mountain torrent disaster monitoring and early warning system
Iotdb permission management
JDBC概念 在idea里创建JDBC项目步骤
. 100 roller events
PHP ueditor uploads files (pictures and videos) to OSS
Qt自定义控件01 简易计时器
7.4 线程退出
将node项目部署到云服务器详细教程 原创
B. Vlad and Candies
[fundamentals of interface testing] Part III differences between traditional style interfaces and restful style interfaces
【网络协议】DHCP和PXE
已有千人收藏,一起“干完”这份300页1000道面试题
JDBC concept steps of creating JDBC project in idea
音视频编解码
6、協議層次化和服務模型(重點)
【acwing】1125. 牛的旅行***(floyd)