# fehler bei db abfrage



## roland (2. Sep 2004)

hi folks!

ich hab nen für mich unerwarteten und unbekannten fehler aus dem ich einfach nicht schlau werde! es geht um einen "beantest"! der jsp code soll mit hilfe des beans zur mssql connecten!


hier mal aller dazugehöriger code:

jsp-code:

```
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

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

<html>
    <head><title>JSP Test</title></head>
    <body>
        <jsp:useBean id="c" scope="session" class="connection.Connect" />
        
        <%
        JspWriter mywriter = pageContext.getOut();
        Connection conn = c.getConnection();
        
        c.displayDbProperties();
        
        c.closeConnection();
        
        conn.close();
	%>
		
    </body>
</html>
```


bean:

```
package connection;


/**
 * @author ZLI-BLJ
 *
 * 


 * Diese Klasse ist das Interface für die Datenbank bezüglich Fragen.
 * </p>
 */
public class Connect {
	public java.sql.Connection con = null;

	public final String url = "jdbc:microsoft:sqlserver://";

	public final String serverName = "10.50.51.17";

	public final String portNumber = "1433";

	public final String databaseName = "EventoZLI";

	public final String userName = "sukarno";

	public final String password = "********"; 
	

	// Informs the driver to use server a side-cursor,
	// which permits more than one active statement
	// on a connection.
	public final String selectMethod = "cursor";

	// Constructor
	public Connect() {
	}

	public String getConnectionUrl() {
		return url + serverName + ":" + portNumber + ";databaseName="
				+ databaseName + ";selectMethod=" + selectMethod + ";";
	}

	public java.sql.Connection getConnection() {
		try {
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			con = java.sql.DriverManager.getConnection(getConnectionUrl(),
					userName, password);
			if (con != null)
				System.out.println("Connection Successful!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error Trace in getConnection() : "
					+ e.getMessage());
		}
		return con;
	}

	/*
	 * Display the driver properties, database details
	 */

	public void displayDbProperties() {
		java.sql.DatabaseMetaData dm = null;
		java.sql.ResultSet rs = null;
		try {
			con = this.getConnection();
			if (con != null) {
				dm = con.getMetaData();
				System.out.println("Driver Information");
				System.out.println("\tDriver Name: " + dm.getDriverName());
				System.out
						.println("\tDriver Version: " + dm.getDriverVersion());
				System.out.println("\nDatabase Information ");
				System.out.println("\tDatabase Name: "
						+ dm.getDatabaseProductName());
				System.out.println("\tDatabase Version: "
						+ dm.getDatabaseProductVersion());
				System.out.println("Avalilable Catalogs ");
				rs = dm.getCatalogs();
				while (rs.next()) {
					System.out.println("\tcatalog: " + rs.getString(1));
				}
				rs.close();
				rs = null;
				closeConnection();
			} else
				System.out.println("Error: No active Connection");
		} catch (Exception e) {
			e.printStackTrace();
		}
		dm = null;
	}

	public void closeConnection() {
		try {
			if (con != null)
				con.close();
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		Connect myDbTest = new Connect();
		myDbTest.displayDbProperties();
	}
}
```


fehlermeldung:

```
exception

org.apache.jasper.JasperException
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
	at java.lang.Thread.run(Thread.java:534)



root cause

java.lang.NullPointerException
	at org.apache.jsp.JDBC_Test2_jsp._jspService(JDBC_Test2_jsp.java:75)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
	at java.lang.Thread.run(Thread.java:534)
```


wäre euch für hilfe sehr dankbar!

mfg roland


----------



## foobar (2. Sep 2004)

Ich weiß zwar nicht ob das der Fehler ist, aber ich würde in der Methode DbProperties einen String zurückgeben und den dann in der Jsp ausgeben lassen.

```
public String displayDbProperties() 
	{
		StringBuffer out = new StringBuffer();
		 java.sql.DatabaseMetaData dm = null;
		 java.sql.ResultSet rs = null;
		 try {
			con = this.getConnection();
			if (con != null) {
			   dm = con.getMetaData();
			   out.append("Driver Information");
			   out.append("\tDriver Name: " + dm.getDriverName());
			   out.append("\tDriver Version: " + dm.getDriverVersion());
			   out.append("\nDatabase Information ");
			   out.append("\tDatabase Name: " + dm.getDatabaseProductName());
			   out.append("\tDatabase Version: "+ dm.getDatabaseProductVersion());
			   out.append("Avalilable Catalogs ");
			   rs = dm.getCatalogs();
			   while (rs.next()) {
				out.append("\tcatalog: " + rs.getString(1));
			   }
			   rs.close();
			   rs = null;
			   closeConnection();
			} else
			out.append("Error: No active Connection");
		 } catch (Exception e) {
			e.printStackTrace();
		 }
		 dm = null;
		 return out.toString();
	  }
```


```
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

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

<html>
    <head><title>JSP Test</title></head>
    <body>
        <jsp:useBean id="c" scope="session" class="connection.Connect" />
       
        <%
        JspWriter mywriter = pageContext.getOut();
        Connection conn = c.getConnection(); %>
       
<%=  c.displayDbProperties(); %>
       
<%   c.closeConnection();
        conn.close(); %>
      
    </body>
</html>
```


----------



## roland (2. Sep 2004)

also das funktioniert irgendwie nicht so ganz!

kannst du mir das nochmal erklären?

mfg roland


----------



## roland (2. Sep 2004)

noe ist alles klar!

hab jetzt aber neue fehler die ich auch nicht verstehe!


```
exception

org.apache.jasper.JasperException: connection.Connect.displayDbProperties()Ljava/lang/String;
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
	at java.lang.Thread.run(Thread.java:534)

root cause

javax.servlet.ServletException: connection.Connect.displayDbProperties()Ljava/lang/String;
	at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:536)
	at org.apache.jsp.JDBC_Test2_jsp._jspService(JDBC_Test2_jsp.java:83)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	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:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
	at java.lang.Thread.run(Thread.java:534)
```


gibts nicht irgend n fehlermeldung-nachschlagewerk?

danke für eure hilfe!

mfg roland


----------



## foobar (2. Sep 2004)

> gibts nicht irgend n fehlermeldung-nachschlagewerk?


Nein sowas gibts leider nicht ;-)
Lass dir mal alle Fehlermeldungen in den Catchblöcken, durch einen Logger in eine Datei schreiben. Du kannst dazu folgenden Code benutzen:

```
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Logger
{
	private PrintWriter pw;
	private static Logger logger;
	private SimpleDateFormat simpledate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");   //$NON-NLS-1$
	public Logger()
	{
		try
		{
			pw = new PrintWriter(new FileWriter("error.log" ,true),true);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static Logger getInstance()
	{
		if(logger == null)
		{
			logger = new Logger();
		}
		return logger;
	}
	
	public void log(String message)
	{
		pw.println("Date: " + simpledate.format(new Date()) + " " + message); //$NON-NLS-1$
	}
	
	public void log(StackTraceElement[] stacktrace)
	{
		for (int i =0; i < stacktrace.length; i++)
		{
			this.log("Class: " + stacktrace[i].getClassName() + " Method: " + stacktrace[i].getMethodName() + " Line: " + stacktrace[i].getLineNumber());//$NON-NLS-1$   //$NON-NLS-2$    //$NON-NLS-3$
		}
	}
	
	public PrintWriter getPrintWriter()
	{
		return this.pw;
	}
	
	public void close()
	{
		pw.close();
	}

}
```
Verwendung:

```
Logger.getInstance().log("Fehlermeldung");
```
HTH


----------



## Illuvatar (2. Sep 2004)

> gibts nicht irgend n fehlermeldung-nachschlagewerk?



Doch, die APIs.


----------



## akira (2. Sep 2004)

google ist auch nicht schlecht, jedoch würde ich den Stacktrace weglassen


----------



## Guest (2. Sep 2004)

Nur mal so ein Gedanke zur "Fehlerbehandlung".
Wenn Du als Entwickler nichts mit dem StackTrace anfangen kannst,
welchen Nutzen hat diese Consoleausgabe für den Anwender?

```
try { 
  ...
} 
catch (Exception e) { 
  e.printStackTrace(); 
}
```
Das ist keine Fehlerbehandlung. Es ist Abfangen von Fehlern, 
da die Syntax dies erzwingt.


----------



## roland (3. Sep 2004)

ich kann einfach den abgefangenen fehler nicht entschlüsseln was der bedeuten soll!

..aber hier reden ja alle nur um den heissen brei herum!

wie wärs den mit etwas konstruktivem?

wäre euch dankbar!

mfg roland


----------



## Guest (3. Sep 2004)

NullPointerException in JDBC_Test2_jsp.java in Zeile 75

Suche mal die Zeile, dann hast Du den Fehler.


----------

