Can JDBC statement run explain plan on query string?
The code throws SQL exception
Error message: Incorrect syntax near the keyword 'plan'.
Stacktrace is null
I just copy from internet of using stmt.execute. However, it seems that stmt.execute() only Returns true if the first result is a ResultSet object; false if it is an update count or there are no results
conn = getEntityManager().unwrap(java.sql.Connection.class);
stmt = conn.createStatement();
stmt.execute("explain plan for SELECT 1 from Dual"); // throws sql exception
rs = stmt.executeQuery("select plan_table_output from table(dbms_xplan.display())");
while (rs.next())
{
System.out.println(rs.getString(1));
}
Related
I am trying to move all query executions from Statement to PreparedStatement due to SQL injection. My original issue was with update statement, but I wanted to try it with select statement as well. When I execute the below line of code, the statement returns nothing.
String selectQuery = "select is_enabled, syllabus_id from ic_syllabus where syllabus_id=?";
PreparedStatement pstmt = conn.prepareStatement(selectQuery);
pstmt.setString(1, "25AC1CFB7C1A2CF07F176BD3A296F229");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String flag = rs.getString(1);
String sybsId = rs.getString(2);
}
I am using Oracle database and am not getting any exceptions either.
This question already has answers here:
mysql prepared statement error: MySQLSyntaxErrorException
(2 answers)
Closed 6 years ago.
I've a course table with the columns,
id, teacher_id and name.
This is the method that I'm using to get a course by id.
public static Course getById(int id) throws SQLException {
String query = "SELECT * FROM courses WHERE id = ?" ;
Course course = new Course();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
connection = (Connection) DriverManager.getConnection(ConnectDb.CONN_STRING, ConnectDb.USERNAME, ConnectDb.PASSWORD);
statement = (PreparedStatement) connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statement.setInt(1, id);
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
course.setId(resultSet.getInt("id"));
course.setName(resultSet.getString("name"));
course.setTeacherId(resultSet.getInt("teacher_id"));
}
}catch (SQLException e) {
System.err.println(e);
}finally{
if (resultSet != null) resultSet.close();;
if (statement != null) statement.close();
if(connection != null) connection.close();
}
return course;
}// end of method
When I run this method, I get an output id :0, teacher_id : 0
The server log says that I've an SQLException
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
The bug is here:
resultSet = statement.executeQuery(query);
You're not calling PreparedStatement#executeQuery, you're calling Statement#executeQuery (Statement is a superinterface of PreparedStatement). So the parameter substitution isn't happening and you're actually sending that ? to the server.
Change it to:
resultSet = statement.executeQuery();
// No argument here ---------------^
(And yes, this is an API design flaw; and no, you're not the first to fall into it.)
There are a few other things about that code that could use improvement:
You're always returning a Course, even if an exception occurred. Best practices would be to allow the exception to propagate to the caller; second-best practices would be to return some kind of flag to the caller that an error occurred, such as null.
The try-with-resources statement can make that code both shorter and clearer
You shouldn't have to cast the return values of getConnection or prepareStatement.
You're using while, but you're expecting only a single result. if would make more sense.
On that topic, you can give the driver a hint in that regard by using setMaxRows.
Your method declares that it can throw SQLException, which is literally true since it calls close, but the only useful SQLException is actually being caught, logged, and suppressed by the code, making declaring it on the method a bit misleading.
I'm told modern JDBC drivers don't need the registerDriver call anymore. (I personally haven't used JDBC for a while now, so...)
Here's an example incoporating the above. It allows an exception to propagate, so errors (exceptional conditions) are not handled in the normal flow of code; it returns null if there's no matching course:
public static Course getById(int id) throws SQLException {
String query = "SELECT * FROM courses WHERE id = ?";
try (
Connection connection = DriverManager.getConnection(ConnectDb.CONN_STRING, ConnectDb.USERNAME, ConnectDb.PASSWORD);
PreparedStatement statement = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
) {
statement.setInt(1, id);
statement.setMaxRows(1);
try (
ResultSet resultSet = statement.executeQuery();
) {
if (resultSet.next()) {
Course course = new Course();
course.setId(resultSet.getInt("id"));
course.setName(resultSet.getString("name"));
course.setTeacherId(resultSet.getInt("teacher_id"));
return course;
}
// No matching course
return null;
}
}
} // end of method
That can probably be improved further, but you get the idea.
This question already has answers here:
MySQLSyntaxErrorException near "?" when trying to execute PreparedStatement
(2 answers)
Closed 7 years ago.
Im preparing a query using PreparedStatements and it runs fine when i hardcode te query with the condition parameter.
but throws error , if the parameter is passed from setString() method.
com.mysql.jdbc.JDBC4PreparedStatement#2cf63e26: select * from linkedin_page_mess ages where company_id = '2414183' com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
In the error log, above my query looks fine.
public JSONObject getLinkedInMessages(String compId)
{
linlogger.log(Level.INFO, "Called getLinkedInMessages method");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
JSONObject resObj = new JSONObject();
JSONArray tempArray = new JSONArray();
try
{
conn = InitOrGetConnection();
String query = "select * from linkedin_page_messages where company_id = ?";
PreparedStatement pst=conn.prepareStatement(query);
pst.setString(1, compId);
System.out.println("\n\n"+pst.toString());
rs= pst.executeQuery(query);
// process resultset logics
}
catch(Exception e)
{
System.out.println(e);
linlogger.log(Level.INFO, "Exception occured "+e.toString());
}
}
Is there anything wrong with the PreparedStatements?
Remove the parameter from
rs= pst.executeQuery(query);
change to
rs= pst.executeQuery();
If you pass query in pst.executeQuery(query); as parameter then this passed query string take priority over the query string you passed in conn.prepareStatement(query); and since in query(select * from linkedin_page_messages where company_id = ?) you dint pass parameter you get the error.
remove the parameter in this line:
rs= pst.executeQuery(query);
It must be
rs= pst.executeQuery();
Because the statement is prepared at PreparedStatement pst=conn.prepareStatement(query);
execute(String sql) is inherited from Statement and will execute the satement (sql) without prepared it.
I'm trying to get the first row in database using JDBC and go last but error message appear to me:
com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
My code is
String sql="select * from Machines";
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
if(rs.first()) {
jCombBrands.setSelectedItem(getN_brandNum(rs.getInt("BrandID")));
txt_Ram.setText(rs.getString("Ram"));
txt_poN.setText(rs.getString("PONummber"));
}
}
I have done below sample JDBC program which retrieving user details.Now i am getting surprised that from same callable statement i am getting different result set for same output parameter index.Ideally it should return same ResultSet object.
when i got the resultset then i am moving cursor -1 to 0.
i am retrieving data from another resultset for the same output param using
column name then i am getting following exception
,
Exception in thread "main" java.sql.SQLException: Missing defines
System.out.println("Before loading connection");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:#170.45.3.165:1541/testdb.mycomp.com",
"admin", "admin123");
System.out.println("Connection loaded " + connection);
CallableStatement callProcedure = connection
.prepareCall("{call ADMIN_USER.Fetch_User_Details(?,?)}");
callProcedure.setString(1, "userid=testid");
callProcedure.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
callProcedure.execute();
ResultSet resultUserDetails = (ResultSet) callProcedure.getObject(2);
resultUserDetails.next();
ResultSet resultUserDetails1 = (ResultSet) callProcedure.getObject(2);
String username = resultUserDetails1.getString(COL_NAME_USER_NAME);
System.out.println(resultUserDetails);
System.out.println(resultUserDetails1);
System.out.println(resultUserDetails == resultUserDetails1);
Can anyone has any idea why we are getting two different resultset for same output index from callable statement?
According to CallableSatement API the expression should be like this
{call <procedure-name>[(<arg1>,<arg2>, ...)]}
try
CallableStatement callProcedure = connection.prepareCall("{call ADMIN_USER.Fetch_User_Details(?,?)}");
Have you tried to check if only reference are different (maybe due to proxy or dirty state because you perform a next() before the second `getObject())?
try
ResultSet resultUserDetails = (ResultSet) callProcedure.getObject(2);
ResultSet resultUserDetails1 = (ResultSet) callProcedure.getObject(2);
System.out.println(resultUserDetails);
System.out.println(resultUserDetails1);
// Check ref or object equality
System.out.println(resultUserDetails == resultUserDetails1);
System.out.println(resultUserDetails.equals(resultUserDetails1));
resultUserDetails.next();
String username = resultUserDetails.getString(COL_NAME_USER_NAME);
resultUserDetails1.next();
String username1 = resultUserDetails1.getString(COL_NAME_USER_NAME);
System.out.println(username);
System.out.println(username1);
// We read the same username or we are reading first and second username?
// If the are different probably resultset is the same, just with different
// reference
System.out.println(resultUserDetails.equals(resultUserDetails1));