Uporaba objektov PreparedStatement
Objekti PreparedStatement
Te objekte lahko uporabimo namesto objektov Statement. Objekte PreparedStatement imajo naslednje prednosti:- Pri ponavljanih povpraševanjih (na primer v zankah) je to bolj učinkovito, ker so stavki SQL prevedeni le enkrat
- Mehanizem vključevanja vrednosti parametrov poskrbi za vse posebne znake
PreparedStatement pstmt = con.prepareStatement( "INSERT INTO Customers " + "(CustomerFirstName, CustomerLastName, CustomerAddress) "+ "VALUES (?, ?, ?)") ; pstmt.clearParameters() ; pstmt.setString(1, "Joan") ; pstmt.setString(2, "D'Arc") ; pstmt.setString(3, "Tower of London") ; count = pstmt.executeUpdate() ; System.out.println ("\nInserted " + count + " record successfully\n") ; pstmt.clearParameters() ; pstmt.setString(1, "John") ; pstmt.setString(2, "D'Orc") ; pstmt.setString(3, "Houses of Parliament, London") ; count = pstmt.executeUpdate() ; System.out.println ("\nInserted " + count + " record successfully\n") ; |