use "explain" of mysql in Java

E

Ela

While "explain" works perfectly in mysql console, it does not print out
anything in java code. Is there any workaround?

selectCmd = " select * from ";
selectCmd += " t1";
selectCmd = "explain" + selectCmd;
stmt = con.createStatement();
stmt.execute(selectCmd);
System.out.println(selectCmd);
return;
 
T

Thomas Kellerer

Ela wrote on 26.04.2008 15:31:
While "explain" works perfectly in mysql console, it does not print out
anything in java code. Is there any workaround?

selectCmd = " select * from ";
selectCmd += " t1";
selectCmd = "explain" + selectCmd;
stmt = con.createStatement();
stmt.execute(selectCmd);

You need to process the result returned from the statement:

ResultSet rs = stmt.execute(selectCom);
while (rs.next())
{
System.out.println(rs.getString(1));
}
 
A

Arne Vajhøj

Thomas said:
Ela wrote on 26.04.2008 15:31:

You need to process the result returned from the statement:

ResultSet rs = stmt.execute(selectCom);
while (rs.next())
{
System.out.println(rs.getString(1));
}

ResultSet rs = stmt.executeQuery(selectCom);

Arne
 
E

Ela

You need to process the result returned from the statement:
ResultSet rs = stmt.execute(selectCom);
while (rs.next())
{
System.out.println(rs.getString(1));
}

Thanks for your prompt advice. However, I'm unable to identify how to print
out the column names of the result set, after checking javadoc of resultset.
Since the query is dynamic and therefore I'm unable to know the names in
advance, would you have any idea?
 
A

Arne Vajhøj

Ela said:
Thanks for your prompt advice. However, I'm unable to identify how to print
out the column names of the result set, after checking javadoc of resultset.
Since the query is dynamic and therefore I'm unable to know the names in
advance, would you have any idea?

JDBC allows both indexing of fields by name and by number.

The code above uses number.

You do not need the names.

Arne
 
S

Stefan Ram

Ela said:
I'm unable to identify how to print out the column names of the
result set, after checking javadoc of resultset. Since the
query is dynamic and therefore I'm unable to know the names in
advance, would you have any idea?

I copy two relevant method declarations from a web page:

private static String thenMakeString
( final java.sql.ResultSet set )
throws java.sql.SQLException
{ final java.sql.ResultSetMetaData desc = set.getMetaData();
return title( desc ) + body( desc, set ); }

private static String title
( final java.sql.ResultSetMetaData desc )
throws java.sql.SQLException
{ final int cols = desc.getColumnCount(); String text = "";
for( int i = 1; i <= cols; ++i )
text += desc.getColumnName( i )+ " ";
text += eol; return text; }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top