Java 尽管在查询中指定了数据库选择异常,但未发生该异常
我有一个类似的问题Java 尽管在查询中指定了数据库选择异常,但未发生该异常,java,mysql,jdbc,Java,Mysql,Jdbc,我有一个类似的问题 SELECT * FROM dbName.dbTable 我正在使用prepared语句执行查询 以下是HicariCP配置设置: hconfig.setDriverClassName("com.mysql.jdbc.Driver"); hconfig.setJdbcUrl("jdbc:mysql://localhost/?useUnicode=true&characterEncoding=UTF8&cachePrepStmts=tr
SELECT * FROM dbName.dbTable
我正在使用prepared语句执行查询
以下是HicariCP配置设置:
hconfig.setDriverClassName("com.mysql.jdbc.Driver");
hconfig.setJdbcUrl("jdbc:mysql://localhost/?useUnicode=true&characterEncoding=UTF8&cachePrepStmts=true&prepStmtCacheSize=250&useServerPrepStmts=true&rewriteBatchedStatements=true&continueBatchOnError=false&prepStmtCacheSqlLimit=2048");
hconfig.setUsername("user");
hconfig.setPassword("passwd");
hconfig.setMaximumPoolSize(10);
hconfig.setConnectionTimeout(60000);
HikariDataSource hikariDataSource= new HikariDataSource(hconfig);
客户端使用:
Client client;
client.executePreparedQuery(
"SELECT * FROM dbName.dbTable",
null,
new ResultSetBinder() {
@Override
public void bind(ResultSet resultSet) throws SQLException {
// binding the result goes here
}
});
客户端类的相关方法(ExecutePrepareQuery):
Connection con = hikariDataSource.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = null;
try {
if (psb != null) {
psb.bind(ps);
}
rs = ps.executeQuery();
while (rs.next()) {
if (rsb != null) {
rsb.bind(rs);
}
}
} finally {
close(rs, ps, con);
}
尽管要使用的数据库在查询本身中是专用的,但代码有时(并非总是!!!)会抛出java.sql.SQLException:未选择任何数据库。
MySQL5.6.22
与HicariCP
一起使用
原因可能是什么?HicariCP配置设置: add=>hconfig.addDataSourceProperty(“databaseName”、“yourDatabaseName”)强>
hconfig.setDriverClassName("com.mysql.jdbc.Driver");
hconfig.setJdbcUrl("jdbc:mysql://localhost/?useUnicode=true&characterEncoding=UTF8&cachePrepStmts=true&prepStmtCacheSize=250&useServerPrepStmts=true&rewriteBatchedStatements=true&continueBatchOnError=false&prepStmtCacheSqlLimit=2048");
hconfig.setUsername("user");
hconfig.setPassword("passwd");
hconfig.setMaximumPoolSize(10);
hconfig.setConnectionTimeout(60000);
hconfig.addDataSourceProperty("databaseName", "yourDatabaseName");
HikariDataSource hikariDataSource= new HikariDataSource(hconfig);
客户端使用:
Client client;
client.executePreparedQuery(
"SELECT * FROM dbName.dbTable",
null,
new ResultSetBinder() {
@Override
public void bind(ResultSet resultSet) throws SQLException {
// binding the result goes here
}
});
chnage to=>“从数据库表中选择*”
希望能有所帮助。请发布导致问题的代码。我们没有一个魔法球可以知道你在做什么;)最有可能的情况是,即使在查询中指定了一个默认数据库,它也会要求您设置一个默认数据库。并发布Hikari配置信息。我无法从查询中删除dbName,因为我使用客户端代码查询多个数据库。也许我应该用@PeterLawrey suggestedI see设置一个默认数据库,并在查询中包含dbname。设置默认数据库可能会有所帮助。