Connector/J & Applet
Posted by: Radu Solomon
Date: November 23, 2004 09:10AM

Hi,

i have an applet which conects to a DB on the localhost and than allows searches. My problem is that when I run the applet from Eclipse it logs in and does all that is suppose to do without throwing any exceptions. However if i try to run it from my html file it says it can not find the driver to connect and exists.

I am using: mysql-connector-java-3.0.16-ga-bin.jar
The error i get from browser is: java.lang.classNotFoundException: com.mysql.jdbc.Driver

I used the procedure described on this website to add it to my CLASSPATH but no success.

Here is the part of the code that does the connection:

import java.applet.AppletStub;
import java.awt.*;
import java.net.*;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JApplet;

import com.mysql.jdbc.*;


public class MyApplet extends JApplet implements AppletStub {

public String username = "";
public String password = "";
private String err;
private Connection conMysql;

public void init() {
if (!login()) {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"accessdenied.html"),"_top");
}
catch (Exception e) {e.printStackTrace(); }
}
else
{
try
{
Class appletClass = Class.forName("SearchApplet");
JApplet realApplet = (JApplet)appletClass.newInstance();
realApplet.setStub(this);
getContentPane().setLayout( new GridLayout(1,0));
getContentPane().add(realApplet);
realApplet.init();
((SearchApplet)realApplet).setConnection(conMysql);
realApplet.start();
}
catch (Exception e)
{
System.out.println( e );
}
validate();
}
}

public void appletResize( int width, int height )
{
resize( width, height );
}

public boolean login()
{
int tries = 1;
MyLogin2 login = new MyLogin2 (new Frame(""));

while ( tries < 3 )
{
if (login.id)
{
username = login.userNameTF.getText();
password = login.passwdTF.getText();

if ( validateUser(username , password) )
{
login.dispose();
return true;
}
else
{
login.error.setForeground(Color.red);
login.error.setText(err);
login.error.setVisible(true);
login.userNameTF.setText(null);
login.passwdTF.setText(null);
login.id = false;
tries++;
login.setSize(300,200);
login.setVisible(true);
login.requestFocus();

}
}
}
return false;
}

private boolean validateUser(String usr, String pwd)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e)
{
err = e.toString();
//err = "Unable to locate MySQL driver";
return false;
}

try
{
String connect_info = "jdbc:mysql://localhost/library?user="+usr+"&password="+pwd;
conMysql = (Connection) DriverManager.getConnection(connect_info);
}
catch (SQLException e)
{
err = "Login for " + usr + " failed !";
return false;
}

try
{
createTable();
populateTable();
}
catch (SQLException e)
{
err = "Error creating / populating tables !";
return false;
}
return true;
}

private void createTable() throws SQLException
{

Statement stmt = (Statement) conMysql.createStatement();

// Drop table if it exist to allow multiple runs.
try
{
stmt.executeUpdate( "DROP TABLE books" );
}
catch ( Exception e )
{
}

// Create table called books
String sql = "CREATE TABLE books (" +
"ID INT NOT NULL, " +
"Title VARCHAR (50) NOT NULL, " +
"Author VARCHAR (20) NOT NULL, " +
"ISBN VARCHAR (15) NOT NULL, " +
"Available INT NOT NULL," +
"CHECK (Available >= 0)," +
"PRIMARY KEY (ID),"+
"UNIQUE ( ISBN ))";

stmt.executeUpdate(sql);

}

private void populateTable() throws SQLException
{
int iRowCount = 0;

String[] TestData =
{
"( 1, 'SQL Anywhere', 'John Doe', '0074444770', 4)",
"( 2, 'Advanced SQL', 'John John', '0073334770', 5)",
"( 3, 'SQL in a nutshell', 'Mazen Khair', '0072344770', 6)"
};

Statement stmt = (Statement) conMysql.createStatement();

for (int i = 0; i < TestData.length; i++)
{
iRowCount += stmt.executeUpdate(
"INSERT INTO books VALUES " +
TestData );
}
}
}

Any suggestions are appreciated.

Options: ReplyQuote


Subject
Written By
Posted
Connector/J & Applet
November 23, 2004 09:10AM
November 23, 2004 10:35AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.