aspose file tools*
The moose likes JDBC and the fly likes ClassNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ClassNotFoundException" Watch "ClassNotFoundException" New topic
Author

ClassNotFoundException

Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
hi, i'm using Sun Java Application Server PE 9 and NetBeans 5.5. I have database store with URL : jdbc:derby://localhost:1527/store and driver : org.apache.derby.jdbc.ClientDriver. On the Netbeans IDE i successfully have connection with database and making changes. So i try to connect to database with this code :

public RSAccounts() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();

} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}

and java return this error :

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
Java Result: 1

Oks! Where i making misstake? Can you someone help me or have any url under hand helpful.

thank you.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Syskata,
It's saying that the class isn't found. So the problem isn't with your connection URL.

Try doing new ClientDriver() to verify you get a compile error. Then it's easier to play with the classpath because you can see when it is fixed without having to run the code.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
So, I see that the class isn't found but i have already bild the database with it. Can you be more specific how to use ClienDriver() and what's the deal with the classpath.

Thank a lot!
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
The class org.apache.derby.jdbc.ClientDriver is not in your application's classpath.

It's most likely in a jar. Find the jar on your filesystem and append it to your application's classpath.
Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
Thank you for helping but i still can't understand what's happening here.
I have only one java fail. where is org.apache.derby.jdbc.ClientDriver and where i must change the classpath. what's the jar file dealing.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Download driver from the below URL,

DERBY

After downloading, read link provided by Scott Johnson.
[ December 07, 2006: Message edited by: Prabhu venkatachalam ]

Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
what's the proglem? This is the code where i wont to work! Please help me!


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class RSAccounts extends JFrame {

private JButton getAccountButton, insertAccountButton, deleteAccountButton,
updateAccountButton, nextButton, previousButton, lastButton,
firstButton, gotoButton, freeQueryButton;

private JList accountNumberList;

private JTextField accountIDText, usernameText, passwordText, tsText,
activeTSText, gotoText, freeQueryText;

private JTextArea errorText;

private Connection connection;

private Statement statement;

private ResultSet rs;

public RSAccounts() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
} catch (Exception e) {
System.err.println("Unable to find and load driver! Program termination!\n");
System.err.println(e);
System.exit(1);
}
}

private void loadAccounts() {
Vector v = new Vector();
try {
rs = statement.executeQuery("SELECT * FROM user");

while (rs.next()) {
v.addElement(rs.getString("userid"));
}
} catch (SQLException e) {
displaySQLErrors(e);
}
accountNumberList.setListData(v);
}

private void buildGUI() {
Container c = getContentPane();
c.setLayout(new FlowLayout());

accountNumberList = new JList();
loadAccounts();
accountNumberList.setVisibleRowCount(2);
JScrollPane accountNumberListScrollPane = new JScrollPane(
accountNumberList);

gotoText = new JTextField(3);
freeQueryText = new JTextField(40);
getAccountButton = new JButton("Get Account");
getAccountButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
rs.first();
while (rs.next()) {
if (rs.getString("userid").equals(
accountNumberList.getSelectedValue()))
break;
}
if (!rs.isAfterLast()) {
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));
}
} catch (SQLException selectException) {
displaySQLErrors(selectException);
}
}
});


insertAccountButton = new JButton("Insert Account");
insertAccountButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement
.executeUpdate("INSERT INTO user VALUES("
+ accountIDText.getText() + ", " + "'"
+ usernameText.getText() + "', " + "'"
+ passwordText.getText() + "', " + "0"
+ ", " + "now())");
errorText.append("Inserted " + i + " rows successfully");
accountNumberList.removeAll();
loadAccounts();
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});
deleteAccountButton = new JButton("Delete Account");
deleteAccountButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement
.executeUpdate("DELETE FROM user WHERE userid = "
+ accountNumberList.getSelectedValue());
errorText.append("Deleted " + i + " rows successfully");
accountNumberList.removeAll();
loadAccounts();
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

updateAccountButton = new JButton("Update Account");
updateAccountButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement.executeUpdate("UPDATE user "
+ "SET first='" + usernameText.getText() + "', "
+ "password='" + passwordText.getText() + "', "
+ "WHERE userid = "
+ accountNumberList.getSelectedValue());
errorText.append("Updated " + i + " rows successfully");
accountNumberList.removeAll();
loadAccounts();
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

nextButton = new JButton(">");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (!rs.isLast()) {
rs.next();
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));

}
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

previousButton = new JButton("<");
previousButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (!rs.isFirst()) {
rs.previous();
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));
}
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

lastButton = new JButton(">|");
lastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
rs.last();
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

firstButton = new JButton("|<");
firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
rs.first();
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

gotoButton = new JButton("Goto");
gotoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
rs.absolute(Integer.parseInt(gotoText.getText()));
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});
freeQueryButton = new JButton("Execute Query");
freeQueryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (freeQueryText.getText().toUpperCase().indexOf("SELECT") >= 0) {
rs = statement.executeQuery(freeQueryText.getText());
if (rs.next()) {
accountIDText.setText(rs.getString("userid"));
usernameText.setText(rs.getString("first"));
passwordText.setText(rs.getString("password"));
}
} else {
int i = statement
.executeUpdate(freeQueryText.getText());
errorText.append("Rows affected = " + i);
loadAccounts();
}
} catch (SQLException insertException) {
displaySQLErrors(insertException);
}
}
});

JPanel first = new JPanel(new GridLayout(5, 1));
first.add(accountNumberListScrollPane);
first.add(getAccountButton);
first.add(insertAccountButton);
first.add(deleteAccountButton);
first.add(updateAccountButton);

accountIDText = new JTextField(15);
usernameText = new JTextField(15);
passwordText = new JTextField(15);
tsText = new JTextField(15);
activeTSText = new JTextField(15);
errorText = new JTextArea(5, 15);
errorText.setEditable(false);

JPanel second = new JPanel();
second.setLayout(new GridLayout(6, 1));
second.add(accountIDText);
second.add(usernameText);
second.add(passwordText);
second.add(tsText);
second.add(activeTSText);

JPanel third = new JPanel();
third.add(new JScrollPane(errorText));

JPanel fourth = new JPanel();
fourth.add(firstButton);
fourth.add(previousButton);
fourth.add(nextButton);
fourth.add(lastButton);
fourth.add(gotoText);
fourth.add(gotoButton);

JPanel fifth = new JPanel();
fifth.add(freeQueryText);

c.add(first);
c.add(second);
c.add(third);
c.add(fourth);
c.add(fifth);
c.add(freeQueryButton);
setSize(500, 500);
show();
}

public void connectToDB() {
try {
connection = DriverManager

.getConnection("jdbc:derby://localhost:1527/store?user=user&password=user");
statement = connection.createStatement();

} catch (SQLException connectException) {
System.out.println(connectException.getMessage());
System.out.println(connectException.getSQLState());
System.out.println(connectException.getErrorCode());
System.exit(1);
}
}

private void displaySQLErrors(SQLException e) {
errorText.append("SQLException: " + e.getMessage() + "\n");
errorText.append("SQLState: " + e.getSQLState() + "\n");
errorText.append("VendorError: " + e.getErrorCode() + "\n");
}

private void init() {
connectToDB();
}

public static void main(String[] args) {
RSAccounts accounts = new RSAccounts();

accounts.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

accounts.init();
accounts.buildGUI();
}
}
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
what's the proglem? This is the code


If the exception is ClassNotFoundException and you've correctly named the driver, then the problem isn't in the code. It's with the classpath, file permissions and/or missing jars.

Did you download the derbyclient.jar (it's inside the lib directory of the db-derby-10.2.1.6-bin.zip archive) from the link above and add it to your classpath?
[ December 07, 2006: Message edited by: Scott Johnson ]
Syskata Mitev
Ranch Hand

Joined: Aug 23, 2006
Posts: 51
I have derbyclient.jar in C:\Sun\AppServer\derby\lib.
How to add it to my classpath? Where are my classpath? How to change main classpath?

thank a lot!
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
How to add it to my classpath? Where are my classpath? How to change main classpath?


Did you read the link in my previous posts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassNotFoundException