IndieGirl
asked on
Exception in thread "main" java.lang.NoClassDefFoundError:
Hi, I've complied a program successfully but when i run i get the following error
Exception in thread "main" java.lang.NoClassDefFoundE rror: /users/0209126m/LAB-6/Dogs /class
Caused by: java.lang.ClassNotFoundExc eption: .users.0209126m.LAB-6.Dogs .class
at java.net.URLClassLoader$1. run(URLCla ssLoader.j ava:200)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:18 8)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:306)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :276)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:251)
at java.lang.ClassLoader.load ClassInter nal(ClassL oader.java :319)
The classpath is set to
.:/users/level3/software/p ublic/DB3/ jxl.jar:/u sers/level 3/software /public/DB 3/tonicpoi nt.jar:/us ers/level3 /software/ public/DB3 /poi-3.0-a lpha2-2006 0616.jar:/ users/leve l3/softwar e/public/D B3/poi-scr atchpad-3. 0-alpha2-2 0060616.ja r:/users/l evel3/soft ware/publi c/DB3/ojdb c14.jar:/u sers/level 3/software /public/DB 3/jfreecha rt-0.9.6.j ar:/users/ level3/sof tware/publ ic/DB3/jco mmon-0.7.2 .jar:/user s/level3/s oftware/pu blic/DB3/d b3.jar:
the program code is
/**
*
* @author Lindsey
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
public class Dogs extends JFrame {
//swing components
private JButton motherButton = new JButton(),
fatherButton = new JButton(),
originalButton = new JButton("Original dog"),
restartButton = new JButton("Choose another dog"),
quitButton = new JButton("Quit");
private JLabel dogInfo = new JLabel(),
pedInfo = new JLabel();
private Stack<String> dogStack = new Stack<String>();
private String topMother, topFather; //the mother and father names of the dog
//at the top of the dogStack
//database stuff
private static final String connStr =
"jdbc:oracle:thin:your_ora cle_020912 6m/" +
"0209126mtest@crooked.dcs. gla.ac.uk: 1521:L3";
private static Connection conn;
private static PreparedStatement dogInfoStmt, siblingStmt, childStmt,
grandchildStmt, parentStmt, breedStmt,
parentBreedStmt;
//output a supplied error message, the details of the exception, and then die
private static void doError(Exception e, String msg)
{
System.out.println(msg);
e.printStackTrace();
System.exit(1);
}
//output any warnings associated with <conn> or <stmt>
private static void printWarnings(Connection conn, Statement stmt)
{
try {
SQLWarning currConnWarn = conn.getWarnings();
while (currConnWarn != null)
System.out.println("Warnin g: " + currConnWarn.getMessage()) ;
SQLWarning currStmtWarn = stmt.getWarnings();
while (currStmtWarn != null)
System.out.println("Warnin g: " + currStmtWarn.getMessage()) ;
} catch(Exception e) {
doError(e, "Problem getting warnings");
}
}
private Dogs() {
//database stuff - load driver, and then obtain connection
try {
Class.forName("oracle.jdbc .driver.Or acleDriver ");
} catch(Exception e) {
doError(e, "Failed to load oracle driver");
}
try {
Connection conn = DriverManager.getConnectio n(connStr) ;
} catch(Exception e) {
doError(e, "Failure to obtain connection: " + connStr);
}
//create prepared statements for later use by the interface code
//this will form a nested query to tell us how many dogs an owner has
//Columns should be renamed, if necessary
//Result: ownerid, noOfDogs
String ownerCount =
"SELECT ownerid, Count(name)" +
"FROM Dog " +
"GROUP BY ownerid";
//finds all the data that are single values per dog
String dogInfoQuery =
"SELECT Dog.breedname, Dog.mothername, " +
" Dog.fathername, Kennel.kennelname, " +
" Kennel.address, Owner.name, " +
" OwnerCount.noOfDogs " +
"FROM Dog, Kennel, " +
" Owner, (" + ownerCount + ") OwnerCount " +
"WHERE Dog.kennelname=Kennel.kenn elname AND " +
" Dog.ownerid=Owner.ownerid AND " +
" Owner.ownerid=OwnerCount.o wnerid AND " +
" Dog.name=?"; //parameterised by dog name
try {
dogInfoStmt = conn.prepareStatement(dogI nfoQuery);
} catch(SQLException e) {
doError(e, "Dog info statement failed to compile: " + dogInfoQuery);
}
//finds the dog's siblings (incl. half siblings), but not the dog itself
//parameterised by (1) the dog's mothername, (2) the dog's fathername and (3) the dog's
//own name
//Result: name
String siblingQuery =
"SELECT d2.name" +
"FROM Dog d1, Dog d2" +
"WHERE (d1.mothername=d2.motherna me)" +
"OR d1.fathername=d2.fathernam e)" +
"AND d1.name=?" +
"AND d2.name<>d1.name";
try {
siblingStmt = conn.prepareStatement(sibl ingQuery);
} catch(SQLException e) {
doError(e, "Sibling statement failed to compile: " + siblingStmt);
}
//finds the dog's children, we don't know if it's a mother or father, so
//we match either (although presumably only one for any given dog!)
//the query should be parametrised by (1) the dog's mothername, and (2) the dog's fathername
//Result: name
//Sorted by: name
String childQuery =
"SELECT name"+
"FROM Dog"+
"WHERE mothername = ?" +
"OR fathername = ?";
try {
childStmt = conn.prepareStatement(chil dQuery);
} catch(SQLException e) {
doError(e, "Child query failed to compile: " + childQuery);
}
//finds the dog's grandchildren, again matching father or mother as above
//in this query the current dog "?" is D1's mother or father, D2 is the
//grandchild.
//Result: D2.name
String grandchildQuery =
"SELECT d2.name " +
"FROM Dog d1, Dog d2" +
"WHERE (d1.mothername = ? OR d1.fathername = ?" +
"AND (d1.name = d2.mothername OR d1.name = d2.fathername)";
try {
grandchildStmt = conn.prepareStatement(gran dchildQuer y);
} catch(SQLException e) {
doError(e, "Granchild query failed to compile: " + grandchildQuery);
}
//finds parents of two dogs at once, the parameters are the two dogs
String parentQuery = "SELECT mothername, fathername " +
"FROM Dog " +
"WHERE name=? OR name=?";
try {
parentStmt = conn.prepareStatement(pare ntQuery);
} catch(SQLException e) {
doError(e, "Failed to compile grandparent statement: " + parentQuery);
}
//a query to return all known breeds for the supplied dog, used in
//getParentsBreeds()
String parentBreedQuery = "SELECT Parent.breedname " +
"FROM Dog Curr, Dog Parent " +
"WHERE (Curr.mothername=Parent.na me OR " +
" Curr.fathername=Parent.nam e) " +
" AND Curr.name=?";
try {
parentBreedStmt = conn.prepareStatement(pare ntBreedQue ry);
} catch(SQLException e) {
doError(e, "Failed to compile parent breed stmt: " + parentBreedStmt);
}
//info text boxes and labels
JPanel dogInfoPanel = new JPanel();
dogInfoPanel.add(dogInfo);
dogInfoPanel.setBorder(
BorderFactory.createCompou ndBorder(
BorderFactory.createTitled Border("Do g information"),
BorderFactory.createEmptyB order(10,1 0,10,10))) ;
JPanel pedInfoPanel = new JPanel();
pedInfoPanel.add(pedInfo);
pedInfoPanel.setBorder(
BorderFactory.createCompou ndBorder(
BorderFactory.createTitled Border("Pe digree"),
BorderFactory.createEmptyB order(10,1 0,10,10))) ;
//positioning of components
Container cp = getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.X_A XIS));
Box buttonBox = Box.createVerticalBox();
buttonBox.add(motherButton );
buttonBox.add(fatherButton );
buttonBox.add(originalButt on);
buttonBox.add(Box.createVe rticalGlue ());
buttonBox.add(restartButto n);
buttonBox.add(quitButton);
Box infoBox = Box.createVerticalBox();
infoBox.add(dogInfoPanel);
infoBox.add(pedInfoPanel);
cp.add(infoBox);
cp.add(buttonBox);
//button handling
ButtonHandler bh = new ButtonHandler();
motherButton.addActionList ener(bh);
fatherButton.addActionList ener(bh);
originalButton.addActionLi stener(bh) ;
restartButton.addActionLis tener(bh);
quitButton.addActionListen er(bh);
//set up the dog to be displayed
dogStack.push(getDogChoice ());
redisplay(); //display dog info
originalButton.setEnabled( false); //cannot go to previous dog
//window settings
Dimension screen = getToolkit().getScreenSize ();
setBounds(0, 0, 700, 700); //position (0,0) and size 700x700
setDefaultCloseOperation(E XIT_ON_CLO SE);
setTitle("Dog information");
setVisible(true);
}
//Returns the most accurate possible breed information for the supplied dog's
//parents. For a parent that is not in the database we will assume that the
//breed is the same as the supplied dog's, unless the supplied dog is not in
//the database in which case we assume <assumeBreed>.
private Vector<String> getParentBreeds(String dogname, String assumeBreed)
{
Vector<String> retval = new Vector<String>(); //breeds we've found
//get as many parent breeds as possible
try {
parentBreedStmt.setString( 1, dogname);
ResultSet results = parentBreedStmt.executeQue ry();
printWarnings(conn, parentBreedStmt);
//Get all the breed information available from the database.
//Due to key constraints there are at most 2 rows in the result.
while (results.next())
retval.add(results.getStri ng(1));
} catch(SQLException e) {
doError(e, "Parent breed query failed to execute");
}
//if we have all the possible parents, then we can stop now, otherwise use
//best guesses as per the comment above
if (retval.size() == 2)
return retval;
else {
try {
dogInfoStmt.setString(1, dogname);
ResultSet results = dogInfoStmt.executeQuery() ;
printWarnings(conn, dogInfoStmt);
if (results.next()) //try to use the supplied dog's real breed
while (retval.size() < 2)
retval.add(results.getStri ng(1));
else //if real breed is not available use the assumed breed
while (retval.size() < 2)
retval.add(assumeBreed);
} catch(SQLException e) {
doError(e, "Failed to execute breed query");
}
}
return retval;
}
//displays the dog data present on the top of the stack of names
private void redisplay() {
if (dogStack.empty()) {
System.out.println("Unexpe ctedly exhausted dogs!");
System.exit(1);
}
String dogName = dogStack.peek();
ResultSet results;
//per-dog information
String name = null, breed = null, mother = null, father = null,
kennel = null, kennAddr = null, owner = null;
int ownerDogs = 0;
//information with arbitrary numbers of values per dog
Vector<String> siblings = new Vector<String>(),
children = new Vector<String>(),
grandchildren = new Vector<String>(),
grandparents = new Vector<String>();
try {
dogInfoStmt.setString(1, dogName);
results = dogInfoStmt.executeQuery() ;
printWarnings(conn, dogInfoStmt);
//make sure that the dog we are trying to display is in the DB
if (!(results.next())) {
//remove it and display a warning message
JOptionPane.showMessageDia log(this,
"Information for " + dogStack.pop() + " missing in database.");
if (dogStack.size() == 1) //in case there is no stack to ascend
originalButton.setEnabled( false);
return; //stop now
}
//since the data is in the database we continue as normal
breed = results.getString(1);
topMother = mother = results.getString(2);
topFather = father = results.getString(3);
kennel = results.getString(4);
kennAddr = results.getString(5);
owner = results.getString(6);
ownerDogs = results.getInt(7);
} catch(SQLException e) {
doError(e, "Failed to execute dog info query for " + dogName);
}
try {
//looking for dogs with the same parents
siblingStmt.setString(1, mother);
siblingStmt.setString(2, father);
siblingStmt.setString(3, dogName); //don't match the dog itself
results = siblingStmt.executeQuery() ;
printWarnings(conn, siblingStmt);
while (results.next()) //store each sibling name for later display
siblings.add(results.getSt ring(1));
} catch(SQLException e) {
doError(e, "Failed to execute sibling query for " + dogName);
}
try {
//looking for child dogs
childStmt.setString(1, dogName); childStmt.setString(2, dogName);
results = childStmt.executeQuery();
printWarnings(conn, childStmt);
while (results.next()) //store each name for display later on
children.add(results.getSt ring(1));
} catch(SQLException e) {
doError(e, "Failed to execute child query for " + dogName);
}
try {
//looking for grandchildren
grandchildStmt.setString(1 , dogName);
grandchildStmt.setString(2 , dogName);
results = grandchildStmt.executeQuer y();
printWarnings(conn, grandchildStmt);
while (results.next())
grandchildren.add(results. getString( 1));
} catch(SQLException e) {
doError(e, "Failed to execute grandchild query for " + dogName);
}
try {
parentStmt.setString(1, mother); //maternal grandparents
parentStmt.setString(2, father); //and paternal grandparents
results = parentStmt.executeQuery();
printWarnings(conn, parentStmt);
while (results.next()) { //zero, one or two rows as a result
if (results.getString(1) != null)
grandparents.add(results.g etString(1 )); //grandmother
if (results.getString(2) != null)
grandparents.add(results.g etString(2 )); //grandfather
}
} catch(SQLException e) {
doError(e, "Failed to execute grandparent query for " + dogName);
}
Vector <String> ancestors = getAncestors(dogName,null) ;
Vector <String> des = getDescendents(dogName,nul l);
if (kennAddr == null) kennAddr = "Unknown";
//output dog info data
dogInfo.setText("<html>Nam e: " + dogName + "<br>" +
"Breed: " + breed + "<br>" +
"Kennel: " + kennel + "<br>" +
"Address: " + kennAddr + "<br>" +
dogName + "'s owner: " +
owner + " (owns " + ownerDogs + " dogs)" + "</html>");
//put together a message about how many parents are not named in the DB
String parentMissingInfo = ""; //nothing by default
if (father == null && mother == null)
parentMissingInfo = "(2 missing)";
else if (father == null ^ mother == null) //exclusive OR
parentMissingInfo = "(1 missing)";
//message about how many grandparents are missing, assume dog isn't inbred
String grandparentMissingInfo = "";
if (grandparents.size() != 4)
grandparentMissingInfo = "(" + (4 - grandparents.size()) + " missing)";
//put together the required breed information
Vector<String> breeds = getParentBreeds(mother, breed);
breeds.addAll(getParentBre eds(father , breed));
breeds.add("ZZZZZZZZZZZZZZ ZZZZZZZZZZ "); //sentinel to help the output code
Object[] breedStrings = breeds.toArray();
Arrays.sort(breedStrings);
boolean isPureBred = breedStrings[0] == breedStrings[1] &&
breedStrings[1] == breedStrings[2] &&
breedStrings[2] == breedStrings[3];
String breedingString = "";
for (int i = 0; i < 4; i++)
for (int j = i; j < 5; j++)
if (!(breedStrings[i].equals( breedStrin gs[j]))) {
breedingString += "(" + (j - i) + "/4 " + breedStrings[i] + ") ";
i = j - 1; //start again at the start of the next run of breeds
break;
}
//output pedigree info
pedInfo.setText("<html>" + dogName + "'s siblings: " + siblings + "<br>" +
dogName + "'s Parents: " +
(father != null ? father : "") +
(father != null && mother != null ? ", " : "") +
(mother != null ? mother : "") +
" " + parentMissingInfo +
"<br>" + dogName + "'s Grandparents: " + grandparents +
" " + grandparentMissingInfo +
"<br>" + dogName + "'s Children: " + children + "<br>" +
dogName + "'s Grandchildren: " + grandchildren +
"<br><br>" +
(isPureBred ? "Purebred<br>" : "Not purebred<br>") +
"Breeding: " + breedingString +
"</html>");
if (mother != null)
motherButton.setText(mothe r + "'s details");
else {
motherButton.setText("Not available");
motherButton.setEnabled(fa lse);
}
if (father != null)
fatherButton.setText(fathe r + "'s details");
else {
fatherButton.setText("Not available");
fatherButton.setEnabled(fa lse);
}
}
//gets a dog name that the user chooses somehow
private String getDogChoice() {
try {
Statement dogStmt = conn.createStatement();
ResultSet dogRes = dogStmt.executeQuery("SELE CT name " +
"FROM Dog " +
"ORDER BY name");
Vector<String> dogNames = new Vector<String>();
while (dogRes.next())
dogNames.add(dogRes.getStr ing(1));
if (dogNames.size() == 0) {
System.out.println("No dogs present in database, exiting.");
System.exit(1);
}
String choice;
do {
//use a modal JOptionPane dialog to let the user choose between dogs
choice = (String)(JOptionPane.showI nputDialog (this, //parent dialog
"Select a dog to view", //message
"Select a dog", //title of dialog
JOptionPane.PLAIN_MESSAGE, //urgency
null,
dogNames.toArray(), //choices
dogNames.get(0))); //default choice
} while (choice == null); //keep going until the user picks one
return choice;
} catch(SQLException e) {
doError(e, "Failed to get dog name choices from DB");
}
return null; //unreachable
}
//handler to do the work of all the buttons, namely the parent buttons, the
//back button and the exit button
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEven t ae) {
Object src = ae.getSource(); //the button that causes the event
if (src == motherButton) {
dogStack.push(topMother); //put mother at the top of the stack
originalButton.setEnabled( true);
motherButton.setEnabled(tr ue);
fatherButton.setEnabled(tr ue);
redisplay(); //refresh the details on screen to show the mother
} else if (src == fatherButton) {
dogStack.push(topFather);
originalButton.setEnabled( true);
motherButton.setEnabled(tr ue);
fatherButton.setEnabled(tr ue);
redisplay();
} else if (src == originalButton) {
dogStack.pop(); //discard currently displayed dog
if (dogStack.size() == 1)
originalButton.setEnabled( false); //can't go any further back
motherButton.setEnabled(tr ue);
fatherButton.setEnabled(tr ue);
redisplay();
} else if (src == restartButton) {
dogStack.clear();
dogStack.push(getDogChoice ());
redisplay();
} else if (src == quitButton) {
try {
conn.close();
} catch(SQLException e) {
System.out.println("Cannot close DB connection");
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
}
public static void main(String[] args) {
Dogs window = new Dogs();
}
private Vector<String> getAncestors(String dogname, Vector<String> anc) {
if (anc == null) anc = new Vector<String>();
try {
// INSERT CODE HERE TO GET ANCESTORS
}
catch(Exception e) {
doError(e, "Failed to execute ancestor query in getBreeding");
}
return anc;
}
private Vector<String> getDescendents(String dogname, Vector<String> anc) {
if (anc == null) anc = new Vector<String>();
try {
//INSERT CODE HERE TO FIND DESCENDENTS
}
catch(Exception e) {
doError(e, "Failed to execute ancestor query in getBreeding");
}
return anc;
}
}
HELP!!
Exception in thread "main" java.lang.NoClassDefFoundE
Caused by: java.lang.ClassNotFoundExc
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
The classpath is set to
.:/users/level3/software/p
the program code is
/**
*
* @author Lindsey
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
public class Dogs extends JFrame {
//swing components
private JButton motherButton = new JButton(),
fatherButton = new JButton(),
originalButton = new JButton("Original dog"),
restartButton = new JButton("Choose another dog"),
quitButton = new JButton("Quit");
private JLabel dogInfo = new JLabel(),
pedInfo = new JLabel();
private Stack<String> dogStack = new Stack<String>();
private String topMother, topFather; //the mother and father names of the dog
//at the top of the dogStack
//database stuff
private static final String connStr =
"jdbc:oracle:thin:your_ora
"0209126mtest@crooked.dcs.
private static Connection conn;
private static PreparedStatement dogInfoStmt, siblingStmt, childStmt,
grandchildStmt, parentStmt, breedStmt,
parentBreedStmt;
//output a supplied error message, the details of the exception, and then die
private static void doError(Exception e, String msg)
{
System.out.println(msg);
e.printStackTrace();
System.exit(1);
}
//output any warnings associated with <conn> or <stmt>
private static void printWarnings(Connection conn, Statement stmt)
{
try {
SQLWarning currConnWarn = conn.getWarnings();
while (currConnWarn != null)
System.out.println("Warnin
SQLWarning currStmtWarn = stmt.getWarnings();
while (currStmtWarn != null)
System.out.println("Warnin
} catch(Exception e) {
doError(e, "Problem getting warnings");
}
}
private Dogs() {
//database stuff - load driver, and then obtain connection
try {
Class.forName("oracle.jdbc
} catch(Exception e) {
doError(e, "Failed to load oracle driver");
}
try {
Connection conn = DriverManager.getConnectio
} catch(Exception e) {
doError(e, "Failure to obtain connection: " + connStr);
}
//create prepared statements for later use by the interface code
//this will form a nested query to tell us how many dogs an owner has
//Columns should be renamed, if necessary
//Result: ownerid, noOfDogs
String ownerCount =
"SELECT ownerid, Count(name)" +
"FROM Dog " +
"GROUP BY ownerid";
//finds all the data that are single values per dog
String dogInfoQuery =
"SELECT Dog.breedname, Dog.mothername, " +
" Dog.fathername, Kennel.kennelname, " +
" Kennel.address, Owner.name, " +
" OwnerCount.noOfDogs " +
"FROM Dog, Kennel, " +
" Owner, (" + ownerCount + ") OwnerCount " +
"WHERE Dog.kennelname=Kennel.kenn
" Dog.ownerid=Owner.ownerid AND " +
" Owner.ownerid=OwnerCount.o
" Dog.name=?"; //parameterised by dog name
try {
dogInfoStmt = conn.prepareStatement(dogI
} catch(SQLException e) {
doError(e, "Dog info statement failed to compile: " + dogInfoQuery);
}
//finds the dog's siblings (incl. half siblings), but not the dog itself
//parameterised by (1) the dog's mothername, (2) the dog's fathername and (3) the dog's
//own name
//Result: name
String siblingQuery =
"SELECT d2.name" +
"FROM Dog d1, Dog d2" +
"WHERE (d1.mothername=d2.motherna
"OR d1.fathername=d2.fathernam
"AND d1.name=?" +
"AND d2.name<>d1.name";
try {
siblingStmt = conn.prepareStatement(sibl
} catch(SQLException e) {
doError(e, "Sibling statement failed to compile: " + siblingStmt);
}
//finds the dog's children, we don't know if it's a mother or father, so
//we match either (although presumably only one for any given dog!)
//the query should be parametrised by (1) the dog's mothername, and (2) the dog's fathername
//Result: name
//Sorted by: name
String childQuery =
"SELECT name"+
"FROM Dog"+
"WHERE mothername = ?" +
"OR fathername = ?";
try {
childStmt = conn.prepareStatement(chil
} catch(SQLException e) {
doError(e, "Child query failed to compile: " + childQuery);
}
//finds the dog's grandchildren, again matching father or mother as above
//in this query the current dog "?" is D1's mother or father, D2 is the
//grandchild.
//Result: D2.name
String grandchildQuery =
"SELECT d2.name " +
"FROM Dog d1, Dog d2" +
"WHERE (d1.mothername = ? OR d1.fathername = ?" +
"AND (d1.name = d2.mothername OR d1.name = d2.fathername)";
try {
grandchildStmt = conn.prepareStatement(gran
} catch(SQLException e) {
doError(e, "Granchild query failed to compile: " + grandchildQuery);
}
//finds parents of two dogs at once, the parameters are the two dogs
String parentQuery = "SELECT mothername, fathername " +
"FROM Dog " +
"WHERE name=? OR name=?";
try {
parentStmt = conn.prepareStatement(pare
} catch(SQLException e) {
doError(e, "Failed to compile grandparent statement: " + parentQuery);
}
//a query to return all known breeds for the supplied dog, used in
//getParentsBreeds()
String parentBreedQuery = "SELECT Parent.breedname " +
"FROM Dog Curr, Dog Parent " +
"WHERE (Curr.mothername=Parent.na
" Curr.fathername=Parent.nam
" AND Curr.name=?";
try {
parentBreedStmt = conn.prepareStatement(pare
} catch(SQLException e) {
doError(e, "Failed to compile parent breed stmt: " + parentBreedStmt);
}
//info text boxes and labels
JPanel dogInfoPanel = new JPanel();
dogInfoPanel.add(dogInfo);
dogInfoPanel.setBorder(
BorderFactory.createCompou
BorderFactory.createTitled
BorderFactory.createEmptyB
JPanel pedInfoPanel = new JPanel();
pedInfoPanel.add(pedInfo);
pedInfoPanel.setBorder(
BorderFactory.createCompou
BorderFactory.createTitled
BorderFactory.createEmptyB
//positioning of components
Container cp = getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.X_A
Box buttonBox = Box.createVerticalBox();
buttonBox.add(motherButton
buttonBox.add(fatherButton
buttonBox.add(originalButt
buttonBox.add(Box.createVe
buttonBox.add(restartButto
buttonBox.add(quitButton);
Box infoBox = Box.createVerticalBox();
infoBox.add(dogInfoPanel);
infoBox.add(pedInfoPanel);
cp.add(infoBox);
cp.add(buttonBox);
//button handling
ButtonHandler bh = new ButtonHandler();
motherButton.addActionList
fatherButton.addActionList
originalButton.addActionLi
restartButton.addActionLis
quitButton.addActionListen
//set up the dog to be displayed
dogStack.push(getDogChoice
redisplay(); //display dog info
originalButton.setEnabled(
//window settings
Dimension screen = getToolkit().getScreenSize
setBounds(0, 0, 700, 700); //position (0,0) and size 700x700
setDefaultCloseOperation(E
setTitle("Dog information");
setVisible(true);
}
//Returns the most accurate possible breed information for the supplied dog's
//parents. For a parent that is not in the database we will assume that the
//breed is the same as the supplied dog's, unless the supplied dog is not in
//the database in which case we assume <assumeBreed>.
private Vector<String> getParentBreeds(String dogname, String assumeBreed)
{
Vector<String> retval = new Vector<String>(); //breeds we've found
//get as many parent breeds as possible
try {
parentBreedStmt.setString(
ResultSet results = parentBreedStmt.executeQue
printWarnings(conn, parentBreedStmt);
//Get all the breed information available from the database.
//Due to key constraints there are at most 2 rows in the result.
while (results.next())
retval.add(results.getStri
} catch(SQLException e) {
doError(e, "Parent breed query failed to execute");
}
//if we have all the possible parents, then we can stop now, otherwise use
//best guesses as per the comment above
if (retval.size() == 2)
return retval;
else {
try {
dogInfoStmt.setString(1, dogname);
ResultSet results = dogInfoStmt.executeQuery()
printWarnings(conn, dogInfoStmt);
if (results.next()) //try to use the supplied dog's real breed
while (retval.size() < 2)
retval.add(results.getStri
else //if real breed is not available use the assumed breed
while (retval.size() < 2)
retval.add(assumeBreed);
} catch(SQLException e) {
doError(e, "Failed to execute breed query");
}
}
return retval;
}
//displays the dog data present on the top of the stack of names
private void redisplay() {
if (dogStack.empty()) {
System.out.println("Unexpe
System.exit(1);
}
String dogName = dogStack.peek();
ResultSet results;
//per-dog information
String name = null, breed = null, mother = null, father = null,
kennel = null, kennAddr = null, owner = null;
int ownerDogs = 0;
//information with arbitrary numbers of values per dog
Vector<String> siblings = new Vector<String>(),
children = new Vector<String>(),
grandchildren = new Vector<String>(),
grandparents = new Vector<String>();
try {
dogInfoStmt.setString(1, dogName);
results = dogInfoStmt.executeQuery()
printWarnings(conn, dogInfoStmt);
//make sure that the dog we are trying to display is in the DB
if (!(results.next())) {
//remove it and display a warning message
JOptionPane.showMessageDia
"Information for " + dogStack.pop() + " missing in database.");
if (dogStack.size() == 1) //in case there is no stack to ascend
originalButton.setEnabled(
return; //stop now
}
//since the data is in the database we continue as normal
breed = results.getString(1);
topMother = mother = results.getString(2);
topFather = father = results.getString(3);
kennel = results.getString(4);
kennAddr = results.getString(5);
owner = results.getString(6);
ownerDogs = results.getInt(7);
} catch(SQLException e) {
doError(e, "Failed to execute dog info query for " + dogName);
}
try {
//looking for dogs with the same parents
siblingStmt.setString(1, mother);
siblingStmt.setString(2, father);
siblingStmt.setString(3, dogName); //don't match the dog itself
results = siblingStmt.executeQuery()
printWarnings(conn, siblingStmt);
while (results.next()) //store each sibling name for later display
siblings.add(results.getSt
} catch(SQLException e) {
doError(e, "Failed to execute sibling query for " + dogName);
}
try {
//looking for child dogs
childStmt.setString(1, dogName); childStmt.setString(2, dogName);
results = childStmt.executeQuery();
printWarnings(conn, childStmt);
while (results.next()) //store each name for display later on
children.add(results.getSt
} catch(SQLException e) {
doError(e, "Failed to execute child query for " + dogName);
}
try {
//looking for grandchildren
grandchildStmt.setString(1
grandchildStmt.setString(2
results = grandchildStmt.executeQuer
printWarnings(conn, grandchildStmt);
while (results.next())
grandchildren.add(results.
} catch(SQLException e) {
doError(e, "Failed to execute grandchild query for " + dogName);
}
try {
parentStmt.setString(1, mother); //maternal grandparents
parentStmt.setString(2, father); //and paternal grandparents
results = parentStmt.executeQuery();
printWarnings(conn, parentStmt);
while (results.next()) { //zero, one or two rows as a result
if (results.getString(1) != null)
grandparents.add(results.g
if (results.getString(2) != null)
grandparents.add(results.g
}
} catch(SQLException e) {
doError(e, "Failed to execute grandparent query for " + dogName);
}
Vector <String> ancestors = getAncestors(dogName,null)
Vector <String> des = getDescendents(dogName,nul
if (kennAddr == null) kennAddr = "Unknown";
//output dog info data
dogInfo.setText("<html>Nam
"Breed: " + breed + "<br>" +
"Kennel: " + kennel + "<br>" +
"Address: " + kennAddr + "<br>" +
dogName + "'s owner: " +
owner + " (owns " + ownerDogs + " dogs)" + "</html>");
//put together a message about how many parents are not named in the DB
String parentMissingInfo = ""; //nothing by default
if (father == null && mother == null)
parentMissingInfo = "(2 missing)";
else if (father == null ^ mother == null) //exclusive OR
parentMissingInfo = "(1 missing)";
//message about how many grandparents are missing, assume dog isn't inbred
String grandparentMissingInfo = "";
if (grandparents.size() != 4)
grandparentMissingInfo = "(" + (4 - grandparents.size()) + " missing)";
//put together the required breed information
Vector<String> breeds = getParentBreeds(mother, breed);
breeds.addAll(getParentBre
breeds.add("ZZZZZZZZZZZZZZ
Object[] breedStrings = breeds.toArray();
Arrays.sort(breedStrings);
boolean isPureBred = breedStrings[0] == breedStrings[1] &&
breedStrings[1] == breedStrings[2] &&
breedStrings[2] == breedStrings[3];
String breedingString = "";
for (int i = 0; i < 4; i++)
for (int j = i; j < 5; j++)
if (!(breedStrings[i].equals(
breedingString += "(" + (j - i) + "/4 " + breedStrings[i] + ") ";
i = j - 1; //start again at the start of the next run of breeds
break;
}
//output pedigree info
pedInfo.setText("<html>" + dogName + "'s siblings: " + siblings + "<br>" +
dogName + "'s Parents: " +
(father != null ? father : "") +
(father != null && mother != null ? ", " : "") +
(mother != null ? mother : "") +
" " + parentMissingInfo +
"<br>" + dogName + "'s Grandparents: " + grandparents +
" " + grandparentMissingInfo +
"<br>" + dogName + "'s Children: " + children + "<br>" +
dogName + "'s Grandchildren: " + grandchildren +
"<br><br>" +
(isPureBred ? "Purebred<br>" : "Not purebred<br>") +
"Breeding: " + breedingString +
"</html>");
if (mother != null)
motherButton.setText(mothe
else {
motherButton.setText("Not available");
motherButton.setEnabled(fa
}
if (father != null)
fatherButton.setText(fathe
else {
fatherButton.setText("Not available");
fatherButton.setEnabled(fa
}
}
//gets a dog name that the user chooses somehow
private String getDogChoice() {
try {
Statement dogStmt = conn.createStatement();
ResultSet dogRes = dogStmt.executeQuery("SELE
"FROM Dog " +
"ORDER BY name");
Vector<String> dogNames = new Vector<String>();
while (dogRes.next())
dogNames.add(dogRes.getStr
if (dogNames.size() == 0) {
System.out.println("No dogs present in database, exiting.");
System.exit(1);
}
String choice;
do {
//use a modal JOptionPane dialog to let the user choose between dogs
choice = (String)(JOptionPane.showI
"Select a dog to view", //message
"Select a dog", //title of dialog
JOptionPane.PLAIN_MESSAGE,
null,
dogNames.toArray(), //choices
dogNames.get(0))); //default choice
} while (choice == null); //keep going until the user picks one
return choice;
} catch(SQLException e) {
doError(e, "Failed to get dog name choices from DB");
}
return null; //unreachable
}
//handler to do the work of all the buttons, namely the parent buttons, the
//back button and the exit button
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEven
Object src = ae.getSource(); //the button that causes the event
if (src == motherButton) {
dogStack.push(topMother); //put mother at the top of the stack
originalButton.setEnabled(
motherButton.setEnabled(tr
fatherButton.setEnabled(tr
redisplay(); //refresh the details on screen to show the mother
} else if (src == fatherButton) {
dogStack.push(topFather);
originalButton.setEnabled(
motherButton.setEnabled(tr
fatherButton.setEnabled(tr
redisplay();
} else if (src == originalButton) {
dogStack.pop(); //discard currently displayed dog
if (dogStack.size() == 1)
originalButton.setEnabled(
motherButton.setEnabled(tr
fatherButton.setEnabled(tr
redisplay();
} else if (src == restartButton) {
dogStack.clear();
dogStack.push(getDogChoice
redisplay();
} else if (src == quitButton) {
try {
conn.close();
} catch(SQLException e) {
System.out.println("Cannot
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
}
public static void main(String[] args) {
Dogs window = new Dogs();
}
private Vector<String> getAncestors(String dogname, Vector<String> anc) {
if (anc == null) anc = new Vector<String>();
try {
// INSERT CODE HERE TO GET ANCESTORS
}
catch(Exception e) {
doError(e, "Failed to execute ancestor query in getBreeding");
}
return anc;
}
private Vector<String> getDescendents(String dogname, Vector<String> anc) {
if (anc == null) anc = new Vector<String>();
try {
//INSERT CODE HERE TO FIND DESCENDENTS
}
catch(Exception e) {
doError(e, "Failed to execute ancestor query in getBreeding");
}
return anc;
}
}
HELP!!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I changed the LAB-6 to lab6 as requested but still receive the same run error
The classpath has to be set to the above in the code as it is shared and can not be written too. The Dogs.class is stored in a personal folder on a shared drive
The classpath has to be set to the above in the code as it is shared and can not be written too. The Dogs.class is stored in a personal folder on a shared drive
<<The classpath has to be set to the above in the code as it is shared and can not be written too. The Dogs.class is stored in a personal folder on a shared drive>>
I couldn't understand what you meant here. Could you please re-phrase it?
Thanks
I couldn't understand what you meant here. Could you please re-phrase it?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Could you please check if jar files in the class path are having the class file (coming in the exception stack trace)?
Also you probably missed out on giving the package declaration to you Dogs class. Please check
Also you probably missed out on giving the package declaration to you Dogs class. Please check
ASKER
complete
please change it to lower case and remove hyphen
see this for reference
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html