jdbc 使用properties导入数据库信息

时间:2014-05-15 19:13:52   收藏:0   阅读:389

package day01;


import java.io.FileInputStream;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Properties;


public class TestClassLoad {

public static void main(String[] args) throws Exception{

InputStream in = new FileInputStream("jdbc.properties");

Properties properties = new Properties();

properties.load(in);

String driverName = properties.getProperty("driverName");

System.out.println(driverName);

Class.forName(driverName);

String url = properties.getProperty("url");

String user = properties.getProperty("user");

String password = properties.getProperty("password");

Connection con = DriverManager.getConnection(url,user,password);

Statement st = con.createStatement();

String sql = "select ename,empno,sal from emp";

ResultSet rs = st.executeQuery(sql);

while (rs.next()) {

System.out.println(rs.getString("ename")+ " "+rs.getString("empno")+" "+rs.getString("sal"));

}

rs.close();

st.close();

con.close();

}

}



//jdbc.properties

driverName=oracle.jdbc.OracleDriver

url=jdbc:oracle:thin:@localhost:1521:orcl

user=scott

password=tiger



之后只要修改properties文件就可以修改信息

jdbc 使用properties导入数据库信息,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!