这个例子主要介绍jdbc连接mysql数据库。
step 1: 下载Oracle JDBC Driver
下载地址:
step 2: java JDBC连接oracle实例
jdbc连接oracle的代码:
- Class.forName("oracle.jdbc.driver.OracleDriver");
-
Connection connection = null;
-
connection = DriverManager.getConnection(
-
"jdbc:oracle:thin:@localhost:1521:orcl","username","password");
-
connection.close();
查看一个完整的实例:
(记得引入step 1 下载的driver: ojdbc***.jar)
文件: JDBCConnectOracle.java
- package org.hnrsc.jdbc;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.SQLException;
-
-
public class JDBCConnectOracle {
-
public static void main(String[] args) {
-
System.out.println("-------- Oracle JDBC Connection Testing ------------");
-
try{
-
Class.forName("oracle.jdbc.driver.OracleDriver");
-
}catch(ClassNotFoundException e){
-
System.out.println("Can't find the Oracle JDBC Driver");
-
e.printStackTrace();
-
return;
-
}
-
-
System.out.println("Oracle JDBC Driver Registered!");
-
Connection conn = null;
-
-
try{
-
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "pass");
-
}catch(SQLException e){
-
System.out.println("Connection Failed! Check output console");
-
e.printStackTrace();
-
return;
-
}
-
-
if (conn != null) {
-
System.out.println("You made it, take control your database now!");
-
} else {
-
System.out.println("Failed to make connection!");
-
}
-
}
-
-
}
step 3:运行
你可能会看到以下结果:
- -------- Oracle JDBC Connection Testing ------------
-
Oracle JDBC Driver
-
You made it, take control your database
阅读(822) | 评论(0) | 转发(0) |