java程序有连接数据库_Java程序连接数据库

/**

* 了解: 利用 Driver 接口的 connect 方法获取连接

*/

// 第一种实现

/**

* 了解: 利用 Driver 接口的 connect 方法获取连接

*/

@Test

public void oracleJdbcTest() throws Exception {

Driver driver = null; // sun提供的接口

String url = "jdbc:oracle:thin:@192.168.5.139:1521:ORCL";

Properties info = null;

info = new Properties();

info.put("user", "scott");

info.put("password", "tiger");

driver = new OracleDriver(); // Oracle数据库厂商自己实现sun提供的接口Driver

Connection connect = driver.connect(url, info); // 获取连接

System.out.println(connect);

}

======================================

/**

* 了解: 使用 DriverManager 来获取数据库连接

* 版本1:

* 好处: 不需要使用原生的 Driver 方法来获取连接.

* 缺点: 还是耦合了具体的实现类.

*/

//第二种实现

@Test

public void oracleJdbcTest1() throws Exception{

Connection connection=null;

DriverManager.registerDriver(new OracleDriver()); //驱动管理器注册Oracle驱动,实现Java程序可以连接Oracle数据库,如果想要连接不同的数据库则需要注册不同的数据库驱动

String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";

Properties info=null;

info=new Properties();

info.put("user", "scott");

info.put("password", "tiger");

connection=DriverManager.getConnection( url, info) ; //DriverManager驱动管理器类,里面的方法都是静态的,类调用获取到一个连接

System.out.println(connection);

}

====================================

/**

* 了解: 更进一步, 不需要关联任何 JDBC 驱动的实现类。

* 但需要提供 JDBC 驱动中 Driver 接口的实现类的全类名的字符串.

*/

//第三种实现

@Test

public void oracleJdbcTest2() throws Exception, InstantiationException, IllegalAccessException, ClassNotFoundException{

Connection connection=null;

String className="oracle.jdbc.driver.OracleDriver";

DriverManager.registerDriver((Driver)Class.forName(className).newInstance()); //注册驱动

String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";

Properties info=null;

info=new Properties();

info.put("user", "scott");

info.put("password", "tiger");

connection=DriverManager.getConnection(url,info);

System.out.println(connection);

}

===========================================================

/**

* 能创建一个不和具体 Driver 耦合的获取数据库连接的方法. 即在方法中不再关联任何数据库驱动的 JDBC 实现.

*@throws SQLException

*/

/**

* final version: 若需要手动获取数据库连接:

*

* 更进一步, 不需要关联任何 JDBC 驱动的实现类。

* 但需要提供 JDBC 驱动中 Driver 接口的实现类的全类名的字符串.

*

* 实际上, 在驱动的实现类中有一个静态代码块: 创建了 Driver 实现类的对象, 并把其注册给 DriverManager

* static {

* try {

* java.sql.DriverManager.registerDriver(new Driver());

* } catch (SQLException E) {

* throw new RuntimeException("Can't register driver!");

* }

* }

*

* 而调用 Class 的 forName 方法在加载类实例时, 会调用静态代码块.

*

*/

//第四种实现(最常用)

@Test

public void oracleJdbcTest3() throws Exception{

Connection connection=null;

String className="oracle.jdbc.driver.OracleDriver";

String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";

Properties info=null;

info=new Properties();

info.put("user", "scott");

info.put("password", "tiger");

Class.forName(className).newInstance();//加载驱动

connection=DriverManager.getConnection(url,info);

System.out.println(connection);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值