jdbc连接oracle数据库

 
 
/**
* 通过改变配置文件来连接不同数据库
*/
package
com.xykj.jdbc; import static org.junit.Assert.*; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; import java.util.Properties; import org.junit.Test; public class JDBCTest0 { public Connection getConnection() throws Exception{ String driverclass = null; String jdbcUrl = null; String user = null; String password = null; InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverclass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver = (Driver)Class.forName(driverclass).newInstance(); Properties info = new Properties(); info.put("user",user); info.put("password", password); Connection connection = driver.connect(jdbcUrl, info); return connection; } @Test public void testGetConnection() throws Exception{ System.out.println(getConnection()); } }
 
 

 

/***  jdbc连接oracle数据库**/  
1
package com.xykj.jdbc; 2 3 import static org.junit.Assert.*; 4 import java.sql.*; 5 import java.util.Properties; 6 7 import org.junit.Test; 8 9 public class JDBCTest { 10 11 /** 12 * Driver是一个接口:数据库厂商必须提供实现的接口,能从其中获取数据库连接。 13 * 1.加入oracle驱动 14 * 1>新建lib目录,复制粘贴jar包放入lib。 15 * 2>右键jar包,build path,add 加入到类路径下。 16 */ 17 @Test 18 public void testDriver() { 19 ResultSet res=null; //创建一个结果集对象 20 PreparedStatement pre = null; //创建预编译语句对象,一般都是用这个而不用Statement 21 Connection connection = null; //创建一个数据库连接 22 try 23 { 24 25 //1.创建一个Driver实现类的对象 26 Driver driver = new oracle.jdbc.driver.OracleDriver(); //加载Oracle驱动程序 27 28 //2.准备连接数据库的基本信息:url,user,password 29 String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; 30 Properties info = new Properties(); 31 info.put("user", "system"); 32 info.put("password", "sys"); 33 34 //3.调用Driver接口的connect(url,info)获取数据库连接 35 connection = driver.connect(url, info); //获取连接 36 System.out.println(connection); 37 System.out.println("数据库连接成功!"); 38 39 //4.对数据库进行操作 40 String sql = "select * from Stu where Name = ?"; //预编译语句,?代表参数 41 pre = connection.prepareStatement(sql); //实例化预编译语句 42 pre.setString(1, "张三"); // 设置参数,前面的1表示参数的索引,而不是表中列名的索引 43 res = pre.executeQuery(); //执行查询 44 while(res.next()) 45 46 System.out.println("姓名:"+res.getString("name") 47 + "性别:"+res.getString("sex") 48 + "年龄:"+res.getString("age")); 49 } 50 catch (Exception e ) 51 { 52 e.printStackTrace(); 53 } 54 finally 55 { 56 try 57 { 58 // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源 59 // 注意关闭的顺序,最后使用的最先关闭 60 if(res != null) 61 res.close(); 62 if(pre != null) 63 pre.close(); 64 if(connection != null) 65 connection.close(); 66 System.out.println("数据库连接已关闭!"); 67 } 68 catch(Exception e){ 69 e.printStackTrace(); 70 } 71 } 72 } 73 }

 

转载于:https://www.cnblogs.com/Crysta1/p/4630680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值