如何更改连接访问的默认数据库?例如,即时通讯试图获取即时通讯连接到的SQL Server中每个表的列表:
List list = new ArrayList();
for (String db: dbList) {
conn.prepareStatement("use ["+db+"]").executeQuery();//THIS LINE ISNT WORKING D:
DatabaseMetaData md = conn.getMetaData();
rs = md.getTables(null, null, "%", null);
while (rs.next())
list.add(rs.getString(3));
}
return list;
Run Code Online (Sandbox Code Playgroud)
不起作用的行抛出此错误:
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:394)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:283)
Run Code Online (Sandbox Code Playgroud)
为每个数据库创建新连接不是一种选择。
我有完全相同的代码,只是以两种不同的方式编译。一个是 Spring 中的 maven 构建,另一个是 Jenkins 中的 maven 构建。
Spring 构建产生
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestApplication 1.0.0-BUILD-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Woof ---
[INFO] Deleting TestApplication\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Woof ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ Woof ---
[WARNING] File encoding has not been set, using platform encoding …Run Code Online (Sandbox Code Playgroud)