In this section we introduce how Java uses JDBC to connect to a MySQL database.
Java connection MySQL driver package, the latest version of the download address: http://dev.mysql.com/downloads/connector/j/ , decompression after 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 and imports the jar package:
Create test data
Next we create the RUNOOB database in MySQL 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', 'The rookie tutorial', 'http://www.interviewbubble.com', '5892', 'IN'), ('4', 'Weibo', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
Connect to the database
The following example uses JDBC to connect to a MySQL database. Pay attention to some data such as user name. The password needs to be configured according to your development environment:
MySQLDemo.java file code:
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/bubble " ;
// The database username and password need to be set
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 the link
System . out . println ( " Connect to database... " ) ;
conn = DriverManager . getConnection ( DB_URL , USER , PASS ) ;
// Execute the query
System . out . println ( " Instantiate the 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
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 rs after completion
rs. close ( ) ;
stmt . close ( ) ;
conn . close ( ) ;
} catch ( SQLException se ) {
// Handle JDBC error
se . printStackTrace ( ) ;
} Catch ( Exception e ) {
// Handle Class . forName error
e . printStackTrace ( ) ;
}finally { // Close resource
try {
if ( stmt != null )
stmt . close ( ) ;
} catch ( SQLException se2 ) {
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
Comments
Post a Comment