aspose file tools*
The moose likes JDBC and the fly likes Error in datasource using SQLSERVER Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Error in datasource using SQLSERVER" Watch "Error in datasource using SQLSERVER" New topic
Author

Error in datasource using SQLSERVER

Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
I am trying to use connection pooling for connecting sqlserver2000 on tomcat webserver using datasource

i make a following entry in server.xml

Host name="localhost" appBase="webapps" debug="0" unpackWARs="false">
- <!-- Insert <Context> Elements Here As Needed
-->
- <Context path="/myproject/me" docBase="d:\gaurav\Project1">
<Resource name="jdbc/LyricNote" auth="Container" type="javax.sql.DataSource" />
- <ResourceParams name="jdbc/Lyricnote">
- <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>30000</value>
</parameter>
- <parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
- <parameter>
<name>username</name>
<value>gaurav</value>
</parameter>
- <parameter>
<name>password</name>
<value>nigam</value>
</parameter>
- <parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
- <parameter>
<name>url</name>
<value>dbc:microsoft:sqlserver://SERVER:1433;DatabaseName=master</value>
</parameter>
- <parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
- <parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
- <parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
</Context>
**************************

in web.xml i write

<resource-ref>
<description> data source connection for myjdbc2</description>
<res-ref-name>jdbc/LyricNote</res-ref-name>
<res-type>javax.sql.Datasource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

***********************
in jsp i write

<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body>

<%@ page import="java.sql.*,javax.naming.*,javax.sql.*"%>

<%
Connection con=null;
Statement st=null;
ResultSet rs=null;

try{
InitialContext initctx= new InitialContext();
Context ctx=(Context)initctx.lookup("java:/comp/env");
DataSource ds = (DataSource)ctx.lookup("jdbc/LyricNote");


con=ds.getConnection();
st=con.createStatement();
rs=st.executeQuery("select * from sysfiles");
while(rs.next())
{
String str= rs.getString(1);
%>
<%=str %>
<%
System.out.println(str);
}

}catch(Exception e){System.out.println("Error "+ e);}
finally{rs.close();
st.close();
con.close();
}
%>

</body>
</html>
*******************************
at run time it is giving stack of errors




java.lang.NullPointerException
at org.apache.jsp.jdbc3$jsp._jspService(jdbc3$jsp.java:114)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet$JspServletWrapper.service(IDEJspServlet.java:173)
at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet.serviceJspFile(IDEJspServlet.java:246)
at org.netbeans.modules.tomcat.tomcat40.runtime.IDEJspServlet.service(IDEJspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:226)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.netbeans.modules.web.monitor.catalina.MonitorValve.invoke(MonitorValve.java:148)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2349)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:469)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1040)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1151)
at java.lang.Thread.run(Thread.java:534)


Manish S.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Gaurav,
Welcome to JavaRanch!

Do you know which line the exception is getting thrown on? You can figure this out by inserting printlns or commenting out code.

In your finally block, use an if statement to check for nulls before closing. I think the null pointer is getting thrown there and masking the real error.

Note that it is good practice to keep code (especially database code) out of a JSP. Instead, put it in a servlet or another class called by the servlet.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
 
subject: Error in datasource using SQLSERVER