JSP Short Tutorial
JSP Short Tutorial
By Emmanuel MASABO
Three-Tier Architecture
Located @ Any PC HTTP Requests Located @ Your PC Microsoft Internet Explorer HTML
Located @ DBLab
Oracle DB Server
JDBC
import java.sql.*; class JdbcTest { public static void main (String args []) throws SQLException { try{ // Connect to the local database Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student s_db","root",""); // Query the student first names Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ("SELECT * FROM Students");
// Print the first name out //firstname is the 3rd attribute of Student while (rset.next ()) System.out.println(rset.getString(3)); //close the result set, statement, and the connection rset.close(); stmt.close(); conn.close(); } catch(SQLException err){ System.out.println(err.getMessage()); } } }
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 Students SET first_name = ? WHERE last_name 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)
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.
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" />
try {
// Make a connection to the MySql datasource Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3 306/students_db","root",""); %>
NB:In the case we get no suitable driver message, we can use Class.forName("com.mysql.jdbc.Driver").newInstance(); before connection or we can go to services, drivers and add mysql driver. Dont forget to add the mysql connect driver also in the library folder.