TallerJavaAvanzado 2
TallerJavaAvanzado 2
Located Microsoft
@ Any PC Internet
Explorer
HTTP
Requests HTML
JDBC
Requests Tuples
Located Oracle
@ DBLab DB Server
Página Cliente
Java DataBase Connectivity (JDBC)
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");
// 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));
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:
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)
Utilizando Transacciones
conn.setAutoCommit(false);
....
transaction
...
con.commit();
con.setAutoCommit(true);
Utilizando Transacciones
Ejemplo
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);
Control de Excepciones
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
Comment
• <%-- Comment --%>
Expression
• <%= java expression %>
Scriplet
• <% java code fragment %>
Include
• <jsp:include page="relativeURL" />
Entry Form - First Attempt
Entry Form - First Attempt
<%
try {
// Load Oracle Driver class file
DriverManager.registerDriver
(new oracle.jdbc.driver.OracleDriver());
<%
// Iterate over the ResultSet
while ( rs.next() ) {
%>
Iteration Code
<%
}
%>
</table>
Entry Form - First Attempt
Entry Form - First Attempt
Iteration Code
<tr>
<%-- Get the SSN, which is a number --%>
<td><%= rs.getInt("SSN") %></td>
conn.setAutoCommit(false);
pstmt.setInt(1,Integer.parseInt(request.getParameter("SSN")));
pstmt.setString(2, request.getParameter("ID"));
…
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
}
Entry Form - Second Attempt
Presentation Code
<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>
Entry Form - Second Attempt
Insert Form Code
<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>
Entry Form - Third Attempt
Entry Form - Third Attempt
JSP Code
<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>
Entry Form - Third Attempt
Update Code
// Check if an update is requested
if (action != null && action.equals("update")) {
conn.setAutoCommit(false);
pstatement.setString(1, request.getParameter("ID"));
pstatement.setString(2, request.getParameter("FIRSTNAME"));
…
int rowCount = pstatement.executeUpdate();
conn.setAutoCommit(false);
conn.setAutoCommit(true);
}
Entry Form - Third Attempt
Delete Code
// Check if a delete is requested
if (action != null && action.equals("delete")) {
conn.setAutoCommit(false);
pstmt.setInt(1,
Integer.parseInt(request.getParameter("SSN")));
int rowCount = pstmt.executeUpdate();
conn.setAutoCommit(false);
conn.setAutoCommit(true);
}
Entry Form - Third Attempt
Presentation Code
<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>
Entry Form - Third Attempt
Iteration Code
<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>