Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

datasource???

 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im testing datasource in tomcat using oracle(database).i configure the server.xml as well as the web.xml,but when i try to run my jsp page im getting an error message "the network adapter cannot make connection.....".But when i try to hard code it, the normal way of connecting to the database(Class.forname(),DriverManager.getConnection) it works fine.
here's my configuration of the server.xml and web.xml.
first, i added these code in the server.xml
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>

<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>bcms_owner</value>
</parameter>
<parameter>
<name>password</name>
<value>bcms_owner</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc racle:thin:@192.168.16.38:1521:BCMSDEV</value>
</parameter>
</ResourceParams>
</Context>
******************
then in the web.xml, i appended this
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
********************8

here's my jsp

<%@page import="javax.naming.*,javax.sql.*,java.sql.*"%>
<html>
<head><title>JSP Page</title></head>
<body>
<%
/* /****if i used this code it works fine***/
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc racle:thin:@192.168.16.38:1521:BCMSDEV","bcms_owner","bcms_owner");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from b_users");
if(rs.next()) {
System.out.println(rs.getString(1));
}
}
catch(Exception e)
{
e.printStackTrace();
}*/
/*** but with the datasource im getting an error ****.
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from b_users");
if(rs.next()) {
System.out.println(rs.getString(1));
}
}catch(Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
thanks in advance

raymond
 
kavitha rama
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your web.xml is right but I am not sure of server.xml, we use Oracle 9iAS, so we configure in data-sources.xml
Anyway try following in your code
InitialContext ctx=new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mmrrcdb");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic