[prev in list] [next in list] [prev in thread] [next in thread]
List: odtug-java-l
Subject: Re: sample jsp + oracle file?
From: rshea () att ! net
Date: 2004-12-21 5:00:31
Message-ID: F001.005F0BD9.20041220210031 () fatcity ! com
[Download RAW message or body]
Try the following:
Create a file called jdbc.jsp so it can be accessed using http://server/jdbc.jsp. \
Using the following code for the jsp:
<%@ page import="java.sql.*"%>
<%
//declaring these variable outside of the try so you can access them in finally \
section.
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//always have jdbc stuff in a try block
try{
//Loading the driver
Class.forName ("oracle.jdbc.driver.OracleDriver");
//getting the connection from the DriverManager
conn = DriverManager.getConnection( \
"jdbc:oracle:thin:@207.67.135.11:1521:orcl", "scott", "tiger" );
//Getting the statement from the connection, so we can execute queries
stmt = conn.createStatement ();
//Execute the query and grab the result set.
rs = stmt.executeQuery ("SELECT emp#, name, hire_date FROM EMP");
//so now we have the resultset, lets display it using HTML, using tables.
%>
<HTML><HEAD><TITLE> Oracle, JDBC, and JSP Tutorial </TITLE></HEAD>
<BODY>
<TABLE BORDER="1">
<!-- Creating the header for the information to be displayed -->
<TR>
<TD>EMP#</TD><TD>NAME</TD><TD>HIRE_DATE</TD>
</TR>
<% while( rs.next() ){%>
<TR>
<TD><%=rs.getInt("emp#")%></TD><TD><%=rs.getString("name")%></TD><TD><%=rs.getDate("hire_date")%></TD> \
</TR>
<%}%>
</TABLE>
</BODY>
</HTML>
<%
}catch(Exception e){
out.println("There was an exception. Stack trace will be printed to the error \
log."); e.printStackTrace();
}finally{
//this is important. You should close all three to free up resources. Always. \
In a finally block. rs.close();
stmt.close();
conn.close();
}
%>
Output should look something like:
EMP# NAME HIRE_DATE
3 KING 01-JAN-80
-------------- Original message ----------------------
>
> Ok, I can make my jsp and servlet connect using this code:
>
> DataSource dataSource = null;
> Connection con = null;
>
> try
> {
> DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
> con = DriverManager.getConnection
>
> ("jdbc:oracle:thin:@mycomputername:1521:orcl10g","myschema","mypassword");
> con = dataSource.getConnection();
> }
> catch ...
>
> However, I can't get this to work:
>
> DataSource dataSource = null;
> Connection con = null;
>
> try
> {
> Context initialContext = new InitialContext();
> dataSource = (DataSource)initialContext.lookup("MYCONNECTON_NAME");
> con = dataSource.getConnection();
> }
> catch ...
>
> It looks like I need to put something in the web.xml file, but I'm not sure what
> and how.
>
>
> Desktop 2005 - You're Virtually There! February 15-17, 2005
> Visit http://www.odtug.com for details. Computer + internet required.
> Keynote Speakers: Bill Inmon, Sohaib Abbasi and Thomas Kurian.
> --
> Author: david wendelken
> INET: davewendelken@earthlink.net
>
> Fat City Hosting, San Diego, California -- http://www.fatcity.com
> ---------------------------------------------------------------------
> To REMOVE yourself from this mailing list, send an E-Mail message
> to: ListGuru@fatcity.com (note EXACT spelling of 'ListGuru') and in
> the message BODY, include a line containing: UNSUB ODTUG-JAVA-L
> (or the name of mailing list you want to be removed from). You may
> also send the HELP command for other information (like subscribing).
Desktop 2005 - You're Virtually There! February 15-17, 2005
Visit http://www.odtug.com for details. Computer + internet required.
Keynote Speakers: Bill Inmon, Sohaib Abbasi and Thomas Kurian.
--
Author:
INET: rshea@att.net
Fat City Hosting, San Diego, California -- http://www.fatcity.com
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru@fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ODTUG-JAVA-L
(or the name of mailing list you want to be removed from). You may
also send the HELP command for other information (like subscribing).
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic