Reputation: 135
When reading across different posts a question came across the mind Where is prepared statement precompiled, in jvm or in db? And when does the process actually happen in a java class. Example Below :=
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PreparedStmtDemo {
public static void main(String args[]) throws SQLException,Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger");
PreparedStatement preStatement = conn.prepareStatement("select RollNo from student where Stream =?");
preStatement.setString(1, "Commerce");
ResultSet result = preStatement.executeQuery();
while(result.next()){
System.out.println("Roll No: " + result.getString("RollNo"));
}
}
}
Upvotes: 4
Views: 3482
Reputation: 4369
JDBC driver precompiles the PreparedStatement to a SQL statement which involves mapping parameters from Java data type to SQL data type.
Then the precompiled statement is pooled in the Oracle database.
PreparedStatement has the following advantages over normal Statement:
But if you don't use query parameters then Statement and PreparedStatement behave the same way.
Upvotes: 3