티스토리 뷰
출처 : https://www.mkyong.com/jdbc/connect-to-oracle-db-via-jdbc-driver-java/
Here’s an example to show you how to connect to Oracle database via a JDBC driver.
1. Download Oracle JDBC Driver
Visit Oracle website to get the Oracle JDBC driver ojdbc6.jar
or ojdbc7.jar
P.S You need to create an Oracle account (free) to download the JDBC driver.
2. Java JDBC connection example
Code snippets to connect an Oracle database via a JDBC driver.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = null;
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:SID","username","password");
connection.close();
See a complete example below :
OracleJDBCExample.java
package com.mkyong;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class OracleJDBCExample {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
3. Run it
Assume OracleJDBCExample.java
is stored in C:\jdbc-test
folder, together with the Oracle JDBC driver ojdbc7.jar
Terminal
C:\jdbc-test>javac OracleJDBCExample.java
C:\jdbc-test>java -cp c:\jdbc-test\ojdbc7.jar;c:\jdbc-test OracleJDBCExample
-------- Oracle JDBC Connection Testing ------------
Oracle JDBC Driver Registered!
You made it, take control your database now!
Done.
References
'program' 카테고리의 다른 글
사용자 브라우저 정보와 IP 정보 얻어오기 (0) | 2019.03.19 |
---|---|
Spring 에서 client ip 가져오는 법. (0) | 2019.03.19 |
echarts line chart append (0) | 2018.10.05 |
[jQuery] 라디오(radio) 버튼, 체크박스(checkbox) 선택/해제 하는 방법 (0) | 2018.09.30 |
javascript 정규식 이용 하여 replaceall 만들기 (0) | 2018.09.05 |