java通过配置文件jdbc.properties链接Oracle数据库工具类
2017-08-09 11:05
597 查看
package resources; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; public class DbUtil { public static Connection getConnection(){ String driverClass = null; String jdbcUrl = null; String user = null; String password = null; //读取类路径下的 jdbc.properties 文件,我的配置文件放在src包下 InputStream in = DbUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); try { properties.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("username"); password = properties.getProperty("password"); Driver driver = null; try { driver = (Driver) Class.forName(driverClass).newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Properties info = new Properties(); info.put("user", user); info.put("password", password); //通过 Driver 的 connect 方法获取数据库连接. Connection connection = null; try { connection = driver.connect(jdbcUrl, info); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("----The database is connected----"); return connection; } } jdbc.properties内容如下: driver=oracle.jdbc.driver.OracleDriver #jdbcUrl=jdbc\:oracle\:thin\:@10.91.4.102\:1521\:orcl jdbcUrl=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl username=cp2 password=cp2test //调用方法 public static void main(String[] args) { String id = "111111"; String gread = "3"; List<Student> sList = new ArrayList<Student>(); Connection con = DbUtilCR.getConnection(); PreparedStatement pre = null; ResultSet result = null; String sql = "select s.name,s.age from student s where s.id=? and s.gread=?"; try { pre = con.prepareStatement(sql); pre.setString(1, id);//传参数学号 pre.setString(2, gread);//传参数年级 result = pre.executeQuery(); System.out.println("执行SQL为:["+sql+"]"); System.out.println("参数为:["+id+","+gread+"]"); while (result.next()){ Student st = new Student(); st.setName(result.getString("name"));//与查询出的字段或者别名保持一致 st.setAge(result.getString("ag 4000 e")); sList.add(st); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0;i<sList.size();i++) { System.out.println("姓名:"+sList.get(i).getName()+"\t年龄:"+sList.get(i).getAge()); } }
相关文章推荐
- JDBC连接MySQL或Oracle数据库(通过从属性文件jdbc.properties读,用Java的Property类)
- Java之数据库MYSQL、JDBC工具类、properties配置文件
- Java properties配置文件工具类
- java解析配置文件config.propertie 使用JDBC链接数据库
- Eclipse和Tomcat和Java读取配置文件工具类PropertiesUtil
- JDBC实例--通过连接工具类DBUtil +配置文件来获取连接数据库,方便又快捷
- java 数据库读取工具类(读取config.properties配置文件)
- jdbc基础 (二) 通过properties配置文件连接数据库
- jdbc基础 (二) 通过properties配置文件连接数据库
- jdbc基础 (二) 通过properties配置文件连接数据库
- Java对properties配置文件操作工具类
- 如何从properties配置文件中对去jdbc链接配置
- java通过配置属性文件连接Oracle数据库
- JAVA JDBC 读取配置文件链接数据库(oracle)
- JDBC 通过从properties配置文件获取信息得到orcale数据库接口
- java 数据库读取工具类(读取config.properties配置文件)[包含线程安全]
- java 通过 Properties 读取数据库配置 .properties 文件的使用。
- java 数据库读取工具类(读取config.properties配置文件)
- 方法:一个简单的读取配置文件.properties的工具类 JAVA
- jdbc基础 (二) 通过properties配置文件连接数据库