当阅读不同的帖子时,脑海中浮现出一个问题,准备好的语句在哪里预编译,在 jvm 中还是在 db 中?该过程何时实际发生在 java 类中。下面的例子:=
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"));
}
}
}