Java MySQL Connection
This chapter introduces how Java uses JDBC to connect to a MySQL database.
Java connecting to MySQL requires a driver package; the latest version can be downloaded from:http://dev.mysql.com/downloads/connector/j/, extract it to get the jar library file, then import the library file into the corresponding project.

You can download the jar package provided by this site:mysql-connector-java-5.1.39-bin.jar
This example uses Eclipse; to import the jar package:

Database connections for MySQL 8.0 and above are different:
1. Driver package version for MySQL 8.0 and above mysql-connector-java-8.0.16.jar.
2. com.mysql.jdbc.Driver change to com.mysql.cj.jdbc.Driver.
MySQL 8.0 and above does not require establishing an SSL connection, so it needs to be explicitly disabled.
allowPublicKeyRetrieval=true allows the client to retrieve the public key from the server.
Finally, you also need to set CST.
The way to load the driver and connect to the database is as follows:
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC","root","password");
Create test data
Next, we create the RUNOOB database in MySQL and create the websites table, with the following structure:
CREATE
TABLE
`
websites
`
(
`
id
`
int
(
11
)
NOT
NULL
AUTO_INCREMENT
,
`
name
`
char
(
20
)
NOT
NULL
DEFAULT
'
'
COMMENT
'
Site name
'
,
`
url
`
varchar
(
255
)
NOT
NULL
DEFAULT
'
'
,
`
alexa
`
int
(
11
)
NOT
NULL
DEFAULT
'
0
'
COMMENT
'
Alexa rank
'
,
`
country
`
char
(
10
)
NOT
NULL
DEFAULT
'
'
COMMENT
'
Country
'
,
PRIMARY
KEY
(
`
id
`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
10
DEFAULT
CHARSET
=
utf8
;
Insert some data:
INSERT
INTO
`
websites
`
VALUES
(
'
1
'
,
'
'
,
'
https://www.google.cm/
'
,
'
1
'
,
'
USA
'
)
,
(
'
2
'
,
'
Taobao
'
,
'
https://www.taobao.com/
'
,
'
13
'
,
'
CN
'
)
,
(
'
3
'
,
'
Runoob Tutorial
'
,
'
http://www.runoob.com
'
,
'
5892
'
,
'
'
)
,
(
'
4
'
,
'
'
,
'
http://weibo.com/
'
,
'
20
'
,
'
CN
'
)
,
(
'
5
'
,
'
'
,
'
https://www.facebook.com/
'
,
'
3
'
,
'
USA
'
)
;
The data table is displayed as follows:

Connect to the database
The following example uses JDBC to connect to a MySQL database. Note that some data such as username and password need to be configured according to your development environment:
MySQLDemo.java file code:
package
com
.
runoob
.
test
;
import
java
.
sql
.*;
public
class
MySQLDemo
{
//
MySQL below 8.0 - JDBC driver name and database URL
static
final
String
JDBC_DRIVER
=
"
com.mysql.jdbc.Driver
"
;
static
final
String
DB_URL
=
"
jdbc:mysql://localhost:3306/RUNOOB
"
;
//
MySQL 8.0 and above - JDBC driver name and database URL
//
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//
static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
//
The database username and password need to be set according to your own configuration
static
final
String
USER
=
"
root
"
;
static
final
String
PASS
=
"
123456
"
;
public
static
void
main
(
String
[
]
args
)
{
Connection
conn
=
null
;
Statement
stmt
=
null
;
try
{
//
Register the JDBC driver
Class
.
forName
(
JDBC_DRIVER
)
;
//
Open the connection
System
.
out
.
println
(
"
Connecting to database...
"
)
;
conn
=
DriverManager
.
getConnection
(
DB_URL
,
USER
,
PASS
)
;
//
Execute query
System
.
out
.
println
(
"
Instantiating Statement object...
"
)
;
stmt
=
conn
.
createStatement
(
)
;
String
sql
;
sql
=
"
SELECT id, name, url FROM websites
"
;
ResultSet
rs
=
stmt
.
executeQuery
(
sql
)
;
//
Traverse the result set database
while
(
rs
.
next
(
)
)
{
//
Retrieve by field
int
id
=
rs
.
getInt
(
"
id
"
)
;
String
name
=
rs
.
getString
(
"
name
"
)
;
String
url
=
rs
.
getString
(
"
url
"
)
;
//
Output data
System
.
out
.
(
"
ID:
"
id
)
;
System
.
out
.
(
"
, Site name:
"
name
)
;
System
.
out
.
(
"
, Site URL:
"
url
)
;
System
.
out
.
(
"
\n
"
)
;
}
//
Close after completion
rs
.
close
(
)
;
stmt
.
close
(
)
;
conn
.
close
(
)
;
}
catch
(
SQLException
se
)
{
//
Handle JDBC errors
se
.
printStackTrace
(
)
;
}
catch
(
Exception
e
)
{
//
Handle Class.forName errors
e
.
printStackTrace
(
)
;
}
finally
{
//
Close resources
try
{
if
(
stmt
!=
null
)
stmt
.
close
(
)
;
}
catch
(
SQLException
se2
)
{
}
//
Do nothing
try
{
if
(
conn
!=
null
)
conn
.
close
(
)
;
}
catch
(
SQLException
se
)
{
se
.
printStackTrace
(
)
;
}
}
System
.
out
.
println
(
"
Goodbye!
"
)
;
}
}
The execution output of the above example is as follows:
