如何在 Java 中手动配置数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1336885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:23:33  来源:igfitidea点击:

How do I manually configure a DataSource in Java?

javajdbcdatasource

提问by Eric Wilson

I'm trying to follow Sun's JDBC tutorial at http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html

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

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

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

这段代码不能编译,因为DataSource接口没有这些方法,除了getConnection()最后调用的方法。

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

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

What am I missing?

我错过了什么?

Edit: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:

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

1) documentation for com.mysql.jdbcregarding a DataSourcethat I can understand, or

1)我能理解的com.mysql.jdbc关于 a 的文件DataSource,或

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

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

采纳答案by Yishai

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 中,大多数这些属性在 API 中是不可配置的,而是依赖于实现。JDBC 处理此问题的方式是允许每个供应商的连接 URL 不同。

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

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

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

Then you form the URL:

然后形成 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);

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.

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

In terms of what properties MySQL supports, see here.

关于 MySQL 支持的属性,请参见此处

EDIT: 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.

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

回答by Kees de Kooter

The javadoc for DataSource you refer to is of the wrong package. You should look at javax.sql.DataSource. As you can see this is an interface. The host and port name configuration depends on the implementation, i.e. the JDBC driver you are using.

您引用的 DataSource 的 javadoc 是错误的包。你应该看看javax.sql.DataSource。如您所见,这是一个界面。主机名和端口名配置取决于实现,即您使用的 JDBC 驱动程序。

I have not checked the Derby javadocs but I suppose the code should compile like this:

我没有检查过 Derby javadocs,但我想代码应该像这样编译:

ClientDataSource ds = org.apache.derby.jdbc.ClientDataSource()
ds.setHost etc....

回答by Vinay Sajip

I think the example is wrong - javax.sql.DataSourcedoesn't have these properties either. Your DataSourceneeds to be of the type org.apache.derby.jdbc.ClientDataSource, which shouldhave those properties.

我认为这个例子是错误的——javax.sql.DataSource也没有这些属性。你DataSource需要在类型org.apache.derby.jdbc.ClientDataSource,它应该具有那些特性。

回答by ColinD

One thing you might want to look at is the Commons DBCPproject. It provides a BasicDataSourcethat is configured fairly similarly to your example. To use that you need the database vendor's JDBC JAR in your classpath and you have to specify the vendor's driver class name and the database URL in the proper format.

您可能想要查看的一件事是Commons DBCP项目。它提供了一个BasicDataSource,其配置与您的示例非常相似。要使用它,您需要在类路径中包含数据库供应商的 JDBC JAR,并且必须以正确的格式指定供应商的驱动程序类名称和数据库 URL。

Edit:

编辑:

If you want to configure a BasicDataSourcefor MySQL, you would do something like this:

如果你想BasicDataSource为 MySQL配置一个,你会做这样的事情:

BasicDataSource dataSource = new BasicDataSource();

dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername("username");
dataSource.setPassword("password");
dataSource.setUrl("jdbc:mysql://<host>:<port>/<database>");
dataSource.setMaxActive(10);
dataSource.setMaxIdle(5);
dataSource.setInitialSize(5);
dataSource.setValidationQuery("SELECT 1");

Code that needs a DataSourcecan then use that.

需要 a 的代码DataSource然后可以使用它。

回答by Luke

DataSource is vendor-specific, for MySql you could use MysqlDataSource which is provided in the MySql Java connector jar:

DataSource 是特定于供应商的,对于 MySql,您可以使用 MySql Java 连接器 jar 中提供的 MysqlDataSource:

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setDatabaseName("xyz");
    dataSource.setUser("xyz");
    dataSource.setPassword("xyz");
    dataSource.setServerName("xyz.yourdomain.com");

回答by Fujian lin

use MYSQL as Example: 1) use database connection pools: for Example: Apache Commons DBCP , also, you need basicDataSource jar package in your classpath

以 MYSQL 为例:1)使用数据库连接池:例如:Apache Commons DBCP ,此外,您的类路径中需要 basicDataSource jar 包

@Bean
public BasicDataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/gene");
    ds.setUsername("root");
    ds.setPassword("root");
    return ds;
}

2)use JDBC-based Driver it is usually used if you don't consider connection pool:

2)使用基于JDBC的驱动程序,如果不考虑连接池,通常使用:

@Bean
public DataSource dataSource(){
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/gene");
    ds.setUsername("root");
    ds.setPassword("root");
    return ds;
}