码迷,mamicode.com
首页 > 数据库 > 详细

Java JDBC链接Oracle数据库

时间:2018-02-02 00:42:12      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:ati   lex   null   statement   str   nec   put   input   select   

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


 

Java JDBC链接Oracle数据库

标签:ati   lex   null   statement   str   nec   put   input   select   

原文地址:https://www.cnblogs.com/personal-blog/p/8401654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!