SQL response type

2019-07-23 02:41发布

问题:

In my application I get an SQL query from user then I send to a db. Can I get information about what is the result type of SQL query is without parsing the query? I use pure JDBC to do these operations.

回答1:

Yes. You can use a PreparedStatement and invoke its getMetadata method in order to have information about the ResultSets columns it will return.

http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#getMetaData()

Connection conn = ...
PreparedStatement ps = conn.prepareStatement(yourStatement);
ResultSetMetadata metadata = ps.getMetadata(); // this is what you need
...
// now you can execute the query
ResultSet rs = ps.executeQuery();


回答2:

Use connection.prepareStatement() on your query, a preparedStatement is returned. Use its .getMetaData() method.



标签: java sql jdbc