且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解析器解析SQL查询并返回Java中的列名和对应的表名

更新时间:2023-02-26 10:19:42

如果您与数据库建立了连接,则可以使用准备好的语句来做到这一点:

If you have a connection to the database, you can do that using a prepared statement:

String sql = "....";
PreparedStatement pstmt = connection.prepareStatement(sql);
ResultSetMetaData meta = pstmt.getMetaData();

然后,您可以使用 ResultSetMetaData.getColumnCount() getColumnName(int) 检索列名.如果您的JDBC驱动程序支持它,您甚至可以使用

Then you can use ResultSetMetaData.getColumnCount() and getColumnName(int) to retrieve the column names. If your JDBC driver supports it, you can even get the underlying table name using getTableName(int)

请注意,并非所有驱动程序都支持在实际执行该语句之前获取元数据,您需要使用所使用的驱动程序进行测试.

Note that not all drivers support getting the metadata before actually executing the statement, you need to test that with the one you are using.