User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 456,472 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,795 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 1244 | Replies: 3
Reply
Join Date: Sep 2007
Posts: 7
Reputation: MonkeyGarage is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MonkeyGarage MonkeyGarage is offline Offline
Newbie Poster

Having an issue with connecting to MySQL

  #1  
Sep 13th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 7
Reputation: MonkeyGarage is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MonkeyGarage MonkeyGarage is offline Offline
Newbie Poster

Re: Having an issue with connecting to MySQL

  #2  
Sep 13th, 2007
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(); 
         } 
    }
	
}

Reply With Quote  
Join Date: Sep 2007
Posts: 7
Reputation: MonkeyGarage is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MonkeyGarage MonkeyGarage is offline Offline
Newbie Poster

Re: Having an issue with connecting to MySQL

  #3  
Sep 13th, 2007
well, nevermind. I downloaded a different connector and didn't have any issues
Reply With Quote  
Join Date: May 2008
Posts: 2
Reputation: dnanetwork is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dnanetwork dnanetwork is offline Offline
Newbie Poster

Re: Having an issue with connecting to MySQL

  #4  
May 23rd, 2008
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>");
%>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 2:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC