Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: Strange JavaBean Query Problem

Status
Not open for further replies.

gargon1

IS-IT--Management
Jan 24, 2003
15
0
0
VI
Hello,

When I try to return all items in a MySQL database I keep getting the error below. However, when I change the SQL query string to "SELECT dept FROM time_sheet" in the javaBean and make necessary changes in the jsp file it works fine and all departments show up on the web page.
Strangely, it only works when I use dept in the query.
It does not work for any other field names, eg. lname, fname etc. When I try different field names I get an error saying "Column 'fname' not found".

I have include the jsp and javaBean code below the error. Any help would be greatly appreciated.



C:\Tomcat 4.1\webapps\timeclock\WEB-INF\classes\com\time>java timeclock
Exception in thread "main" java.lang.NoClassDefFoundError: timeclock (wrong name
: com/time/timeclock)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)


JAVA_BEAN+++++++++++++++++++++++++++++++++++++++++++
// timeclock.java
package com.time;

import java.sql.*;
import java.util.*;


public class timeclock {

String error;
Connection con;

public timeclock() { }

public void connect() throws ClassNotFoundException,
SQLException,
Exception {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("JDBC driver loaded");

con = DriverManager.getConnection
("jdbc:mysql://mon1/time_clock?user=username&password=");
System.out.println("Database connection established");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT fname FROM time_sheet");
//ResultSet rs= stmt.executeQuery("Select fname, lname FROM time_sheet");
while (rs.next()) {
String title = rs.getString("fname");
//String title = rs.getString("lname");
//float price = rs.getFloat("Price");
System.out.println(title);
}
} catch (ClassNotFoundException cnfe) {
System.out.println("ClassNotFoundException: Could not locate driver");
} catch (SQLException cnfe) {
System.out.println("SQLException: Could not connect to database");
} catch (Exception e) {
System.out.println
("An unknown error occurred while connecting to database");
}
}

public void disconnect() throws SQLException {
try {
if ( con !=null ) {
con.close();
}
} catch(SQLException sqle) {
System.out.println("Unable to close database connection.");
}
}

public ResultSet viewUsers() throws SQLException, Exception {
ResultSet rs= null;
try {
String queryString=("SELECT fname FROM time_sheet");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
} catch (SQLException sqle) {
error = "SQLException: Could not execute query.";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception has occured while retrieving departments,";
throw new Exception(error);
}
return rs;
}

public static void main (String[] args)
{
timeclock sdb = new timeclock();

try
{
sdb.connect();

}
catch (Exception e)
{
e.printStackTrace();
}
}

JSP+++++++++++++++++++++++++++++++++++++++++++++++++++++
<%@page contentType=&quot;text/html&quot; import=&quot;java.sql.*, java.util.*&quot; %>
<jsp:useBean id=&quot;mytime&quot; class=&quot;com.time.timeclock&quot;/>

<html>
<head><title>JSP Page</title></head>
<body>
<H1>BEAN TEST</H1>
<table border=&quot;1&quot;>
<tr>
<td><b><B>Clock Number:</b></td>
<td><b><B>Date:</b></td>
<td><b><B>Time:</b></td>
<td><b><B>User ID:</b></td>
<td><b><B>First Name:</b></td>
<td><b><B>Last Name:</b></td>
<td><b><B>Departments:</b></td>
<td><b><B>Status:</b></td>
<td><b><B>Record Number:</b></td>
</tr>
<%
mytime.connect();
ResultSet rs = mytime.viewUsers();
while (rs.next()) {
%>
<tr>
<td><%= rs.getString(&quot;fname&quot;) %></td>
</tr>
<%
}
%>

</table>

<%-- <jsp:useBean id=&quot;beanInstanceName&quot; scope=&quot;session&quot; class=&quot;package.class&quot; /> --%>
<%-- <jsp:getProperty name=&quot;beanInstanceName&quot; property=&quot;propertyName&quot; /> --%>

</body>
</html>

 
sql server is case sensitive. try and list out all the column names for your time_sheet table by bringing up query analyzer from the SQL Server Mgr Group. Copy the select statement that you use from your java file and paste it into the query analyzer. See if it returns a resultset. if it says lname does not exist, then you know the error is in the database.



~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top