我试图从MySQL数据库生成代码,我通过SSL连接。我从设置数据库的人那里得到了3个文件(client-cert.pem,client-key.pem,ca-cert.pem)
要用Java连接到数据库,我首先必须解决证书的另一个问题。在http://bugs.mysql.com/bug.php?id=68957的帮助和" [2013年4月17日9:04] Alexander Soklakov" 的回答中,我谨慎地让JDBC连接按预期工作我的Java代码。
但是现在我不知道在JOOQs library.xml中配置SSL的位置。我在哪里提供* .pem文件?或者我创造的信任 - &密钥库?如果没有配置SSL,我会
Jun 11, 2014 1:53:40 PM org.jooq.tools.JooqLogger info
Information: Initialising properties : ./library.xml
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
javax.net.ssl.SSLException
MESSAGE: Unsupported record version Unknown-0.0
STACKTRACE:
javax.net.ssl.SSLException: Unsupported record version Unknown-0.0
at sun.security.ssl.InputRecord.readV3Record(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:72)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1222)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.jooq.util.GenerationTool.run(GenerationTool.java:206)
at org.jooq.util.GenerationTool.main(GenerationTool.java:173)
at org.jooq.util.GenerationTool.main(GenerationTool.java:160)
** END NESTED EXCEPTION **
Last packet sent to the server was 396 ms ago.
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:87)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1222)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.jooq.util.GenerationTool.run(GenerationTool.java:206)
at org.jooq.util.GenerationTool.main(GenerationTool.java:173)
at org.jooq.util.GenerationTool.main(GenerationTool.java:160)
Jun 11, 2014 1:53:41 PM org.jooq.tools.JooqLogger error
Schwerwiegend: Cannot read ./library.xml. Error : Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
javax.net.ssl.SSLException
MESSAGE: Unsupported record version Unknown-0.0
STACKTRACE:
javax.net.ssl.SSLException: Unsupported record version Unknown-0.0
at sun.security.ssl.InputRecord.readV3Record(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:72)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1222)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.jooq.util.GenerationTool.run(GenerationTool.java:206)
at org.jooq.util.GenerationTool.main(GenerationTool.java:173)
at org.jooq.util.GenerationTool.main(GenerationTool.java:160)
** END NESTED EXCEPTION **
Last packet sent to the server was 396 ms ago.
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
javax.net.ssl.SSLException
MESSAGE: Unsupported record version Unknown-0.0
STACKTRACE:
javax.net.ssl.SSLException: Unsupported record version Unknown-0.0
at sun.security.ssl.InputRecord.readV3Record(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:72)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1222)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.jooq.util.GenerationTool.run(GenerationTool.java:206)
at org.jooq.util.GenerationTool.main(GenerationTool.java:173)
at org.jooq.util.GenerationTool.main(GenerationTool.java:160)
** END NESTED EXCEPTION **
Last packet sent to the server was 396 ms ago.
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:87)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1222)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.jooq.util.GenerationTool.run(GenerationTool.java:206)
at org.jooq.util.GenerationTool.main(GenerationTool.java:173)
at org.jooq.util.GenerationTool.main(GenerationTool.java:160)
Jun 11, 2014 1:53:41 PM org.jooq.tools.JooqLogger error
Schwerwiegend: Usage : GenerationTool <configuration-file>
我的library.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-2.5.0.xsd">
<!-- Configure the database connection here -->
<jdbc>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://server:3306/dbname?useSSL=true&requireSSL=true&verifyServerCertificate=false;</url>
<user>user</user>
<password>pass</password>
</jdbc>
<generator>
<!-- The default code generator. You can override this one, to generate your own code style
Defaults to org.jooq.util.DefaultGenerator -->
<name>org.jooq.util.DefaultGenerator</name>
<database>
<!-- The database type. The format here is:
org.util.[database].[database]Database -->
<name>org.jooq.util.mysql.MySQLDatabase</name>
<!-- The database schema (or in the absence of schema support, in your RDBMS this
can be the owner, user, database name) to be generated -->
<inputSchema>dbname</inputSchema>
<!-- All elements that are generated from your schema (several Java regular expressions, separated by comma)
Watch out for case-sensitivity. Depending on your database, this might be important! -->
<includes>smv_.*</includes>
<!-- All elements that are excluded from your schema (several Java regular expressions, separated by comma).
Excludes match before includes -->
<excludes></excludes>
</database>
<target>
<!-- The destination package of your generated classes (within the destination directory) -->
<packageName>my.package</packageName>
<!-- The destination directory of your generated classes -->
<directory>./src</directory>
</target>
</generator>
</configuration>
当我查看http://www.jooq.org/xsd/jooq-codegen-2.5.0.xsd
下的架构时,我也找不到任何提示现在看来我不得不放弃使用JOOQ :(
编辑:我使用Eclipse生成JOOQ代码,如下所示:http://www.jooq.org/doc/3.0/manual/code-generation/codegen-configuration/在&#34;运行Eclipse生成代码&#34;。
答案 0 :(得分:1)
如果您需要一种更特殊的方式来设置数据库连接,那么正确的方法可能是使用编程代码生成配置。这在此处记录:
http://www.jooq.org/doc/latest/manual/code-generation/codegen-programmatic/
我们(遗憾的是)尚未明确记录这一点,但您可以通过调用GenerationTool
setConnection()
答案 1 :(得分:1)
解决方案,基于Lukas Eder的接受答案
在creating the truststore and keystore之后(向下滚动到[2013年4月17日9:04] Alexander Soklakov),我可以使用以下类生成代码,在那里我获得SSL数据库连接并将其用于JOOQ Generator:
import java.sql.Connection;
import org.jooq.util.GenerationTool;
import org.jooq.util.jaxb.Configuration;
import org.jooq.util.jaxb.Database;
import org.jooq.util.jaxb.Generator;
import org.jooq.util.jaxb.Target;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class JooqGenerator {
public static void main(String[] args) throws Exception {
MysqlDataSource mysqlDS = new MysqlDataSource();
mysqlDS.setUseSSL(true);
mysqlDS.setRequireSSL(true);
mysqlDS.setClientCertificateKeyStoreUrl(".../keystore");
mysqlDS.setClientCertificateKeyStorePassword("keystore-pw");
mysqlDS.setTrustCertificateKeyStoreUrl(".../truststore");
mysqlDS.setTrustCertificateKeyStorePassword("truststore-pw");
mysqlDS.setServerName("server");
mysqlDS.setPort(3306);
mysqlDS.setDatabaseName("databasename");
Connection connection = mysqlDS.getConnection("user", "password");
Configuration configuration = new Configuration()
.withGenerator(new Generator()
.withName("org.jooq.util.DefaultGenerator")
.withDatabase(new Database()
.withName("org.jooq.util.mysql.MySQLDatabase")
.withIncludes(".*")
.withExcludes("")
.withInputSchema("databasename"))
.withTarget(new Target()
.withPackageName("my.package.name")
.withDirectory("./src")));
GenerationTool tool = new GenerationTool();
tool.setConnection(connection);
tool.run(configuration);
}
}