Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in datasource using SQLSERVER

 
Manish Sridharan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic