Get the Schema/Metadata out of a SQL Query without executing it

Writing by on Thursday, 7 of June , 2012 at 5:06 pm

You can use PreparedStatement to compile the SQL and then get the ResultSetMetaData from it.

// Get the Connection
Connection conn = getConnection();

// Get the MetaData
PreparedStatement ps = conn.prepareStatement(sql);
ResultSetMetaData rsmd = ps.getMetaData();

// Get the alias & type
for (int i=0; i<rsmd.getColumnCount(); i++) {
   String alias = rsmd.getColumnName(i+1);
   String strType = rsmd.getColumnTypeName(i+1);
   int type = rsmd.getColumnType(i+1)
}

Leave a comment

Category: Java,SQL DB

No Comments

No comments yet.

Leave a comment

You must be logged in to post a comment.

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.