简体   繁体   English

如何使用 Java 从数据库中检索所有表

[英]How to retrieve all the tables from database using Java

I am using JDBC and PostgreSQL as database, I was trying to create a logic in such a way that we can fetch all the data from a table, whatever table name user gives in the input it should get fetched, but the issue here is, I don't know how to do that.我正在使用 JDBC 和 PostgreSQL 作为数据库,我试图以这样一种方式创建一个逻辑,即我们可以从一个表中获取所有数据,无论用户在输入中提供什么表名,它都应该被获取,但这里的问题是,我不知道该怎么做。

Whenever we used to fetch table data from the database we are required to specify the the type of data we are getting on every index while we use ResultSet.每当我们过去从数据库中获取表数据时,我们都需要指定我们在使用 ResultSet 时在每个索引上获取的数据类型。

How to overcome from this hardcoded need of providing this metadata and make our code more general for any table with any number of columns and with any type如何克服提供此元数据的这种硬编码需求,并使我们的代码对具有任意数量的列和任何类型的任何表更通用

My code:我的代码:

Statement sttm = con1.createStatement();
System.out.println("Enter table name (usertable)");
String name = sc.next();
String tableData="";

String qu = "select * from "+name;
ResultSet rs =sttm.executeQuery(qu);
while(rs.next()) {
    // here we need to define the type by writing .getInt or getString
    tableData = rs.getInt(1)+":"+rs.getString(2)+":"+rs.getInt(3);
    System.out.println(tableData);
}
System.out.println("*********---------***********-----------**********");
sttm.close();

Anyone please suggest me some way to do it.任何人都请建议我一些方法来做到这一点。

If you want to print data of any table from a database then check my github project over CRUD java MySQL如果您想打印数据库中任何表格的数据,请通过 CRUD 检查我的 github 项目 java MySQL

https://github.com/gptshubham595/jdbc_mysql_CRUD-JAVA- https://github.com/gptshubham595/jdbc_mysql_CRUD-JAVA-

These are implemented这些是实现的

在此处输入图像描述

You can use ResultSet.getObject(int) .您可以使用ResultSet.getObject(int) getObject will automatically retrieve the data in the most appropriate Java type for the SQL datatype of the column. getObject将自动检索最适合列的 SQL 数据类型的 Java 类型的数据。

To retrieve the number of columns, you can use ResultSet.getMetaData() , and then use ResultSetMetaData.getColumnCount() to retrieve the number of columns.要检索列数,您可以使用ResultSet.getMetaData() ,然后使用ResultSetMetaData.getColumnCount()检索列数。

In short, to print all columns of all rows, you can do something like:简而言之,要打印所有行的所有列,您可以执行以下操作:

try (ResultSet rs = stmt.executeQuery(qu)) {
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    while (rs.next()) {
        StringBuilder tableData = new StringBuilder();
        for (int colIdx = 1; colIdx <= columnCount; colIdx++) {
            tableData.append(rs.getObject(colIdx));
            if (colIdx != columnCount) {
                tableData.append(':');
            }
        }
        System.out.println(TableData);
    }
}

You can also use ResultSetMetaData to get more information on the columns of the result set, for example if you need specific handling for certain types of columns.您还可以使用ResultSetMetaData获取有关结果集列的更多信息,例如,如果您需要对某些类型的列进行特定处理。 You can use getColumnType to get the java.sql.Types value of the column, or getColumnTypeName to get the type name in the database, or getColumnClassName to get the name of the class returned by ResultSet.getObject(int/String) , etc.您可以使用getColumnType获取列的java.sql.Types值,或getColumnTypeName获取数据库中的类型名称,或getColumnClassName获取ResultSet.getObject(int/String)返回的类的名称等。

However, as Sorin pointed out in the comments, accepting user input and concatenating it into a query string like you're currently doing, makes you vulnerable to SQL injection.但是,正如 Sorin 在评论中指出的那样,接受用户输入并将其连接到查询字符串中,就像您当前所做的那样,会使您容易受到 SQL 注入的攻击。 Unfortunately, it is not possible to parameterize object names, but you can mitigate this risk somewhat by 1) checking the table against the database metadata (eg DatabaseMetaData.getTables ), and 2) using Statement.enquoteIdentifier (though this won't necessarily protect you against all forms of injection).不幸的是,无法参数化对象名称,但您可以通过 1)根据数据库元数据(例如DatabaseMetaData.getTables )检查表,以及 2)使用Statement.enquoteIdentifier (尽管这不一定会保护你反对所有形式的注射)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM