Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An JDBC problem

 
Timothy Wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The following applet can be compiled with no error, but when I run it in an HTML file, statement < Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");> will produce an exception: java.lang.classNotFoundException why?
NT+SP4+JDK1.3
Thanks for your kindly help!
import java.applet.Applet;
import java.awt.Graphics;
import java.util.Vector;
import java.sql.*;
public class test extends Applet implements Runnable {
private Thread worker;
private Vector queryResults;
private String message = "Initializing";
public synchronized void start() {
// Every time "start" is called we create a worker thread to
// re-evaluate the database query.
if (worker == null) {
message = "Connecting to database";
worker = new Thread(this);
worker.start();
}
}
public void run() {
String url = "jdbc dbc :people";
String query = "select First from csv";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(Exception ex) {
setError(""+ex);
return;
}
try {
Vector results = new Vector();
Connection con = DriverManager.getConnection(url,"sa", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String s = rs.getString("First");
//float f = rs.getFloat("PRICE");
String text = s;// + " " + f;
results.addElement(text);
}
stmt.close();
con.close();
setResults(results);
} catch(SQLException ex) {
setError("SQLException: " + ex);
}
}
public synchronized void paint(Graphics g) {
// If there are no results available, display the current message.
if (queryResults == null) {
g.drawString(message, 5, 50);
return;
}
// Display the results.
g.drawString("Prices of coffee per pound: ", 5, 10);
int y = 30;
java.util.Enumeration enum = queryResults.elements();
while (enum.hasMoreElements()) {
String text = (String)enum.nextElement();
g.drawString(text, 5, y);
y = y + 15;
}
}
private synchronized void setError(String mess) {
queryResults = null;
message = mess;
worker = null;
// And ask AWT to repaint this applet.
repaint();
}
private synchronized void setResults(Vector results) {
queryResults = results;
worker = null;
// And ask AWT to repaint this applet.
repaint();
}
}
============================================================
<html>
<head>

<title>Welcome To Microsoft Personal Web Server</title>
</head>
<applet code=test.class height=300 weight=300>
</applet>
</body>
</html>

[This message has been edited by Thomas Paul (edited January 22, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
 
wayne hua
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the JDBC setting in your program (according to my localhost), and your code run fine.
You can check url, classname, user name and password to see whether it is compatible with your local configuration.
Here is the revised code (just show the changed portion):
public void run() {
String url = "jdbc:avenir:localhost:5000:c:\\My documents\\coldfusion\\housing.mdb";
String query = "select name from seeker";
try {
Class.forName("acs.jdbc.Driver");
}
catch(Exception ex) {
setError(""+ex);
return;
}
try {
Vector results = new Vector();
Connection con = DriverManager.getConnection(url,"Admin", "");
Good luck.

 
Kevin Mukhar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassNotFoundException always means what it says. The Java Virtual Machine could not locate the class. In this case, the JVM is the JVM inside the browser. You need to make sure that rt.jar is in the classpath used by the browser.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic