Reprinted from http://www.runoob.com/java/java-mysql-connect.html
In this chapter, we will show you how to use JDBC to connect to MySQL database.
A driver package is required for Java to connect to MySQL. The latest download address is: http://dev.mysql.com/downloads/connector/j/ , extract the jar library file, and then import the library file in 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:
Create test data
Next, we create the RUNOOB database in MySQL and 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 ',' rookie tutorial ',' http://www.runoob.com ',' 5892 ','), ('4 ',' Weibo ',' http://weibo.com/ ','20', 'CN'), ('5 ',' Facebook ',' https://www.facebook.com/ ','3', 'USA');
The data table is as follows:
Connect to database
The following example uses JDBC to connect to MySQL database. Note that some data such as user name and password need to be configured according to your development environment:
package com.runoob.test; import java.sql.*; public class MySQLDemo { // 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"; // The user name and password of the database should 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("com.mysql.jdbc.Driver"); // Open links System.out.println("Connect to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // Execution query System.out.println(" instantiation Statement object..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, url FROM websites"; ResultSet rs = stmt.executeQuery(sql); // Expand 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.print("ID: " + id); System.out.print(", Site name: " + name); System.out.print(", site URL: " + url); System.out.print("\n"); } // Close when done rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // Handling JDBC errors se.printStackTrace(); }catch(Exception e){ // Handling Class.forName error e.printStackTrace(); }finally{ // close resource try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// Don't do anything? try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); } }
The output results of the above example are as follows: