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

jdbc 使用properties导入数据库信息

时间:2014-05-15 19:13:52      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:package   public   import   数据库   信息 jdbc   

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

jdbc 使用properties导入数据库信息

标签:package   public   import   数据库   信息 jdbc   

原文地址:http://nobelking.blog.51cto.com/7607139/1411809

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