简体   繁体   English

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

[英]How do I manually configure a DataSource in Java?

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 DataSource interface 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.编辑:我实际上是在尝试连接到 MySQL ( com.mysql.jdbc ),但我找不到相关的 javadoc。 I'll accept an answer that points me to either:我会接受一个指向我的答案:

1) documentation for com.mysql.jdbc regarding a DataSource that I can understand, or 1) com.mysql.jdbc关于我能理解的DataSource文档,或

2) gives an example to follow for what the tutorial's code should be, for any database. 2) 给出了一个例子来说明教程的代码应该是什么,对于任何数据库。

One thing you might want to look at is the Commons DBCP project.您可能想要查看的一件事是Commons DBCP项目。 It provides a BasicDataSource that is configured fairly similarly to your example.它提供了一个BasicDataSource ,其配置与您的示例非常相似。 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.要使用它,您需要在类路径中包含数据库供应商的 JDBC JAR,并且必须以正确的格式指定供应商的驱动程序类名称和数据库 URL。

Edit:编辑:

If you want to configure a BasicDataSource for MySQL, you would do something like this:如果你想为 MySQL 配置一个BasicDataSource ,你可以这样做:

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 DataSource can then use that.需要DataSource代码然后可以使用它。

Basically in JDBC most of these properties are not configurable in the API like that, rather they depend on implementation.基本上在 JDBC 中,大多数这些属性在 API 中是不可配置的,而是依赖于实现。 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:因此,您要做的是注册驱动程序,以便 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 驱动程序没有,所以如果您不确定,注册第二个驱动程序没有什么害处,它只是在内存中创建一个重复的对象。

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

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;
}

I think the example is wrong -javax.sql.DataSource doesn't have these properties either.我认为这个例子是错误的 -javax.sql.DataSource也没有这些属性。 Your DataSource needs to be of the type org.apache.derby.jdbc.ClientDataSource , which should have those properties.您的DataSource需要是org.apache.derby.jdbc.ClientDataSource类型,它应该具有这些属性。

The javadoc for DataSource you refer to is of the wrong package.您引用的 DataSource 的 javadoc 是错误的包。 You should look atjavax.sql.DataSource .你应该看看javax.sql.DataSource As you can see this is an interface.如您所见,这是一个界面。 The host and port name configuration depends on the implementation, ie the JDBC driver you are using.主机名和端口名配置取决于实现,即您使用的 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....

For postgres, the below works.对于 postgres,下面的工作。 I actually used it in integ tests.我实际上在整数测试中使用了它。 I guess there should be some more consideration for production usage.我想应该更多地考虑生产用途。

PGSimpleDataSource ds = new PGSimpleDataSource() ;  
ds.setServerName( "localhost" );  
ds.setDatabaseName( "your_db_name_here" );   
ds.setUser( "scott" );       
ds.setPassword( "tiger" );   

The class is bundled in the postgres jdbc driver.该类捆绑在 postgres jdbc 驱动程序中。

The original stackoverflow post i followed: https://stackoverflow.com/a/45091982/3877642我关注的原始stackoverflow帖子: https : //stackoverflow.com/a/45091982/3877642

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM