在 getTables() 方法中的何处指定数据库名称。
Where do I specify the database name in the getTables() method.
getTables() 方法未返回数据库中存在的 table 的任何结果。我认为问题在于不知道在 ip 上查看哪个数据库。当我有多个具有相同 table 的数据库时,如何指定 getTables() 方法应该查找的数据库?
//concatinating strings for IP to hit
String ArchiveDB_URL = "jdbc:sqlserver://" + getArchiveIPaddress();
// Database credentials
String USER = "Removed";
String PASS = "Removed";
Connection archiveConn = null;
Statement archiveStmt = null;
try {
// Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Open a connection
archiveConn = DriverManager.getConnection(ArchiveDB_URL, USER, PASS);
DatabaseMetaData dbm = archiveConn.getMetaData();
ResultSet rsTBmd = dbm.getTables(null,null, "[myDatabase].[dbo].[myTable]", new String[] {"TABLE"});
System.out.println(rsTBmd.getString("TABLE_NAME"));
if (rsTBmd.next()){
//result sets start at 1 not 0
int rsTBmdi = 1;
while(rsTBmd.next()){
System.out.println(rsTBmd.getString(rsTBmdi));
rsTBmdi++;
}
}
尝试在 ArchiveDB_URL
中添加数据库名称
String ArchiveDB_URL = "jdbc:sqlserver://" + getArchiveIPaddress() + "/yourDatabaseNameHere";
getTables 的前两个参数是 catalog
和 schemaPattern
。 "Catalog" 是 "Database" 的另一个名称,因此只需提供数据库名称作为第一个参数:
ResultSet rsTBmd = dbm.getTables("myDatabase", "dbo", "myTable", new String[] {"TABLE"});
请注意,在这种情况下,您 不会 将名称括在方括号中,即使它们包含空格或 "funny characters".
根据 Microsoft,你的 JDBC URL 看起来像 "jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;user=UserName;password=*****";
,使你的代码:
// Database credentials
String USER = "Removed";
String PASS = "Removed";
Connection archiveConn = null;
Statement archiveStmt = null;
try {
// Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Open a connection
//concatinating strings for IP to hit
String ArchiveDB_URL = "jdbc:sqlserver://" + getArchiveIPaddress() + ";database=myDatabase;username="+USER+";password="+PASS;
archiveConn = DriverManager.getConnection(ArchiveDB_URL);
DatabaseMetaData dbm = archiveConn.getMetaData();
ResultSet rsTBmd = dbm.getTables(null,null, "[myDatabase].[dbo].[myTable]", new String[] {"TABLE"});
System.out.println(rsTBmd.getString("TABLE_NAME"));
if (rsTBmd.next()){
//result sets start at 1 not 0
int rsTBmdi = 1;
while(rsTBmd.next()){
System.out.println(rsTBmd.getString(rsTBmdi));
rsTBmdi++;
}
}
我希望这对您有所帮助...
getTables() 方法未返回数据库中存在的 table 的任何结果。我认为问题在于不知道在 ip 上查看哪个数据库。当我有多个具有相同 table 的数据库时,如何指定 getTables() 方法应该查找的数据库?
//concatinating strings for IP to hit
String ArchiveDB_URL = "jdbc:sqlserver://" + getArchiveIPaddress();
// Database credentials
String USER = "Removed";
String PASS = "Removed";
Connection archiveConn = null;
Statement archiveStmt = null;
try {
// Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Open a connection
archiveConn = DriverManager.getConnection(ArchiveDB_URL, USER, PASS);
DatabaseMetaData dbm = archiveConn.getMetaData();
ResultSet rsTBmd = dbm.getTables(null,null, "[myDatabase].[dbo].[myTable]", new String[] {"TABLE"});
System.out.println(rsTBmd.getString("TABLE_NAME"));
if (rsTBmd.next()){
//result sets start at 1 not 0
int rsTBmdi = 1;
while(rsTBmd.next()){
System.out.println(rsTBmd.getString(rsTBmdi));
rsTBmdi++;
}
}
尝试在 ArchiveDB_URL
中添加数据库名称String ArchiveDB_URL = "jdbc:sqlserver://" + getArchiveIPaddress() + "/yourDatabaseNameHere";
getTables 的前两个参数是 catalog
和 schemaPattern
。 "Catalog" 是 "Database" 的另一个名称,因此只需提供数据库名称作为第一个参数:
ResultSet rsTBmd = dbm.getTables("myDatabase", "dbo", "myTable", new String[] {"TABLE"});
请注意,在这种情况下,您 不会 将名称括在方括号中,即使它们包含空格或 "funny characters".
根据 Microsoft,你的 JDBC URL 看起来像 "jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;user=UserName;password=*****";
,使你的代码:
// Database credentials
String USER = "Removed";
String PASS = "Removed";
Connection archiveConn = null;
Statement archiveStmt = null;
try {
// Register JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Open a connection
//concatinating strings for IP to hit
String ArchiveDB_URL = "jdbc:sqlserver://" + getArchiveIPaddress() + ";database=myDatabase;username="+USER+";password="+PASS;
archiveConn = DriverManager.getConnection(ArchiveDB_URL);
DatabaseMetaData dbm = archiveConn.getMetaData();
ResultSet rsTBmd = dbm.getTables(null,null, "[myDatabase].[dbo].[myTable]", new String[] {"TABLE"});
System.out.println(rsTBmd.getString("TABLE_NAME"));
if (rsTBmd.next()){
//result sets start at 1 not 0
int rsTBmdi = 1;
while(rsTBmd.next()){
System.out.println(rsTBmd.getString(rsTBmdi));
rsTBmdi++;
}
}
我希望这对您有所帮助...