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文件就可以修改信息