SlideShare a Scribd company logo
Three-Tier Architecture
Oracle
DB Server
Apache Tomcat
App Server
Microsoft
Internet
Explorer
HTML
Tuples
HTTP
Requests
JDBC
Requests
Java Server
Pages (JSPs)
Located
@ DBLab
Located
@ Your PC
Located
@ Any PC
Data Entry Forms
Java Database Connectivity (JDBC)
import java.sql.*;
class JdbcTest {
public static void main (String args []) throws SQLException {
// Load Oracle driver
DriverManager.registerDriver (new
oracle.jdbc.driver.OracleDriver());
// Connect to the local database
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:ORCL","scott", "tiger");
JDBC
// Query the student names
Statement stmt = conn.createStatement ();
ResultSet rset = stmt.executeQuery ("SELECT name FROM
Student");
// Print the name out
//name is the 2nd
attribute of Student
while (rset.next ())
System.out.println (rset.getString (2));
//close the result set, statement, and the connection
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 PreparedStatement
object instead.
PreparedStatement updateStud =
conn.prepareStatement( "UPDATE Student SET name = ?
WHERE lastname LIKE ?");
updateStud.setString(1, “John”);
updateStud.setString(2, “Smith”);
updateStud.executeUpdate();
PreparedStatement Object
the following two code fragments accomplish the same thing:
• Code Fragment 1:
String updateString = "UPDATE COFFEES SET SALES = 75
" + "WHERE COF_NAME LIKE 'Colombian'";
stmt.executeUpdate(updateString);
• Code Fragment 2:
PreparedStatement updateSales =
con.prepareStatement( "UPDATE COFFEES SET
SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate():
• int getInt(int columnIndex)
Retrieves the value of the designated column in the
current row of this ResultSet object as an int in the Java
programming language.
• int getInt(String columnName)
• String getString(int columnIndex)
• String getString(String columnName)
Using Transactions
When a connection is created, it is in auto-commit mode. This means that
each individual SQL statement is treated as a transaction and will be
automatically committed right after it is executed.
conn.setAutoCommit(false);
....
transaction
...
con.commit();
con.setAutoCommit(true);
Using Transactions
example
con.setAutoCommit(false);
PreparedStatement updateSales = con.prepareStatement( "UPDATE
COFFEES SET SALES = ? WHERE COF_NAME LIKE ?");
updateSales.setInt(1, 50);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate();
PreparedStatement updateTotal = con.prepareStatement( "UPDATE
COFFEES SET TOTAL = TOTAL + ? WHERE COF_NAME LIKE ?");
updateTotal.setInt(1, 50);
updateTotal.setString(2, "Colombian");
updateTotal.executeUpdate();
con.commit();
con.setAutoCommit(true);
Retrieving Exceptions
JDBC lets you see the warnings and exceptions generated by your
DBMS and by the Java compiler. To see exceptions, you can have a
catch block print them out. For example, the following two catch
blocks from the sample code print out a message explaining the
exception:
try {
// Code that could generate an exception goes here.
// If an exception is generated, the catch block below
// will print out information about it.
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
JSP Syntax
• Comment
– <%-- Comment --%>
• Expression
– <%= java expression %>
• Scriplet
– <% java code fragment %>
• Include
– <jsp:include page="relativeURL" />
Entry Form - First Attempt
Entry Form - First Attempt
<b>Data Entry Menu</b>
<ul>
<li>
<a href="courses.jsp">Courses<a>
</li>
<li>
<a href="classes.jsp">Classes<a>
</li>
<li>
<a href="students.jsp">Students<a>
</li>
</ul>
Menu HTML Code
Entry Form - First Attempt
<html>
<body>
<table>
<tr>
<td>
<jsp:include page="menu.html" />
</td>
<td>
Open connection code
Statement code
Presentation code
Close connection code
</td>
</tr>
</table>
</body>
</html>
JSP Code
Entry Form - First Attempt
<%-- Set the scripting language to java and --%>
<%-- import the java.sql package --%>
<%@ page language="java" import="java.sql.*" %>
<%
try {
// Load Oracle Driver class file
DriverManager.registerDriver
(new oracle.jdbc.driver.OracleDriver());
// Make a connection to the Oracle datasource
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@feast.ucsd.edu:1521:source",
“user", “pass");
%>
Open Connectivity Code
Entry Form - First Attempt
<%
// 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");
%>
Statement Code
Entry Form - First Attempt
<table>
<tr>
<th>SSN</th>
<th>First</th>
<th>Last</th>
<th>College</th>
</tr>
<%
// Iterate over the ResultSet
while ( rs.next() ) {
%>
Iteration Code
<%
}
%>
</table>
Presentation Code
Entry Form - First Attempt
Entry Form - First Attempt
<tr>
<%-- Get the SSN, which is a number --%>
<td><%= rs.getInt("SSN") %></td>
<%-- Get the ID --%>
<td><%= rs.getString("ID") %></td>
<%-- Get the FIRSTNAME --%>
<td><%= rs.getString("FIRSTNAME") %></td>
<%-- Get the LASTNAME --%>
<td><%= rs.getString("LASTNAME") %></td>
<%-- Get the COLLEGE --%>
<td><%= rs.getString("COLLEGE") %></td>
</tr>
Iteration Code
Entry Form - First Attempt
<%
// 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());
}
%>
Close Connectivity Code
Entry Form - Second Attempt
Entry Form - Second Attempt
<html>
<body>
<table>
<tr>
<td>
Open connection code
Insertion Code
Statement code
Presentation code
Close connection code
</td>
</tr>
</table>
</body>
</html>
JSP Code
Entry Form - Second Attempt
// 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);
}
Insertion Code
Entry Form - Second Attempt
<table>
<tr>
<th>SSN</th>
<th>First</th>
<th>Last</th>
<th>College</th>
</tr>
Insert Form Code
<%
// Iterate over the ResultSet
while ( rs.next() ) {
%>
Iteration Code
<%
}
%>
</table>
Presentation Code
Entry Form - Second Attempt
<tr>
<form action="students.jsp" method="get">
<input type="hidden" value="insert" name="action">
<th><input value="" name="SSN" size="10"></th>
<th><input value="" name="ID" size="10"></th>
<th><input value="" name="FIRSTNAME" size="15"></th>
<th><input value="" name="LASTNAME" size="15"></th>
<th><input value="" name="COLLEGE" size="15"></th>
<th><input type="submit" value="Insert"></th>
</form>
</tr>
Insert Form Code
Entry Form - Third Attempt
Entry Form - Third Attempt
<html>
<body>
<table>
<tr>
<td>
Open connection code
Insertion Code
Update Code
Delete Code
Statement code
Presentation code
Close connection code
</td>
</tr>
</table>
</body>
</html>
JSP Code
Entry Form - Third Attempt
// 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);
}
Update Code
Entry Form - Third Attempt
// 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);
}
Delete Code
Entry Form - Third Attempt
<table>
<tr>
<th>SSN</th>
<th>First</th>
<th>Last</th>
<th>College</th>
</tr>
Insert Form Code
<%
// Iterate over the ResultSet
while ( rs.next() ) {
%>
Iteration Code
<%
}
%>
</table>
Presentation Code
Entry Form - Third Attempt
<tr>
<form action="students.jsp" method="get">
<input type="hidden" value="update" name="action">
<td><input value="<%= rs.getInt("SSN") %>" name="SSN"></td>
<td><input value="<%= rs.getString("ID") %>" name="ID"></td>
…
<td><input type="submit" value="Update"></td>
</form>
<form action="students2.jsp" method="get">
<input type="hidden" value="delete" name="action">
<input type="hidden" value="<%= rs.getInt("SSN") %>" name="SSN">
<td><input type="submit" value="Delete"></td>
</form>
</tr>
Iteration Code
Ad

Recommended

PPT
Web based development
Mumbai Academisc
 
PPS
Jdbc example program with access and MySql
kamal kotecha
 
PPT
Data Access with JDBC
BG Java EE Course
 
DOCX
Java beans
Sher Singh Bardhan
 
PPTX
Database Programming Techniques
Raji Ghawi
 
PDF
Advance Java Practical file
varun arora
 
KEY
JDBC Basics (In 20 Minutes Flat)
Craig Dickson
 
PDF
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
jillisacebi75827
 
PPT
JDBC – Java Database Connectivity
Information Technology
 
PDF
Java JDBC
Jussi Pohjolainen
 
DOC
Java database connectivity notes for undergraduate
RameshPrasadBhatta2
 
PPT
jdbc
vikram singh
 
PPS
Jdbc api
kamal kotecha
 
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
PPS
Jdbc session02
Niit Care
 
PPTX
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
tirthasurani118866
 
PPT
Jdbc
lathasiva
 
PPT
Jdbc day-1
Soham Sengupta
 
PDF
Get Back in Control of Your SQL with jOOQ at #Java2Days
Lukas Eder
 
PDF
Get Back in Control of your SQL
Java Usergroup Berlin-Brandenburg
 
PPTX
Jdbc Java Programming
chhaichivon
 
PPT
Executing Sql Commands
phanleson
 
PPT
Executing Sql Commands
leminhvuong
 
PPT
CS124-L9-JDBC.ppt Add more information to your upload
muzammil9676
 
PPT
JDBC JAVA DATABASE CONNECTIVITY AND JAVA
AdarshSrungarapu
 
PPT
statement interface
khush_boo31
 
PPT
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
PPT
c-step-by-step-execution power point presentation
10300PEDDIKISHOR
 

More Related Content

Similar to Java Database Connectivity Java Database (20)

PPT
JDBC – Java Database Connectivity
Information Technology
 
PDF
Java JDBC
Jussi Pohjolainen
 
DOC
Java database connectivity notes for undergraduate
RameshPrasadBhatta2
 
PPT
jdbc
vikram singh
 
PPS
Jdbc api
kamal kotecha
 
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
PPS
Jdbc session02
Niit Care
 
PPTX
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
tirthasurani118866
 
PPT
Jdbc
lathasiva
 
PPT
Jdbc day-1
Soham Sengupta
 
PDF
Get Back in Control of Your SQL with jOOQ at #Java2Days
Lukas Eder
 
PDF
Get Back in Control of your SQL
Java Usergroup Berlin-Brandenburg
 
PPTX
Jdbc Java Programming
chhaichivon
 
PPT
Executing Sql Commands
phanleson
 
PPT
Executing Sql Commands
leminhvuong
 
PPT
CS124-L9-JDBC.ppt Add more information to your upload
muzammil9676
 
PPT
JDBC JAVA DATABASE CONNECTIVITY AND JAVA
AdarshSrungarapu
 
PPT
statement interface
khush_boo31
 
JDBC – Java Database Connectivity
Information Technology
 
Java database connectivity notes for undergraduate
RameshPrasadBhatta2
 
Jdbc api
kamal kotecha
 
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Jdbc session02
Niit Care
 
preparecallablepptx__2023_09_11_14_40_58pptx__2024_09_23_11_14_59.pptx
tirthasurani118866
 
Jdbc
lathasiva
 
Jdbc day-1
Soham Sengupta
 
Get Back in Control of Your SQL with jOOQ at #Java2Days
Lukas Eder
 
Get Back in Control of your SQL
Java Usergroup Berlin-Brandenburg
 
Jdbc Java Programming
chhaichivon
 
Executing Sql Commands
phanleson
 
Executing Sql Commands
leminhvuong
 
CS124-L9-JDBC.ppt Add more information to your upload
muzammil9676
 
JDBC JAVA DATABASE CONNECTIVITY AND JAVA
AdarshSrungarapu
 
statement interface
khush_boo31
 

More from 10300PEDDIKISHOR (7)

PPT
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
PPT
c-step-by-step-execution power point presentation
10300PEDDIKISHOR
 
PPT
Structures-in-C programming with examples
10300PEDDIKISHOR
 
PPTX
file handling in c programming with file functions
10300PEDDIKISHOR
 
PPT
Unity JDBC ICEIS ppt UnityJDBC ICEIS ppt
10300PEDDIKISHOR
 
PPT
JDBC DriversPros and Cons of Each Driver
10300PEDDIKISHOR
 
PDF
Every SQL Query must have: • SELECT clause: specifies columns to be retained ...
10300PEDDIKISHOR
 
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
c-step-by-step-execution power point presentation
10300PEDDIKISHOR
 
Structures-in-C programming with examples
10300PEDDIKISHOR
 
file handling in c programming with file functions
10300PEDDIKISHOR
 
Unity JDBC ICEIS ppt UnityJDBC ICEIS ppt
10300PEDDIKISHOR
 
JDBC DriversPros and Cons of Each Driver
10300PEDDIKISHOR
 
Every SQL Query must have: • SELECT clause: specifies columns to be retained ...
10300PEDDIKISHOR
 
Ad

Recently uploaded (20)

PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PDF
Hurricane Helene Application Documents Checklists
Mebane Rash
 
PPTX
How to use search fetch method in Odoo 18
Celine George
 
PDF
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
PPTX
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
PPTX
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
PPTX
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
PPTX
How to use _name_search() method in Odoo 18
Celine George
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
PDF
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
PPTX
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PPTX
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
PPTX
List View Components in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
PDF
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
Hurricane Helene Application Documents Checklists
Mebane Rash
 
How to use search fetch method in Odoo 18
Celine George
 
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
How to use _name_search() method in Odoo 18
Celine George
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
How to Customize Quotation Layouts in Odoo 18
Celine George
 
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
List View Components in Odoo 18 - Odoo Slides
Celine George
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
Ad

Java Database Connectivity Java Database

  • 1. Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages (JSPs) Located @ DBLab Located @ Your PC Located @ Any PC
  • 4. import java.sql.*; class JdbcTest { public static void main (String args []) throws SQLException { // Load Oracle driver DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // Connect to the local database Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@myhost:1521:ORCL","scott", "tiger"); JDBC
  • 5. // Query the student names Statement stmt = conn.createStatement (); ResultSet rset = stmt.executeQuery ("SELECT name FROM Student"); // Print the name out //name is the 2nd attribute of Student while (rset.next ()) System.out.println (rset.getString (2)); //close the result set, statement, and the connection rset.close(); stmt.close(); conn.close();
  • 6. PreparedStatement Object If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. PreparedStatement updateStud = conn.prepareStatement( "UPDATE Student SET name = ? WHERE lastname LIKE ?"); updateStud.setString(1, “John”); updateStud.setString(2, “Smith”); updateStud.executeUpdate();
  • 7. PreparedStatement Object the following two code fragments accomplish the same thing: • Code Fragment 1: String updateString = "UPDATE COFFEES SET SALES = 75 " + "WHERE COF_NAME LIKE 'Colombian'"; stmt.executeUpdate(updateString); • Code Fragment 2: PreparedStatement updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? "); updateSales.setInt(1, 75); updateSales.setString(2, "Colombian"); updateSales.executeUpdate():
  • 8. • int getInt(int columnIndex) Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language. • int getInt(String columnName) • String getString(int columnIndex) • String getString(String columnName)
  • 9. Using Transactions When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. conn.setAutoCommit(false); .... transaction ... con.commit(); con.setAutoCommit(true);
  • 10. Using Transactions example con.setAutoCommit(false); PreparedStatement updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?"); updateSales.setInt(1, 50); updateSales.setString(2, "Colombian"); updateSales.executeUpdate(); PreparedStatement updateTotal = con.prepareStatement( "UPDATE COFFEES SET TOTAL = TOTAL + ? WHERE COF_NAME LIKE ?"); updateTotal.setInt(1, 50); updateTotal.setString(2, "Colombian"); updateTotal.executeUpdate(); con.commit(); con.setAutoCommit(true);
  • 11. Retrieving Exceptions JDBC lets you see the warnings and exceptions generated by your DBMS and by the Java compiler. To see exceptions, you can have a catch block print them out. For example, the following two catch blocks from the sample code print out a message explaining the exception: try { // Code that could generate an exception goes here. // If an exception is generated, the catch block below // will print out information about it. } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); }
  • 12. JSP Syntax • Comment – <%-- Comment --%> • Expression – <%= java expression %> • Scriplet – <% java code fragment %> • Include – <jsp:include page="relativeURL" />
  • 13. Entry Form - First Attempt
  • 14. Entry Form - First Attempt <b>Data Entry Menu</b> <ul> <li> <a href="courses.jsp">Courses<a> </li> <li> <a href="classes.jsp">Classes<a> </li> <li> <a href="students.jsp">Students<a> </li> </ul> Menu HTML Code
  • 15. Entry Form - First Attempt <html> <body> <table> <tr> <td> <jsp:include page="menu.html" /> </td> <td> Open connection code Statement code Presentation code Close connection code </td> </tr> </table> </body> </html> JSP Code
  • 16. Entry Form - First Attempt <%-- Set the scripting language to java and --%> <%-- import the java.sql package --%> <%@ page language="java" import="java.sql.*" %> <% try { // Load Oracle Driver class file DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); // Make a connection to the Oracle datasource Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@feast.ucsd.edu:1521:source", “user", “pass"); %> Open Connectivity Code
  • 17. Entry Form - First Attempt <% // 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"); %> Statement Code
  • 18. Entry Form - First Attempt <table> <tr> <th>SSN</th> <th>First</th> <th>Last</th> <th>College</th> </tr> <% // Iterate over the ResultSet while ( rs.next() ) { %> Iteration Code <% } %> </table> Presentation Code
  • 19. Entry Form - First Attempt
  • 20. Entry Form - First Attempt <tr> <%-- Get the SSN, which is a number --%> <td><%= rs.getInt("SSN") %></td> <%-- Get the ID --%> <td><%= rs.getString("ID") %></td> <%-- Get the FIRSTNAME --%> <td><%= rs.getString("FIRSTNAME") %></td> <%-- Get the LASTNAME --%> <td><%= rs.getString("LASTNAME") %></td> <%-- Get the COLLEGE --%> <td><%= rs.getString("COLLEGE") %></td> </tr> Iteration Code
  • 21. Entry Form - First Attempt <% // 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()); } %> Close Connectivity Code
  • 22. Entry Form - Second Attempt
  • 23. Entry Form - Second Attempt <html> <body> <table> <tr> <td> Open connection code Insertion Code Statement code Presentation code Close connection code </td> </tr> </table> </body> </html> JSP Code
  • 24. Entry Form - Second Attempt // 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); } Insertion Code
  • 25. Entry Form - Second Attempt <table> <tr> <th>SSN</th> <th>First</th> <th>Last</th> <th>College</th> </tr> Insert Form Code <% // Iterate over the ResultSet while ( rs.next() ) { %> Iteration Code <% } %> </table> Presentation Code
  • 26. Entry Form - Second Attempt <tr> <form action="students.jsp" method="get"> <input type="hidden" value="insert" name="action"> <th><input value="" name="SSN" size="10"></th> <th><input value="" name="ID" size="10"></th> <th><input value="" name="FIRSTNAME" size="15"></th> <th><input value="" name="LASTNAME" size="15"></th> <th><input value="" name="COLLEGE" size="15"></th> <th><input type="submit" value="Insert"></th> </form> </tr> Insert Form Code
  • 27. Entry Form - Third Attempt
  • 28. Entry Form - Third Attempt <html> <body> <table> <tr> <td> Open connection code Insertion Code Update Code Delete Code Statement code Presentation code Close connection code </td> </tr> </table> </body> </html> JSP Code
  • 29. Entry Form - Third Attempt // 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); } Update Code
  • 30. Entry Form - Third Attempt // 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); } Delete Code
  • 31. Entry Form - Third Attempt <table> <tr> <th>SSN</th> <th>First</th> <th>Last</th> <th>College</th> </tr> Insert Form Code <% // Iterate over the ResultSet while ( rs.next() ) { %> Iteration Code <% } %> </table> Presentation Code
  • 32. Entry Form - Third Attempt <tr> <form action="students.jsp" method="get"> <input type="hidden" value="update" name="action"> <td><input value="<%= rs.getInt("SSN") %>" name="SSN"></td> <td><input value="<%= rs.getString("ID") %>" name="ID"></td> … <td><input type="submit" value="Update"></td> </form> <form action="students2.jsp" method="get"> <input type="hidden" value="delete" name="action"> <input type="hidden" value="<%= rs.getInt("SSN") %>" name="SSN"> <td><input type="submit" value="Delete"></td> </form> </tr> Iteration Code