Java C9. Advanced JDBC pptx

34 354 0
Java C9. Advanced JDBC pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 java.sql.Connection.prepareStatement(String sql) throws SQLException  Arguments : setting by parameters “?”  Exemple : Connection con; PreparedStatement stmt; try { con = DriverManager. getConnection(url,userName,userPassword); stmt = con.prepareStatement ( "Update Student Set mark = ? Where id = ?") ; } catch (SQLException e) { e.printStackTrace(); } 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 = con.prepareStatement ( "Update Student Set mark = ? Where id = ?") ;  Exemple : stmt = con.prepareStatement("Update Student Set mark = ? Where NAME = ?"); stmt.setInt(1, 7); stmt.setString(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 = stmt.executeQuery();  count = stmt.executeUpdate(); // number of updated records Application example  prompt > java UpdateStudent <name> <newMark> Configuration of input parameters 1 2 3 Args[0] : name Args[1] : mark [...]... “J2EE”, 3 for class “J2ME” Hands-on exercises : advanced exercises  Write a Java program to move a student from one class to another class :   prompt > java changeClass Write a Java program to move one student out of database :  prompt > java removeStudent Hands-on exercises : another exercises  Write Java programs to do the following works :   ... database named “qlsv” and 2 tables CLASS and STUDENT in MySQL Write a Java program :    prompt > java inputClass STUDENTNUMBER = 0 Use this program to insert 3 classes : “J2SE”, “J2EE”, “J2ME” Hands-on exercises : Transaction  Write a Java program insert a student into Student table:      prompt > java inputStudent ...Declaration (1) import import import import java. sql.Connection; java. sql.PreparedStatement; java. sql.DriverManager; java. sql.SQLException; public class UpdateStudent { public static void main(String[] args) { if( args.length != 2 ){ System.out.println("Usage : UpdateStudent ... PreparedStatement stmt = null; String userName = "root"; String userPassword = "quangnh“; String drv = "com.mysql .jdbc. Driver"; String url = "jdbc: mysql://localhost:3306/qlsv"; String name = args[0]; int newMark = Integer.parseInt(args[1]); System.out.println("Update database : "); Update database (1) try { // JDBC driver‘s reading Class.forName (drv); // Connection to database con = DriverManager.getConnection(url,userName,userPassword);... setting default Each command is itshelf a transaction Syntax :   void java. sql.Connection.setAutoCommit(boolean autoCommit) throws SQLException Control auto-commit mode by setting autoCommit argument : Enable(true) / Disable (false) Exemple Connection con = null; String userName = “student"; String userPassword = “student"; String url = "jdbc: mysql://localhost:3306/qlsv"; con = DriverManager.getConnection... (url,userName,userPassword); con.setAutoCommit(false); Committing a transaction   void java. sql.Connection.commit() throws SQLException Example : con = DriverManager.getConnection (url,userName,userPassword); con.setAutoCommit(false); // multiple database update processing … con.commit(); Cancellation of transaction   void java. sql.Connection.rollback() throws SQLException Example : con = DriverManager.getConnection . Chapter 9. Advanced JDBC ITSS Java Programming NGUYEN Hong Quang, HUT Statement and Prepared Statement (1) Making of. example  prompt > java UpdateStudent <name> <newMark> Configuration of input parameters 1 2 3 Args[0] : name Args[1] : mark Declaration (1) import java. sql.Connection; import java. sql.PreparedStatement; import. mark Declaration (1) import java. sql.Connection; import java. sql.PreparedStatement; import java. sql.DriverManager; import java. sql.SQLException; public class UpdateStudent { public static void main(String[]

Ngày đăng: 28/06/2014, 03:20

Mục lục

  • Statement and Prepared Statement (1)

  • Statement and Prepared Statement (2)

  • Creation of Prepared Statement

  • Setting of parameters : Exemple

  • Configuration of input parameters

  • Disable automatic commit mode

  • Sample application : identify the mark of the student

  • Sample application : Update new mark

  • Sample application : commit or rollback

  • Sample application : display newMark

  • Sample application : Results of commit transaction

  • Sample application : Results of rollback transaction

  • Hands-on exercise : structure of database

  • Hands-on exercises : Connection, Execution Simple SQL Statement

  • Hands-on exercises : advanced exercises

  • Hands-on exercises : another exercises

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan