LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2004, 03:25 PM   #1
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
Cannot instantiate class from JSP


Well... I have a JSP. This JSP instanciates a class called ReportCard which I located in WEB-INF/classes/db/ReportCard. In the ReportCard java file, I added "package db".

Everything works ok with that. However, in the same directory (WEB-INF/classes/db/) I have another class that connects to a server. I want that one to instanciate another object of class ReportCard.

Nevertheless, when I try to compile that class (QuestionGetter.class) I get the following:

Code:
bash-2.05b$ javac QuestionGetter.java
QuestionGetter.java:338: cannot resolve symbol
symbol  : class ReportCard
location: class db.QuestionGetter
                        ReportCard report = new ReportCard();
That happens even when I tried writing (even though I don't think you should import your own package)

Code:
import java.db.*;
I'm not sure what I can do. I think that as I have to compile it, the db package must be located in some directory in the classpath. However, I do not have any variable called CLASSPATH. If I go like

Code:
export CLASSPATH=/usr/local/tomcat/webapps/examen/WEB-INF/classes
It doesn't work either.

The only workaround I've gotten is to include both QuestionGetter and ReportCard in the same .java. However, that's not a solution because I need the ReportCard class to be public so that I can instantiate it from the JSP mentioned above.

What can I do? Please help.

Here you have both source codes if you need them:

1. QuestionGetter.java
Code:
package db; 
 
import java.util.*; 
import java.sql.*; 
import java.io.*; 
 

public class QuestionGetter 
{		
	String driverName = "com.mysql.jdbc.Driver"; 
	String dbrmUrl = "jdbc:mysql://localhost/examen";
	String dbUsername = "root"; 
	String dbPassword = ""; 
	
	final static int QUESTION = 1; 
	final static int ANSWER = 0; 
	
	
	Connection conn; 
	PreparedStatement pstmt; 
	
	public QuestionGetter()
	{
		startConnection(); 
	}
	
	public void startConnection()
	{
		try
		{
		
			
			Class.forName(driverName); 
			conn = DriverManager.getConnection(dbrmUrl,dbUsername, dbPassword); 
			
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
		
	}
 
	public boolean hasAnsweredSection(String Section, String UID)
	{
		if (conn == null)
			startConnection();
		try
		{	
			pstmt = conn.prepareStatement("SELECT DISTINCT tblQuestions.SID FROM tblUserAnswers INNER JOIN tblQuestions on tblUserAnswers.QID = tblQuestions.QID WHERE UID = ?"); 
 
			pstmt.setString(1, UID); 
			
			ResultSet rs = pstmt.executeQuery(); 
			
			while (rs.next())
			{
				if (rs.getString("SID").equals(Section)) 
				{
					return true; 
				}
			}
			return false;
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
			
		return false; 
		
	}
		
	public void storeUserAnswer(String UIDstring, String QIDstring, String AIDstring)
	{
		if (conn == null)
			startConnection();
	
		boolean isRight = false; 
		
		try
		{	
		
			int UID = Integer.parseInt(UIDstring); 
			int QID = Integer.parseInt(QIDstring); 
			int AID = Integer.parseInt(AIDstring); 
			
			pstmt = conn.prepareStatement("SELECT C_AID FROM tblQuestions WHERE QID = ?"); 
			pstmt.setInt(1, QID); 
			
			ResultSet rs = pstmt.executeQuery(); 
			
			rs.next(); 
			int resultado = rs.getInt("C_AID"); 
			
			if (AID == resultado)
				isRight = true; 
					
			pstmt = conn.prepareStatement("INSERT INTO tblUserAnswers (UID, QID, AID, IsRight) VALUES (?, ?, ?, ?)"); 
			
			pstmt.setInt(1, UID); 
			pstmt.setInt(2, QID); 
			pstmt.setInt(3, AID); 
			pstmt.setString(4, isRight ? "Y" : "N" ); 
			
 
			pstmt.executeUpdate(); 		
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
				
	}
	
	public int getMaxSections()
	{
		if (conn == null)
			startConnection();
	
		try
		{	
			pstmt = conn.prepareStatement("SELECT SID FROM tblSections ORDER BY SID DESC LIMIT 1"); 
 
			ResultSet rs = pstmt.executeQuery(); 
			
			rs.next(); 
			int resultado = rs.getInt("SID"); 
			return resultado;
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
		
		return -1; 
		
	}
	
	public String getSectionName(String SID)
	{
		if (conn == null)
			startConnection();
	
		try
		{	
			pstmt = conn.prepareStatement("SELECT Description FROM tblSections WHERE SID = ?");
			pstmt.setString(1, SID); 
			
			ResultSet rs = pstmt.executeQuery(); 
			
			rs.next(); 
			String resultado = rs.getString("Description"); 
			return resultado;
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
		
		return null; 
		
	}
	public Vector getQuestions(String section) 
	{
	
		if (conn == null)
			startConnection();
	
		try
		{	
			pstmt = conn.prepareStatement("SELECT * FROM tblQuestions WHERE SID = ? ");
			pstmt.setString(1, section); 
						
			return this.toVector(pstmt.executeQuery(), QUESTION);  
		}
		catch (Exception e) 
		{
			System.err.println(e.getMessage()); 
		}
		
		return null; 
	} 
	
	public Vector getAnswersforQuestion(String QID)
	{
		try
		{			
			if (conn == null)
				startConnection();
			
			pstmt = conn.prepareStatement("SELECT * FROM tblAnswers WHERE QID = ? ");
			pstmt.setString(1, QID); 
			
			return this.toVector(pstmt.executeQuery(), ANSWER);  
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
		return null; 
	}
		
	private Vector toVector(ResultSet rs, int type)
	{
		Vector vector = new Vector(); 
		try
		{
			
			while (rs.next())
			{
				
				Hashtable hashtable = new Hashtable(); 
				
				if (type == QuestionGetter.QUESTION)
				{
					hashtable.put("QID", rs.getString("QID") );
					hashtable.put("Question", rs.getString("Question") ); 
 
					if (rs.getString("Image") != null) 	hashtable.put("Image", 		rs.getString("Image")); 
					else
						hashtable.put("Image", ""); 
					
					
					hashtable.put("SID", rs.getString("SID")); 
					
					hashtable.put("C_AID", rs.getString("C_AID") ); 			
	
					vector.add(hashtable); 
			
				}
				else if (type == QuestionGetter.ANSWER)
				{
					hashtable.put("AID", rs.getString("AID") ); 
					hashtable.put("QID", rs.getString("QID") ); 
					hashtable.put("Ans_Letter", rs.getString("Ans_Letter")); 
					hashtable.put("Answer", rs.getString("Answer")); 
								
					vector.add(hashtable); 
				
				}
			}
		}
		catch (Exception e) 
		{
			System.err.println(e.getMessage()); 
		}		
		stopConnection(); 
 
		return vector; 
	
	}
	
	public float getAverage()
	{
		try
		{	
		
			int rightOnes = 0; 
			int totalUsers = 0; 
			int totalQuestions = 0; 
			int totalOnes = 0; 
					
			if (conn == null)
				startConnection();
			
			
			pstmt = conn.prepareStatement("SELECT SUM(IsRight) AS 'RIGHT' from tblUserAnswers where IsRight = 'Y' GROUP BY IsRight");
 
						
			ResultSet rs = pstmt.executeQuery(); 
			rs.next(); 
			rightOnes = rs.getInt("RIGHT"); 
			
 
			pstmt = conn.prepareStatement("SELECT COUNT(UID) FROM tblUsers GROUP BY UID");
 
			rs = pstmt.executeQuery(); 
			while (rs.next()) 
				totalUsers++; 
										
			pstmt = conn.prepareStatement("SELECT COUNT(QID) FROM tblQuestions GROUP BY QID");
 
			rs = pstmt.executeQuery(); 
			while (rs.next()) 
				totalQuestions++; 
		
			totalOnes = totalQuestions * totalUsers; 
						
			return (float) ((double) ((double) rightOnes / (double) totalOnes) * 100); 
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
		return -1; 
	
	}
	
	public Vector createReports()
	{	
		int totalQuestions = 0; 
	
		pstmt = conn.prepareStatement("SELECT COUNT(QID) FROM tblQuestions GROUP BY QID");
 
		ResultSet rs = pstmt.executeQuery(); 
		while (rs.next()) 
			totalQuestions++; 			
			
		
		
		pstmt = conn.prepareStatement("select distinct tblUserAnswers.UID, tblUsers.LastName, tblUsers.FirstName  from tblUserAnswers INNER JOIN tblUsers WHERE tblUserAnswers.UID = tblUsers.UID");
 
	
		rs = pstmt.executeQuery(); 
		while (rs.next())
		{ 
			int uid = rs.getInt("UID"); 
			String name = rs.getString("LastName") + ", " +
				      rs.getString("FirstName");  
			
			int rightQuestions = 0; 
			
			boolean answerIsRight[] = new boolean[totalQuestions]; 
			int i = 0; 			
			
			PreparedStatement pstmt2 = conn.prepareStatement("SELECT QID, IsRight FROM tblUserAnswers WHERE UID = ? AND IsRight = 'Y'"); 
			
			pstmt2.setInt(1, rs.getInt("UID")); 
			
			ResultSet rs2 = pstmt2.executeQuery(); 
			
			while (rs2.next())
			{
				answerIsRight[rs2.getInt("QID") - 1 ] = true; 
				rightQuestions++;
			}
			
			ReportCard report = new ReportCard(); 
			//ReportCard report = new ReportCard(uid, name, totalQuestions, rightQuestions, (totalQuestions - rightQuestions)); 
		
		}
		
		return null; 
	}
 

	public int peoplePassed()
	{
		try
		{	
		
			int totalQuestions = 0; 
					
			if (conn == null)
				startConnection();
			
			
			pstmt = conn.prepareStatement("SELECT COUNT(QID) FROM tblQuestions GROUP BY QID");
 
			ResultSet rs = pstmt.executeQuery(); 
			while (rs.next()) 
				totalQuestions++; 
 
							
			pstmt = conn.prepareStatement("SELECT DISTINCT UID from tblUserAnswers;");
 
		
			rs = pstmt.executeQuery(); 
			while (rs.next())
			{ 
				PreparedStatement pstmt2 = conn.prepareStatement("SELECT IsRight FROM tblUserAnswers WHERE UID = ? AND IsRight = 'Y'"); 
				
				pstmt2.setInt(1, rs.getInt("UID")); 
				
				ResultSet rs2 = pstmt2.executeQuery(); 
				int correctPerPerson = 0; 
				
				while (rs2.next())
				{
					correctPerPerson++; 
				}
				
				System.err.println("User with ID " + rs.getInt("UID") + " has " + correctPerPerson + "/" + totalQuestions + " correct questions"); 
			}
 
		
						
			return 0; 
		}
		catch (Exception e) 
		{
			System.err.println(e.toString()); 
		}
		return -1; 
	
	
	}
	public void stopConnection()
	{
		/*try
		{
			conn.close(); 
		}
		catch (Exception e) 
		{
		}*/
	}
}

2. ReportCard.java
Code:
package db; 
 
public class ReportCard
{
	int uid; 
	String name; 
	
	int totalQuestions; 
	int rightQuestions; 
	int wrongQuestions; 
	
	float grade; 
	
	boolean[] answerIsRight; 
	
	public ReportCard()
	{
	}
	public ReportCard(int uidGot, String nameGot, int totalQuestionsGot, 
			  int rightQuestionsGot, int WrongQuestionsGot, char[] answerIsRightGot[])
	{
		grade = (float) (((float) ((float) rightQuestions) / ((float) totalQuestions)) * 100);
	}
}
 
Old 07-17-2004, 04:27 PM   #2
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Original Poster
Rep: Reputation: 32
I solved it. I had mistyped CLASSPATH.
 
Old 07-19-2004, 12:52 PM   #3
jpostma
Member
 
Registered: Jun 2003
Location: Alphen ad Rijn -- The Netherlands
Distribution: Fedora Core 2 - Slackware 12
Posts: 119

Rep: Reputation: 15
I see that you make the connection with "DriverManager.getConnection". The preferred way is to make a connection through a DataSource. A DataSource uses a connectionpool and is therefore faster by heavy load.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
Communicating Class A and Class C Networks chadtce Linux - Networking 10 07-23-2003 01:36 PM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration