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 {

Class.forName(“oracle.jdbc.driver.OracleDriver”);
//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 =
DriverManager.getConnection(
“jdbc:oracle:thin:@orcl:1521:catinfo”,
“percy”,
“sardine”);
//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.

resultSet.close();
statement.close();
connection.close();

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

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

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 {
Class.forName(“com.mysql.jdbc.Driver”);
} 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=127.0.0.1)(protocol=tcp)(port=1521)))(connect_data=(sid= oradb)))
username=doannv
password=doannv

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

//Configuration.java

import java.util.ResourceBundle;

public class Configuration {
private ResourceBundle rb;

public Configuration() {
super();
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() {
super();
}

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){
if(conn!=null){
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

–@Developer by Doannv

Gửi phản hồ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 Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s