0% found this document useful (0 votes)
90 views

Database Access Using JSP

Uploaded by

ErwinMacaraig
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Database Access Using JSP

Uploaded by

ErwinMacaraig
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JSP: Database Access

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.

Database Connectivity and JDBC


When a database starts, it usually services requests by listening to a network port, often using TCP/IP. The situation is analogous to a web server that starts, listens to port 80 and then responds to requests. However, unlike a web server, no two databases speak exactly the same language. Even a complex application that deals with raw streams of data needs a translator to facilitate the connection to the database. That translator mechanism is a database driver. The translation layer can be a call interface that is compiled into a program or it can be an ODBC (open database connectivity) or a JDBC driver. Compiling a call interface into a program is the most fossilized approach; however, it is suitable for rare occasions when the interfaces can be determined in advanced and when the extra time it takes to load a driver is unacceptable. JDBC takes advantage of Javas virtual machine layer and Javas ability to produce write-once run-anywhere code. Today, every major database maker provides pure Java database drivers. The JDBC classes and objects related to the user of JDBC are found in the java.sql and javax.sql packages. How JDBC Starts You must explicitly load a JDBC driver into the DriverManager in the Java runtime environment. o Use the general dynamic class loading method java.lang.Class.forName() Class.forName(com.mysql.jdbc.Driver)

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.

Load JDBC Driver com.mysql.jdbc.Driver

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>

You might also like