classes:general:dbsamples:jsp_oracle
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
classes:general:dbsamples:jsp_oracle [2017/01/14 19:45] – created localadmin | classes:general:dbsamples:jsp_oracle [2017/01/14 19:52] (current) – [The Class (Bean)] localadmin | ||
---|---|---|---|
Line 2: | Line 2: | ||
There are two distinct methods for connecting to the database using JSP. The JSP will be compiled into a servlet, and that happens automatically each time you change the code. The first method makes database connections in the .jsp page itself, while the second one sets up the database connection in a separate class (called a bean in Java server programming). | There are two distinct methods for connecting to the database using JSP. The JSP will be compiled into a servlet, and that happens automatically each time you change the code. The first method makes database connections in the .jsp page itself, while the second one sets up the database connection in a separate class (called a bean in Java server programming). | ||
+ | |||
+ | ===== Connecting within JSP page ===== | ||
+ | ---- | ||
+ | Here is an example for connecting to the Oracle database through JSP. This approach mixes the presentation and logic. | ||
+ | |||
+ | This is very similar to accessing the database through a java application, | ||
+ | |||
+ | This method is **not recommended** as it may expose your username and password if you are using a shared environment such as the department lab. This is because the public_html directory cannot be read protected and still serve its purpose. | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | <%@ page import=" | ||
+ | <%@ page import=" | ||
+ | |||
+ | <% | ||
+ | // | ||
+ | Class.forName(" | ||
+ | |||
+ | // Initialize connection to database | ||
+ | // Remember to change username and password | ||
+ | Connection conn = DriverManager.getConnection(" | ||
+ | |||
+ | // Create a statement and a query, execute the query | ||
+ | Statement stmt = conn.createStatement(); | ||
+ | |||
+ | String query = ""; | ||
+ | |||
+ | ResultSet rset = stmt.executeQuery(query); | ||
+ | |||
+ | // Iterate through all result rows | ||
+ | while(rset.next()) | ||
+ | { | ||
+ | // Print the first item of the result | ||
+ | out.println(rset.getString(1)); | ||
+ | |||
+ | // Print a line break | ||
+ | out.println("< | ||
+ | } | ||
+ | |||
+ | // Clean up | ||
+ | stmt.close(); | ||
+ | rset.close(); | ||
+ | conn.close(); | ||
+ | |||
+ | %> | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Connecting using a Bean ===== | ||
+ | ---- | ||
+ | If you want to split presentation and database access to make a 3-tier application (presentation, | ||
+ | |||
+ | - A class (called a ' | ||
+ | - A JSP page to call functions of the class and display query results | ||
+ | |||
+ | The logic layer (database queries and other logic) will be implemented in a class, the presentation layer (your JSP/HTML) will call functions of the class, and the class will do the work of getting data from the database. | ||
+ | |||
+ | ==== The Class (Bean) ==== | ||
+ | |||
+ | You will need a class for your logic layer. This class needs to be part of a package otherwise the server won't be able to access it. | ||
+ | |||
+ | Here is an example class: | ||
+ | < | ||
+ | package beans; | ||
+ | |||
+ | import java.io.*; | ||
+ | import java.sql.*; | ||
+ | import java.util.*; | ||
+ | |||
+ | public class DBCon | ||
+ | { | ||
+ | public DBCon() | ||
+ | { | ||
+ | // empty constructor | ||
+ | } | ||
+ | |||
+ | public ArrayList getRecords() | ||
+ | throws SQLException, | ||
+ | { | ||
+ | ArrayList records = new ArrayList(); | ||
+ | // Load Oracle driver | ||
+ | Class.forName(" | ||
+ | |||
+ | // Initialize connection to database | ||
+ | // Remember to change username and password | ||
+ | Connection conn = DriverManager.getConnection(" | ||
+ | |||
+ | // Create a statement and a query, execute the query | ||
+ | Statement stmt = conn.createStatement(); | ||
+ | |||
+ | String query = ""; | ||
+ | |||
+ | ResultSet rset = stmt.executeQuery(query); | ||
+ | |||
+ | // Iterate through all result rows | ||
+ | while(rset.next()) | ||
+ | { | ||
+ | records.add(rset.getString(1)); | ||
+ | } | ||
+ | |||
+ | // Clean up | ||
+ | stmt.close(); | ||
+ | rset.close(); | ||
+ | conn.close(); | ||
+ | |||
+ | return records; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This class is part of the package ' | ||
+ | |||
+ | Once compiled, this class needs to be placed in the folder: / | ||
+ | |||
+ | ==== The JSP page ==== | ||
+ | |||
+ | The JSP page will be placed in the public_html directory, and can be reached through the URL http:// | ||
+ | |||
+ | To use the database functions defined in your bean, you need to use a special JSP directive. | ||
+ | |||
+ | You can import packages using the syntax: | ||
+ | < | ||
+ | <%@page import=" | ||
+ | </ | ||
+ | This imports java.util.* | ||
+ | |||
+ | To import your bean, use the syntax: | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | This defines an object called ' | ||
+ | |||
+ | This is similar to the java: | ||
+ | < | ||
+ | DBCon dbcon = new DBCon(); | ||
+ | </ | ||
+ | |||
+ | in that it makes a new object for code the use. The difference is that jsp:useBean is used for JSP pages to load Beans. | ||
+ | |||
+ | Here is a small JSP page that loads the DBCon bean and calls the function getRecords(). Note that we do not specify ANY database connection information here, that's taken care of in the DBCon bean. | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | |||
+ | <%@page import=" | ||
+ | < | ||
+ | |||
+ | <% | ||
+ | // Get the first item of each record by calling a function of the DBCon class | ||
+ | ArrayList records = dbcon.getRecords(); | ||
+ | |||
+ | // Go through each record and print | ||
+ | for(int i = 0; i < records.size(); | ||
+ | { | ||
+ | // Print out each item in the ' | ||
+ | out.println(" | ||
+ | out.println("< | ||
+ | } | ||
+ | |||
+ | %> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
classes/general/dbsamples/jsp_oracle.1484444739.txt.gz · Last modified: 2017/01/14 19:45 by localadmin