0

我正在尝试查找特定字符串在数据库(所有表)中的位置。我有以下代码:

 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 不存在,因此会引发异常。有没有什么方法可以在不指定列的情况下搜索整个表格?

4

2 回答 2

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 where upper(column_name) = 'PRODUCTID'

因此你的电话应该是这样的

select table_name from user_tab_columns where upper(column_name) = 'F01' 然后 SELECT * FROM "+rs.getString(1)+"WHERE F01 = '0000000000998'

如您所见,我使用的是 LOWER 和 UPPER,您需要确保包含它们,这是您可以理解的原因,即使 SQL 在其语句中不区分大小写,但发生条件检查的值是区分大小写的。

于 2013-11-08T20:02:31.697 回答
0

以下 Java 代码循环遍历每个表中的每个 VARCHAR/NVARCHAR 列,并SELECT TOP 1对该列执行 a 以查看它是否被命中:

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]
于 2013-11-09T10:25:04.110 回答