Database Access Using JSP
Database Access Using JSP
Objectives:
Understand JDBC Implement the use of Java Database Connectivity Grasp the concept of connection pooling Create simple application illustrating basic database interactions
Concepts
Almost all applications must deal with saving and preserving data. Although some information can be stored in property files and the system environment using a database is a natural way to ease development and increase the scalability of an application. Even if an application isnt primarily designed for storing and cataloging information, it can still benefit from having easy access to data storage and retrieval. Databases are the lifeblood of many systems and applications and being able to understand them and manipulate them comfortably is important to the career of a software engineer. Data management is such a large task that a database team is an integral part of any medium-to-large sized organization and an organization can have a number of enterprise database systems.
Class.forName( ) forces the class to be dynamically loaded into the Java runtime environment You establish a connection by calling the getConnection method on the java.sql.DriverManager object. o
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" %> <%@ page import="java.io.*" %> <%@ page errorPage="JdbcConnectError.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sample Database Connection in JSP</title> </head> <body> <h1> Sample Database Connection</h1> <h2> Connection Status</h2> <% Connection connection;
Includes the connection method, the database driver type, the machine name with the database followed by a colon and the port number the database is listening to, the default startup database and username and password for the database.
Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ cargo", "root", ""); connection.close(); %> <p> If this message displays in the browser, <br /> your MySQL JDBC connection is set up properly. </p> </body> </html>
Create a connection by using getConnection( ) method
JdbcConnectError.jsp
<%@ page isErrorPage="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JDBC Connect</title> </head> <body> <h3>JDBC Connect Error</h3> <table width="600"> <tr> <td>There are several reasons a JDBC connection can fail:</td> </tr> <tr> <td> <ul> <li>The MySQL database is not installed</li> <li>The MySQL database is not running</li> <li>The JDBC Driver is not installed</li> <li>The JDBC Driver is not in the CLASSPATH of the JavaServer Engine</li> <li>The naming scheme of the downloaded JDBC driver is different <br /> compared with the name found in the Java code that invokes the driver by name </li> </ul> </td> </tr> <tr> <td> Unfortunately, the above explanations are probably more lucid<br /> than the usually cryptic exception message </td> </tr> <tr> <td> JdbcSetupTest.jsp threw this exception report: <br /> <%= exception %> </td> </tr> </table> </body> </html>