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)
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);
}
}