Sử dụng JDBC kết nối và lấy dữ liệu từ cơ sở dữ liệu oracle

import java.sql.*;

class Get_CatsFavoriteFood {
public static void main(String[] args) {

try {

//loads the driver class
//an alternative is to use:
//  DriverManager.registerDriver(
//    new oracle.jdbc.driver.OracleDriver());
//  which creates a new instance of the
//    Oracle driver and registers it

Connection connection =
//connects to the Oracle catinfo database at orcl,
//  logs in as “percy” with a password of “sardine”

Statement statement = connection.createStatement();
//this is what we use to tie our connection to our SQL

ResultSet resultSet = statement.executeQuery(
“Select catname, favoritefood from favoritefoods”);
//now run the SQL

while (resultSet.next()) {
System.out.println(resultSet.getString(1) +
“‘s favorite food is ” + resultSet.getString(2));
//traverse the ResultSet,
//The cat names are in the first column, and the
//  favorite foods are in the second column.
//note: the subscript of the first column is one, not zero.


} catch(java.lang.ClassNotFoundException cnfException) {
“The following error occured ” +
“in finding the Oracle driver: ” +

} catch(SQLException sqlException) {
“The following error occured in ” +
“reading from the favoritefoods table: ” +

trong file class bạn cần kết nối tới csdl bạn sử dụng :

DBConnection dbc = new DBConnection();
Connection connection = dbc.getConnection();

in a supporting class somewhere:

import java.sql.Connection;
import java.sql.DriverManager;

private Connection connection = null;

public DBConnection() {
//register the driver
try {
} catch (Exception ex) {
System.out.println(“Exception getting driver”);
System.out.println(“Exception: ” + ex);

public Connection getConnection() {
if (null == connection) {connect();}

return connection;

private void connect() {

try {

connection = DriverManager.getConnection(“jdbc:mysql://localhost/YOURDBNAME?user=YOURUSERNAME&password=YOURPASSWORD”);

} catch (java.sql.SQLException ex) {
// handle any errors
System.out.println(“SQLException: ” + ex.getMessage());
System.out.println(“SQLState: ” + ex.getSQLState());
System.out.println(“VendorError: ” + ex.getErrorCode());


Ngoài ra các bạn có thể sử dụng cách này:

+ Tạo 1 file jdbc.properties :

jdbc=jdbc:oracle:thin:@(description=(address_list= (address=(host= oradb)))

+ Tạo 1 file configuration để lấy các thông tin :


import java.util.ResourceBundle;

public class Configuration {
private ResourceBundle rb;

public Configuration() {
rb = ResourceBundle.getBundle(“jdbc”);

public String getConnectionString(){
return rb.getString(“jdbc”);

public String getUsername(){
return rb.getString(“username”);

public String getPassword(){
return rb.getString(“password”);

+ Tạo 1 bean để kết nối tới CSDL Oracle

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class GenericDAO {
public GenericDAO() {

public Connection getConnection() throws SQLException {
Connection conn = null;
Configuration config = new Configuration();
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

conn = DriverManager.getConnection(config.getConnectionString(), config.getUsername(), config.getPassword());

return conn;

public void releaseConnection(Connection conn){
try {
conn = null;
} catch (SQLException e) {


–@Developer by Doannv

Trả lời

Please log in using one of these methods to post your comment:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s