java程序有连接数据库_Java程序连接数据库
发布日期:2021-06-24 13:19:27 浏览次数:4 分类:技术文章

本文共 2760 字,大约阅读时间需要 9 分钟。

/**

* 了解: 利用 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);

}

转载地址:https://blog.csdn.net/weixin_33093437/article/details/114803038 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java reduce.mdn_reduce高级用法
下一篇:辽宁师范大学java_辽宁师范大学心理学院

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月09日 17时14分18秒