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

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

问题描述

我正在尝试通过 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) com.mysql.jdbc 的文档一个我能理解的 DataSource ,或者

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());

然后您形成网址:

 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中注册驱动程序的方法,然后从那里查找一个DataSource,并在其上调用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中手动配置DataSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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