当前位置:网站首页>JDBC connection database
JDBC connection database
2022-07-27 16:57:00 【zhangzhang_ one】
JDBC
JDBC refer to Java Database connection , It's a standard Java Application programming interface , Each database vendor needs to implement this set of interfaces , We just need to call to use Java Connect to database .
Download the driver package
Download at :https://downloads.mysql.com/archives/c-j/
After downloading , Unzip it .
There are two types of drives : The old version :mysql-connector-java-5.1.46-bin.jar;
The new version :mysql-connector-java-8.0.11.jar.
Different versions of drivers are different when connecting to the database . The old version of the driver name is :com.mysql.jdbc.Driver; The driver name of the new version is com.mysql.cj.jdbc.Driver.
Drive configuration
(1) Create a new one under the current project lib Folder , Will drive jar The package is in this directory 
(2) Right click drive jar package , Add it to the path 
After the configuration is successful, it displays :
Connect to database test
stay MySQL Create a database in the database , And create a table to test .
The code is as follows :
package dbtest;
import java.sql.*;
public class MySQLDemo {
// Statement JDBC Driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // Set according to different version numbers
static final String DB_URL = "jdbc:mysql://localhost:3306/runoob?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=UTF-8";
// Declare the user and password of the database
static final String USER = "root";
static final String PASSWORD = "123456";
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
try{
// register JDBC drive
Class.forName(JDBC_DRIVER);
// Connect to database
System.out.println(" Connecting to database .....");
conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
// Execution insert
System.out.println(" Execution insert ......");
stmt = conn.createStatement();
//SQL Insert statement
String sql = "INSERT INTO websites VALUES ('2', ' TaoBao ', 'https://www.taobao.com/', '13', 'CN')";
stmt.execute(sql); // perform
// Close after execution
stmt.close();
conn.close();
}catch(SQLException se){
// Handle SQL abnormal
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(SQLException se2){
se2.printStackTrace();
}
}
System.out.println(" Insert a piece of data !");
}
}
Mistakes encountered :
Because the coding of the database is inconsistent , So when inserting Chinese into the database, there is garbled code .
By command show variables like ‘character%’; View the encoding of the database .
Can pass set character_set_xxxx=utf8 Order to modify , But this modification is in mysql It won't work after restarting .
Therefore, it is recommended to modify the character code mysql Configuration file for ,windows Next is my.ini file ,linux Next is my.conf file , And then restart mysql The server takes effect .
边栏推荐
猜你喜欢

C语言之指针进阶

C语言之程序环境和预处理
![[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images](/img/7d/a80f216117b647abac414fd82821da.png)
[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images

Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl

C语言之函数

Storage of data in C language

C语言之枚举和联合体

Structure and bit segment of C language

牛客题目——判断是不是完全二叉树、平衡二叉树

Clear understanding of torchvision (mind map)
随机推荐
合工大苍穹战队视觉组培训Day7——视觉,jetson naon与D435i
C语言之操作符
Cvxpy - latest issue
Gurobi——GRBEnv
Program environment and preprocessing of C language
The difference between MVC and MVP and MVVM
jsp-El表达式,JSTL标签
Apache
(二)动态卷积之Dynamic Convolution
Niuke topic -- symmetric binary tree
JSON data parsing
The class declared by the scala object keyword directly calls methods and associated objects
Servlet uses cookies to realize the last login time of users
Servlet Chinese garbled setcontenttype setting is invalid
Quadratic programming based on osqp
kubesphere多节点安装出错
Pdf extract text
(2) Dynamic convolution of dynamic convolution
C语言之字符函数和字符串函数及内存函数
Fast Planner - detailed explanation of kinetic astar