如何在 Java 中手动配置数据源? [英] How do I manually configure a DataSource in Java?

查看:29
本文介绍了如何在 Java 中手动配置数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 http 上学习 Sun 的 JDBC 教程://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html

它给出了以下示例代码:

It gives the following example code:

DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource()
ds.setPort(1527);
ds.setHost("localhost");
ds.setUser("APP")
ds.setPassword("APP");

Connection con = ds.getConnection();

此代码无法编译,因为 DataSource 接口没有这些方法,除了最后调用的 getConnection() 方法.

This code doesn't compile because the DataSource interface has none of these methods, except for the getConnection() method invoked last.

(这里是 javadoc:http://java.sun.com/javase/6/docs/api/javax/sql/DataSource.html)

(Here's the javadoc: http://java.sun.com/javase/6/docs/api/javax/sql/DataSource.html)

我错过了什么?

我实际上正在尝试连接到 MySQL (com.mysql.jdbc),但我找不到相关的 javadoc.我会接受一个指向我的答案:

I'm actually trying to connect to MySQL (com.mysql.jdbc) and I can't find the javadoc for that. I'll accept an answer that points me to either:

1) 关于 DataSourcecom.mysql.jdbc 文档,我可以理解,或者

1) documentation for com.mysql.jdbc regarding a DataSource that I can understand, or

2) 给出了一个例子来说明教程的代码应该是什么,对于任何数据库.

2) gives an example to follow for what the tutorial's code should be, for any database.

推荐答案

基本上在 JDBC 中,大多数这些属性在 API 中是不可配置的,而是依赖于实现.JDBC 处理此问题的方式是允许每个供应商的连接 URL 不同.

Basically in JDBC most of these properties are not configurable in the API like that, rather they depend on implementation. The way JDBC handles this is by allowing the connection URL to be different per vendor.

所以你要做的是注册驱动程序,以便JDBC系统知道如何处理URL:

So what you do is register the driver so that the JDBC system can know what to do with the URL:

 DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance());

然后形成 URL:

 String url = "jdbc:mysql://[host][,failoverhost...][:port]/[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]"

最后,使用它来获取连接:

And finally, use it to get a connection:

 Connection c = DriverManager.getConnection(url);

在更复杂的 JDBC 中,您会涉及到连接池等,并且应用服务器通常有自己的方式在 JNDI 中注册驱动程序,您可以从那里查找数据源,并在其上调用 getConnection.

In more sophisticated JDBC, you get involved with connection pools and the like, and application servers often have their own way of registering drivers in JNDI and you look up a DataSource from there, and call getConnection on it.

关于 MySQL 支持哪些属性,请参见 此处.

In terms of what properties MySQL supports, see here.

再想一想,从技术上讲,只要有一行执行 Class.forName("com.mysql.jdbc.Driver") 的代码就足够了,因为该类应该有自己的静态初始化程序来注册一个版本,但有时 JDBC 驱动程序没有,所以如果您不确定,注册第二个驱动程序没有什么害处,它只会在内存中创建一个重复的对象.

One more thought, technically just having a line of code which does Class.forName("com.mysql.jdbc.Driver") should be enough, as the class should have its own static initializer which registers a version, but sometimes a JDBC driver doesn't, so if you aren't sure, there is little harm in registering a second one, it just creates a duplicate object in memeory.

这篇关于如何在 Java 中手动配置数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆