jdbc 使用properties导入数据库信息

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导入数据库信息,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。