欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java JDBC链接Oracle数据库

程序员文章站 2022-07-02 16:51:32
package com.test.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;impor ......

package com.test.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
* 通过Java JDBC链接Oracle数据库
* @author Administrator
*
*/
public class OracleJdbcTest {
String driverClass = "oracle.jdbc.driver.OracleDriver";

Connection con;

public void init(FileInputStream fs) throws ClassNotFoundException,
SQLException, FileNotFoundException, IOException {
Properties props = new Properties();
props.load(fs);
String url = props.getProperty("db.url");
String userName = props.getProperty("db.user");
String password = props.getProperty("db.password");
Class.forName(driverClass);
con = DriverManager.getConnection(url, userName, password);
}

public void fetch() throws SQLException, IOException {
PreparedStatement ps = con.prepareStatement("select SYSDATE from dual");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// do the thing you do
}
rs.close();
ps.close();
}

public static void main(String[] args) throws ClassNotFoundException,
FileNotFoundException, SQLException, IOException {
OracleJdbcTest test = new OracleJdbcTest();
//配置文件路径
FileInputStream fs = null;
test.init(fs);
test.fetch();
}
}