在建立了数据库基础上,进行如下操作:
1、导入 JDBC 驱动
2、注册 JDBC 驱动程序
我们在使用驱动程序之前,必须注册你的驱动程序。注册驱动程序的本质就是将我们将要使用的数据库的驱动类文件动态的加载到内存中,然后才能进行数据库。比如我们使用的 Mysql 数据库。我们可以通过以下两种方式来注册我们的驱动程序。
1、方法1——Class.forName():
动态加载一个类最常用的方法是使用Java的Class.forName()方法,通过使用这个方法来将数据库的驱动类动态加载到内存中,然后我们就可以使用。
使用Class.forName()来注册 Mysql 驱动程序:
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
2、方法2——DriverManager.registerDriver():
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
3、指定数据库连接URL
当加载了驱动程序,便可以使用 DriverManager.getConnection() 方法连接到数据库了。
这里给出 DriverManager.getConnection() 三个重载方法:
getConnection(String url)
getConnection(String url, Properties prop)
getConnection(String url, String user, String password)
数据库的URL是指向数据库地址。下表列出了下来流行的JDBC驱动程序名和数据库的URL。
RDBMS | JDBC驱动程序的名称 | URL |
---|---|---|
Mysql | com.mysql.jdbc.Driver | jdbc:mysql://hostname/ databaseName |
Oracle | oracle.jdbc.driver.OracleDriver | jdbc:oracle:thin:@hostname:port Number:databaseName |
DB2 | COM.ibm.db2.jdbc.net.DB2Driver | jdbc:db2:hostname:port Number/databaseName |
Sybase | com.sybase.jdbc.SybDriver | jdbc:sybase:Tds:hostname: port Number/databaseName |
4、创建连接对象
下面三种形式DriverManager.getConnection()方法来创建一个连接对象,以 Mysql 为例。getConnection()最常用形式要求传递一个数据库URL,用户名 username和密码 password。
1、使用数据库URL的用户名和密码
String URL = "jdbc:mysql://localhost/EXAMPLE";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS);
2、只使用一个数据库URL
然而,在这种情况下,数据库的URL,包括用户名和密码。
String URL = "jdbc:mysql://localhost/EXAMPLE?user=root&password=0909";
//Mysql URL的参数设置详细可以查阅相关资料
Connection conn = DriverManager.getConnection(URL);
3、使用数据库的URL和一个Properties对象
import java.util.*;
String URL = "jdbc:mysql://localhost/EXAMPLE";
Properties pro = new Properties( );
//Properties对象,保存一组关键字-值对
pro.put( "user", "root" );
pro.put( "password", "" );
Connection conn = DriverManager.getConnection(URL, pro);
4、关闭JDBC 连接
conn.close();
5、java 程序访问数据库
package com.shiyanlou;
import java.sql.*;
public class JdbcTest {
// JDBC 驱动器名称 和数据库地址
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//数据库的名称为 EXAMPLE
static final String DB_URL = "jdbc:mysql://localhost/EXAMPLE";
// 数据库用户和密码
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//注册JDBC 驱动程序
Class.forName("com.mysql.jdbc.Driver");
//打开连接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//执行查询
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, age FROM Students";
ResultSet rs = stmt.executeQuery(sql);
//得到和处理结果集
while(rs.next()){
//检索
int id = rs.getInt("id");
int age = rs.getInt("age");
String name = rs.getString("name");
//显示
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", Name: " + name);
System.out.println();
}
//清理环境
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// JDBC 操作错误
se.printStackTrace();
}catch(Exception e){
// Class.forName 错误
e.printStackTrace();
}finally{
//这里一般用来关闭资源的
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
3、最后我们来看看结果吧: