Presentation is loading. Please wait.

Presentation is loading. Please wait.

19 augustus 2003augustus 2003 JSP-2. BICT 2JDBC BICT 3Install MySQL Download MySQL daemon – Free – Windows version… Start Daemon – Mysqld-nt.exe Download.

Similar presentations


Presentation on theme: "19 augustus 2003augustus 2003 JSP-2. BICT 2JDBC BICT 3Install MySQL Download MySQL daemon – Free – Windows version… Start Daemon – Mysqld-nt.exe Download."— Presentation transcript:

1 19 augustus 2003augustus 2003 JSP-2

2 BICT 2JDBC

3 BICT 3Install MySQL Download MySQL daemon – Free – Windows version… Start Daemon – Mysqld-nt.exe Download JDBC-driver for MySQL – Free – Unpack it to a sub-dir

4 BICT 4MySQL-server is running! Ict-server MySQL installed Address: – Or: ictserver

5 BICT 5Jbuilder  MySQL-driver Tools – Configure libraries – Select directory (mysql-connector) Tools – Enterprise setup – Add MySQL-config to the database- drivers Project – Project properties – Required libraries – Add MySQL

6 BICT 6Form and database connection String isbnValue = request.getParameter(“isbn”); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = null; con = DriverManager.getConnection( "jdbc:mysql:// :3306/nieuwsgroep", "nieuwsgroep", ""); out.println(“messages "); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM message"); while (rs.next()) { for (int i = 1; i < 4; i++) System.out.print(rs.getString(i)+" "); System.out.println(" "); }

7 BICT 7Entry Form - First Attempt

8 BICT 8Entry Form - First Attempt Menu HTML Code Data Entry Menu Courses Classes Students

9 BICT 9Entry Form - First Attempt JSP Code Open connection code Statement code Presentation code Close connection code

10 BICT 10Entry Form - First Attempt Open Connectivity Code <% try { // Load Oracle Driver class file DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // Make a connection to the Oracle datasource Connection conn = DriverManager.getConnection “user", “pass"); %>

11 BICT 11Entry Form - First Attempt Statement Code <% // Create the statement Statement statement = conn.createStatement(); // Use the statement to SELECT the student attributes // FROM the Student table. ResultSet rs = statement.executeQuery ("SELECT * FROM Student"); %>

12 BICT 12Entry Form - First Attempt Presentation Code SSN First Last College <% // Iterate over the ResultSet while ( rs.next() ) { %> Iteration Code <% } %>

13 BICT 13Entry Form - First Attempt

14 BICT 14Entry Form - First Attempt Iteration Code

15 BICT 15Entry Form - First Attempt Close Connectivity Code <% // Close the ResultSet rs.close(); // Close the Statement statement.close(); // Close the Connection conn.close(); } catch (SQLException sqle) { out.println(sqle.getMessage()); } catch (Exception e) { out.println(e.getMessage()); } %>

16 BICT 16Entry Form - Second Attempt

17 BICT 17Entry Form - Second Attempt JSP Code Open connection code Insertion Code Statement code Presentation code Close connection code

18 BICT 18Entry Form - Second Attempt Insertion Code // Check if an insertion is requested String action = request.getParameter("action"); if (action != null && action.equals("insert")) { conn.setAutoCommit(false); // Create the prepared statement and use it to // INSERT the student attrs INTO the Student table. PreparedStatement pstmt = conn.prepareStatement( ("INSERT INTO Student VALUES (?, ?, ?, ?, ?)")); pstmt.setInt(1,Integer.parseInt(request.getParameter("SSN"))); pstmt.setString(2, request.getParameter("ID")); … pstmt.executeUpdate(); conn.commit(); conn.setAutoCommit(true); }

20 BICT 20Entry Form - Second Attempt Insert Form Code

21 BICT 21Entry Form - Third Attempt

22 BICT 22Entry Form - Third Attempt JSP Code Open connection code Insertion Code Update Code Delete Code Statement code Presentation code Close connection code

23 BICT 23Entry Form - Third Attempt Update Code // Check if an update is requested if (action != null && action.equals("update")) { conn.setAutoCommit(false); // Create the prepared statement and use it to // UPDATE the student attributes in the Student table. PreparedStatement pstatement = conn.prepareStatement( "UPDATE Student SET ID = ?, FIRSTNAME = ?, " + "LASTNAME = ?, COLLEGE = ? WHERE SSN = ?"); pstatement.setString(1, request.getParameter("ID")); pstatement.setString(2, request.getParameter("FIRSTNAME")); … int rowCount = pstatement.executeUpdate(); conn.setAutoCommit(false); conn.setAutoCommit(true); }

24 BICT 24Entry Form - Third Attempt Delete Code // Check if a delete is requested if (action != null && action.equals("delete")) { conn.setAutoCommit(false); // Create the prepared statement and use it to // DELETE the student FROM the Student table. PreparedStatement pstmt = conn.prepareStatement( "DELETE FROM Student WHERE SSN = ?"); pstmt.setInt(1, Integer.parseInt(request.getParameter("SSN"))); int rowCount = pstmt.executeUpdate(); conn.setAutoCommit(false); conn.setAutoCommit(true); }

25 BICT 25Entry Form - Third Attempt Presentation Code SSN First Last College Insert Form Code <% // Iterate over the ResultSet while ( rs.next() ) { %> Iteration Code <% } %>

26 BICT 26Entry Form - Third Attempt Iteration Code " name="SSN"> " name="ID"> … " name="SSN">

27 BICT 27JSP Actions The syntax for action elements is based on XML(i.e. they have a start tag, a body, and an end tag). The JSP specification includes some action types that are standard. New action types are added using the taglib directive.

28 BICT 28Standard Actions Web container implements these actions

29 BICT 29Standard Actions Associates an instance of a bean to a variable to use with in the jsp page …


Download ppt "19 augustus 2003augustus 2003 JSP-2. BICT 2JDBC BICT 3Install MySQL Download MySQL daemon – Free – Windows version… Start Daemon – Mysqld-nt.exe Download."

Similar presentations


Ads by Google