Java JDBC链接Oracle数据库

2018-02-02 08:27:00来源:cnblogs.com作者:IT工程师人点击

分享
第七城市

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();
}
}


第七城市

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台