I get the following when attempting to view a project I have, I have MySQL 5.0 and Tomcat 6.0. Both services are running. As far as MySQL connectors, I have both "mysql-connector-java-5.0.7-bin" and "mysql-connector-java-3.0.8-stable-bin". This has not worked with either one.

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 8

5: </head><body>
6: <%
7: DBConnect d = new DBConnect();
8: ResultSet rs = d.getResult("SELECT * FROM items");
9: //this is where you loop over your results
10: 
11: while (rs.next()) {


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.NullPointerException
    com.monkeygarage.DBConnect.getResult(Unknown Source)
    org.apache.jsp.test_jsp._jspService(test_jsp.java:62)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
Apache Tomcat/6.0.14

and here is my code to connect to the DB, minus correct login info:

package com.monkeygarage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnect {

	private Connection conn;

	private String db = "";

	private String host = "l";

	private String pass = "";

	private Statement statement;

	private String user = "";

	/**
	 * 
	 */
	public DBConnect() {
		connect();
	}

	private void connect() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			// create connection string
			String connUrl = "jdbc:mysql://" + this.host + "/" + this.db + "?user="
					+ this.user + "&password=" + this.pass;

			// pass database parameters to JDBC driver
			this.conn = DriverManager.getConnection(connUrl);
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}

	public void disconnect() {
		try {
			this.statement.close();
			this.conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public ResultSet getResult(String sql) throws SQLException {
		if (this.statement != null) {
			this.statement.close();
		}
		this.statement = this.conn.prepareStatement(sql);
		this.statement.setMaxRows(1);
		return this.statement.executeQuery(sql);
	}

	 public static void main(String[] args) { 
         try { 
              DBConnect d = new DBConnect(); 
              ResultSet rs = d.getResult("SELECT * FROM items"); 

              while (rs.next()) { 
                   String name = rs.getString("name"); 
                   System.out.println(name); 
              } 
         } catch (SQLException ex) { 
              ex.printStackTrace(); 
         } 
    }
	
}

well, nevermind. I downloaded a different connector and didn't have any issues

In the following examples, please substitute your information where the following data is referenced:

<server>: enter the MySQL server that you are assigned to, for example, mysql4.safesecureweb.com
<username>: enter the username provided for your database
<password>: enter the password provided for your database
<database>: enter the database name provided for your database
<DSN>: enter the DSN name (ColdFusion only)
PHP
<?php
$link = mysql_connect('<server>', '<username>', '<password>');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(<database>);
?>

ColdFusion
<CFQUERY Name="test" DATASOURCE="<DSN>" USERNAME="<username>" PASSWORD="<password>">
</CFQUERY>

Perl
#!/usr/bin/perl

use DBI;

$db = DBI->connect("dbi:mysql:<database>","<username>","<password>")
or die("Couldn't connect");

$db->disconnect;

JSP
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>

<%!
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn;
conn = DriverManager.getConnection(
"jdbc:mysql://<server>/<database>?user=<username>&password=<password>");
%>

commented: DB connection from JSP is very-very bad idea -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.