我试图找到数据库中存在特定字符串的位置(所有表)。我有以下代码:
DatabaseMetaData md = con.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
stm = con.createStatement();
String sql;
sql = "SELECT * FROM "+rs.getString(3)+"WHERE F01 = '0000000000998'";
rs2 = stm.executeQuery(sql);
while(rs2.next()){
System.out.println(rs.getString(3));
}
}
问题在于某些表F01不存在,因此抛出异常。有没有办法即使没有指定列我可以搜索整个表?
答案 0 :(得分:1)
您正在使用哪个数据库。
查看此讨论帖。在致电支票之前,您可能会对如何确保列存在感到满意 http://www.coderanch.com/t/299298/JDBC/databases/Oracle-describe-table-jdbc
对于oracle 你可以查看这些查询
从user_tab_columns中选择COLUMN_NAME,DATA_LENGTH,DATA_TYPE,其中Lower(table_name)='product' 从user_tab_columns中选择table_name,COLUMN_NAME,DATA_LENGTH,DATA_TYPE,其中upper(column_name)='PRODUCTID'
因此你的电话应该是这样的
从user_tab_columns中选择table_name,其中upper(column_name)='F01' 然后 SELECT * FROM“+ rs.getString(1)+”WHERE F01 ='0000000000998'
如您所见,我正在使用LOWER和UPPER,您需要确保包含它们,因为您可以理解,即使SQL在其语句中不区分大小写,但条件检查发生的值是区分大小写的
答案 1 :(得分:0)
以下Java代码遍历每个表中的每个VARCHAR / NVARCHAR列,并在该列上执行SELECT TOP 1
以查看它是否受到命中:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCQuery {
public static void main(String args[]) {
String textToSearchFor = "Gord"; // test data
System.out.println(String.format("The value '%s' was found in the following locations:", textToSearchFor));
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connectionString =
"jdbc:odbc:Driver={SQL Server};" +
"Server=.\\SQLEXPRESS;" +
"Trusted_Connection=yes;" +
"Database=myDb";
conn = DriverManager.getConnection(connectionString);
DatabaseMetaData md = conn.getMetaData();
ResultSet mdrs = md.getTables(null, null, "%", new String[] { "TABLE" });
List<String> tableList = new ArrayList<String>();
while (mdrs.next()) {
tableList.add(String.format("[%s].[%s].[%s]", mdrs.getString(1), mdrs.getString(2), mdrs.getString(3)));
// i.e., [catalogName].[schemaName].[tableName]
}
mdrs.close();
for (String tableName : tableList) {
Statement stmt = conn.createStatement();
stmt.execute("SELECT * FROM " + tableName + " WHERE 0=1");
ResultSet rs0 = stmt.getResultSet();
ResultSetMetaData rsmd = rs0.getMetaData();
List<String> columnList = new ArrayList<String>();
for (int colIndex = 1; colIndex <= rsmd.getColumnCount(); colIndex++) {
switch (rsmd.getColumnType(colIndex)) {
case java.sql.Types.VARCHAR:
case java.sql.Types.NVARCHAR:
columnList.add("[" + rsmd.getColumnName(colIndex) + "]");
break;
}
}
rs0.close();
stmt.close();
for (String columnName : columnList) {
String psSql = String.format("SELECT TOP 1 * FROM %s WHERE %s = ?", tableName, columnName);
ps = conn.prepareStatement(psSql);
ps.setString(1, textToSearchFor);
ResultSet rs1 = ps.executeQuery();
if (rs1.next()) {
System.out.println(String.format("column %s in %s", columnName, tableName));
}
rs1.close();
ps.close();
}
}
} catch( Exception e ) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch( Exception e ) {
e.printStackTrace();
}
}
}
}
结果看起来像这样;
The value 'Gord' was found in the following locations:
column [textCol] in [myDb].[dbo].[linkedTable]
column [FirstName] in [myDb].[dbo].[myContacts]