By: Bruce W. Perry in JSP Tutorials on 2008年11月24日 [フレーム]
Example below imports the necessary classes at the top of the code using the pagedirective and its import attribute. Otherwise, this JSP accomplishes everything that the servlet of the prior recipe does, including the display of nearly identical output in the web browser.
<%@ page import="java.util.Hashtable,java.sql.*,javax.naming.*,javax.sql.*" %> <html> <head><title>Database Query in WebLogic</title></head> <body> <h2>Querying a database with a JSP in WebLogic</h2> <% Context env = null; DataSource pool = null; Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL,"t3://localhost:7001"); env = new InitialContext(ht); //Lookup this DataSouce at the top level of the WebLogic JNDI tree pool = (DataSource) env.lookup ("oracle-8i-athletes"); String sql = "select * from athlete"; Connection conn = null; Statement stmt = null; ResultSet rs = null; ResultSetMetaData rsm = null; %> <table border='1'><tr> <% try{ //get a java.sql.Connection from the pool conn = pool.getConnection(); stmt = conn.createStatement();//create a java.sql.Statement //execute a SQL statement,generating a ResultSet rs = stmt.executeQuery(sql); rsm = rs.getMetaData(); int colCount = rsm.getColumnCount(); //print column names for (int i = 1; i <=colCount; ++i) { %> <th><%=rsm.getColumnName(i)%> </th> <% } %> </tr> <% while( rs.next()){ %> <tr> <% for (int i = 1; i <=colCount; ++i) { %> <td> <%= rs.getString(i) %> </td> <%}//for %> </tr> <%} //while } catch (Exception e) { throw new JspException(e.getMessage()); } finally { try{ stmt.close(); conn.close(); } catch (SQLException sqle){ } } %> </body> </html>
After making sure that you have properly configured the connection pool and DataSource in the WebLogic console, view this JSP's output by copying it to WebLogic's default web application, then request a URL in your browser that looks like this one: http://localhost:7001/sqlWeblogic.jsp.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in JSP )
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Steps to get a Free SSL certificate for your Tomcat
Uploading a file to a server using JSP
Server Side Programming using JSP
Latest Articles (in JSP)
Show a calendar for user input in JSP
Steps to get a Free SSL certificate for your Tomcat
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
A JSP page that gets properties from a bean
Scriptlets and Expressions in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
Steps to get a Free SSL certificate for your Tomcat
JSP Tags for SQL to connect to a database
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate