04 - Application Server and Database Server Communication

04 Application - CSE 135 Application Server and Database Server Communication Three-Tier Architecture Located Any PC Browser HTTP Requests Located

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 135 Application Server and Database Server Communication Browser HTTP Located @ Any PC Three-Tier Architecture App Server HTML Tuples Requests JDBC Located @ Server 2 JSPs 2 Database Server Requests Located @ Server 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Data Entry Form 3 JDBC import java.sql.*; class JdbcTest { public static void main (String args []) throws SQLException { // Registering Postgresql JDBC driver Class.forName("org.postgresql.Driver"); 4 // Open a connection to the database Connection conn = DriverManager.getConnection( "jdbc:postgresql://localhost/jdbc-examples?" + "user=postgres&password=postgres");
Background image of page 2
3 JDBC (cont’d) // Query the student PIDs Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("SELECT pid FROM students"); // Print out the PID (1st attribute) while (rset.next ()) System.out.println (rset.getInt(1)); //close the result set, statement, and connection 5 rset.close(); stmt.close(); conn.close(); } } PreparedStatement Object If you want to execute a Statement object many times, it will normally reduce execution time to use a P dSt t t object instead: use a PreparedStatement PreparedStatement pstmt = conn.prepareStatement( "UPDATE students SET first_name = ? " + "WHERE pid = ?"); pstmt setString(1 ”John"); 6 pstmt.setString(1, John ); pstmt.setInt(2, 88888888); pstmt.executeUpdate();
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 PreparedStatement Object (cont’d) The following two code fragments accomplish the same thing: String updateStr = "UPDATE students SET first_name = ’John’ " + "WHERE pid = 88888888"; stmt.executeUpdate(updateStr); PreparedStatement pstmt = conn.prepareStatement( "UPDATE students SET first name = ? " + 7 "WHERE pid = ?"); pstmt.setString(1, ”John"); pstmt.setInt(2, 88888888); updateSales.executeUpdate(): ResultSet Object int getInt(int columnIndex) Retrieves the value of the designated column in the current row of a R ltS t object as an it the current row of a ResultSet object as an int Java type int getInt(String columnName) String getString(int columnIndex) 8 String getString(String columnName)
Background image of page 4
5 Using Transactions When a connection is created, it is in AutoCommit mode, that is, each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed conn.setAutoCommit(false); ... <transaction>
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/31/2011 for the course CSE 135 taught by Professor Papakonstantinou during the Fall '10 term at UCSD.

Page1 / 17

04 Application - CSE 135 Application Server and Database Server Communication Three-Tier Architecture Located Any PC Browser HTTP Requests Located

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online