English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this chapter, we introduce how Java uses JDBC to connect to the MySQL database.
Java needs a driver package to connect to MySQL, and the latest version download address is:http://dev.mysql.com/downloads/connector/j/, unzip it to get the jar library file, and then import the library file into the corresponding project.
You can download the jar package provided on this site:mysql-connector-java-5.1.39-bin.jar
This example uses Eclipse, import the jar package:
MySQL 8Database connections above 8.0 version are different from the following:
1, MySQL 8Driver package version above 8.0 mysql-connector-java-8.0.16.jar.
2,com.mysql.jdbc.Driver Replace it with com.mysql.cj.jdbc.Driver.
MySQL 8Versions above 8.0 do not need to establish an SSL connection, and need to be explicitly closed.
allowPublicKeyRetrieval=true allows the client to retrieve the public key from the server.
Finally, you still 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");
Next, we create w3codebox database and create the websites data table, the table structure is as follows:
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 Ranking', `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', 'Google', 'https://www.google.cm/', ''1', 'USA'), ('2', 'Taobao', 'https://www.taobao.com/', ''13', 'CN'), ('3', 'w3codebox//www.oldtoolbag.com5892', ''), ('4', 'Weibo', 'http://weibo.com/', ''20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', ''3', 'USA');
The data table is displayed as follows:
The following example uses JDBC to connect to the MySQL database. Note that some data such as username and password need to be configured according to your development environment:
package com.w3codebox.test; import java.sql.*; public class MySQLDemo { // MySQL 8.0 The following version - JDBC Driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/w3codebox" // MySQL 80.0 and above versions - 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/w3codebox?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; // Database username and password, need to be set according to your own settings static final String USER = "root"; static final String PASS = ""123456"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // Register JDBC Driver Class.forName(JDBC_DRIVER); // Open Connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // Execute Query System.out.println("Example Statement object..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, url FROM websites"; ResultSet rs = stmt.executeQuery(sql); // Expand ResultSet Database while(rs.next()){ // Field-based Search int id = rs.getInt("id"); String name = rs.getString("name"); String url = rs.getString("url"); // Output Data System.out.print("ID: ", + id); System.out.print(", Site Name: ", + name); System.out.print(", Site URL: " + url); System.out.print("\n"); } // Close After Completion rs.close(); stmt.close(); conn.close(); } // Handle JDBC Error se.printStackTrace(); } // Handle Class.forName Error e.printStackTrace(); } // Close Resources try{ if(stmt!=null) stmt.close(); }2{ }// Do nothing try{ if(conn!=null) conn.close(); } se.printStackTrace(); } } System.out.println("Goodbye!"); } }
The execution output of the above examples is as follows: