Question & Answer
Question
How do I retrieve the execution plan for a SQL statement in Java?
Answer
The following Java code, which works for running a SQL select statement, returns no output when the execution plan is requested:
- package netezza;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Junk {
public static Connection getConnection () {
String databaseName="DEV_WHSE";
Connection conn=null;
String user="admin";
String password="password";
try {
Class.forName("org.netezza.Driver");
//.newInstance();
conn =DriverManager.getConnection("jdbc:netezza://server1/" + databaseName, user, password);
conn.setAutoCommit(false);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return conn;
} //getConnection
public static void main(String[] args) {
ResultSet rs=null;
Statement stmt = null;
Connection conn=null;
String sql = "explain verbose select * from orders";
try {
conn= getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String resultString=rs.getString(1); System.out.println(resultString );
} //while
stmt.close();
rs.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
} //main
} //Class
I want to issue EXPLAIN VERBOSE and retrieve the output in Java. For example:
- explain verbose select sic_code from sic_location s where sic_id< ";
The output of EXPLAIN VERBOSE is very different from the output of a SELECT statement; it is a series of 'warnings'. Use the following code to allow for those warnings and retrieve the output:
- SQLWarning w = stmt.getWarnings();
if (w != null) {
do
{
System.out.println(w);
} while ((w = w.getNextWarning()) != null);
}
[{"Product":{"code":"SSULQD","label":"IBM PureData System"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":null,"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"1.0.0","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]
Historical Number
NZ896992
Was this topic helpful?
Document Information
Modified date:
17 October 2019
UID
swg21574852