tailieunhanh - Java C9. Advanced JDBC
Prepared Statement : compile only one time Statement : compile each time to run If we have to use one SQL statement several times, it would better to use Preparerd Statement. PreparedStatement (String sql) throws SQLException Arguments : setting by parameters “?” | Chapter 9. Advanced JDBC ITSS Java Programming NGUYEN Hong Quang, HUT Statement and Prepared Statement (1) Making of Statement object All of parameters must be in the statement Making of Preparerd Statement object Some parameters are not in the statement Setting value of parameter Execution of Prepared Statement Execution of Statement Statement and Prepared Statement (2) Prepared Statement : compile only one time Statement : compile each time to run If we have to use one SQL statement several times, it would better to use Preparerd Statement Creation of Prepared Statement PreparedStatement (String sql) throws SQLException Arguments : setting by parameters “?” Exemple : Connection con; PreparedStatement stmt; try { con = (url,userName,userPassword); stmt = ("Update Student Set mark = ? Where id = ?"); } catch (SQLException e) { (); } Setting of parameters Format : stmt. setXXX (index, . | Chapter 9. Advanced JDBC ITSS Java Programming NGUYEN Hong Quang, HUT Statement and Prepared Statement (1) Making of Statement object All of parameters must be in the statement Making of Preparerd Statement object Some parameters are not in the statement Setting value of parameter Execution of Prepared Statement Execution of Statement Statement and Prepared Statement (2) Prepared Statement : compile only one time Statement : compile each time to run If we have to use one SQL statement several times, it would better to use Preparerd Statement Creation of Prepared Statement PreparedStatement (String sql) throws SQLException Arguments : setting by parameters “?” Exemple : Connection con; PreparedStatement stmt; try { con = (url,userName,userPassword); stmt = ("Update Student Set mark = ? Where id = ?"); } catch (SQLException e) { (); } Setting of parameters Format : stmt. setXXX (index, value); setXXX : method corresponding with each data type Index : numerical value corresponding to the position of the parameter Value : value of parameter Setting of parameters : Exemple PreparedStatement stmt; stmt = ("Update Student Set mark = ? Where id = ?"); Exemple : stmt = ("Update Student Set mark = ? Where NAME = ?"); (1, 7); (2, "Tran Duy Dong"); Updated parameter methods Execute SQL Statement Reference type and update type SQL Statement executeQuery (String sql) Executes the SELECT statement. executeUpdate (String sql) Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement. Exemple : ResultSet res = (); count = (); // number of updated records Application example prompt>java UpdateStudent Configuration of input parameters 1 2 3 Args[0] : name Args[1] : mark Declaration (1) import ; import
đang nạp các trang xem trước