opabc
asked on
JDBC Applet cannot run
Here is the code:
import java.awt.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.URLConnection;
public class LoginApplet extends Applet implements ActionListener
{
//Graphical User Interface
//Labels, TextFields, Buttons and the TextArea
private Label noLabel;
private TextField noTextField;
private Label passLabel;
private TextField passTextField;
private Button clearButton, loginButton;
//JDBC
private String id; // User ID
private String password = null; // User Password
private String sql = null; // SQL for the queries
private String rst = null; // Results String based on the Column Name
private StringBuffer buf; // Buffer to store the data
private Connection con; // Connection to the Datasource
private Driver d; // Driver for the JDBC connection
private Enumeration en; // Driver
private Statement stmt = null; // Statements for the SQL queries
private SQLWarning warning = null; // SQL warning
private ResultSet results = null; // Results that were retrieved
public void init()
{
resize(280,100);
// Border Layout as overall
// NORTH
// Panel FlowLayout LEFT
// Panel GridLayout Labels
// Panel GridLayout TextFields
// Panel FlowLayout RIGHT
// Panel GridLayout Labels
// Panel GridLayout TextFields
// CENTER
// Panel FlowLayout CENTER
Panel mainPanel = new Panel();
mainPanel.setLayout(new BorderLayout());
Panel topPanel = new Panel();
topPanel.setLayout(new FlowLayout());
Panel labelPanel = new Panel();
labelPanel.setLayout(new GridLayout(2,0,10,5));
Panel textfieldPanel = new Panel();
textfieldPanel.setLayout(n ew GridLayout(2,0,10,5));
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENT ER));
noLabel = new Label(" Staff / Admission No. : ");
labelPanel.add(noLabel);
noTextField = new TextField(10);
textfieldPanel.add(noTextF ield);
passLabel = new Label(" Password : ");
labelPanel.add(passLabel);
passTextField = new TextField(5);
passTextField.setEchoChar( '*');
textfieldPanel.add(passTex tField);
loginButton = new Button("Login");
buttonPanel.add(loginButto n);
loginButton.addActionListe ner(this);
clearButton = new Button("Cancel");
buttonPanel.add(clearButto n);
clearButton.addActionListe ner(this);
topPanel.add(labelPanel);
topPanel.add(textfieldPane l);
mainPanel.add(topPanel, (BorderLayout.NORTH));
mainPanel.add(buttonPanel, (BorderLayout.CENTER));
add(mainPanel);
}
/*
public void start()
{
LoginApplet applet = new LoginApplet();
JFrame login = new JFrame("Login"); //set the frame name
login.setContentPane(apple t);
login.setVisible(true);
}
*/
public void actionPerformed(ActionEven t ae)
{
if (ae.getSource().equals(cle arButton)) // button clicked is the cancel button
{
noTextField.setText("");
passTextField.setText("");
}
else // button clicked is the login button
{
String no = noTextField.getText();
String pass = passTextField.getText();
//char pass[] = passTextField.getText();
if (no.equals("") == true) // the staff/admission no textfield is empty
{
//loginTextArea.append("Pl ease enter the Staff / Admission No.\n");
System.out.println("Please enter the Staff / Admission No.\n");
noTextField.requestFocus() ;
}
else if (pass.equals("") == true) // the password textfield is empty
{
//loginTextArea.append("Pl ease enter the Password\n");
System.out.println("Please enter the Password\n");
passTextField.requestFocus ();
}
else // Proceed to the next screen
{
//loginTextArea.append("Pl ease wait.\n");
System.out.println("Please wait.\n");
LoginApplet database = new LoginApplet(no, pass);
// Check if the person exists in the database
if (database.checkDatabase() == true)
{
System.out.println("YES!") ;
//employee = new EmployeeApplet(database); // create the Employee GUI
//employee.setVisible(true );
}
else
{ // The person does not exists in the database
System.out.println("Error! There is no such person!");
noTextField.setText("");
passTextField.setText("");
database.closeStatement();
database.closeConnection() ;
database.deregisterDriver( );
}
}
}
}
// Register the Driver that will be used
// Get the Connection Required to the ODBC datasource
// Check for any Connection Warnings
// Create the Statement
public LoginApplet(String id, String password)
{
this.id = id;
this.password = password;
registerDriver();
getConnection();
getConnectionWarnings();
}
// Check the database for the particular staff or student
public boolean checkDatabase()
{
int rows=-1;
stmt = this.createStatement(); // create statement for the query
results = this.executeQuery("select \"Employee ID\" from " + "\"" + "Employee Brief" + "\"" + " ;");
while (rows != 0)
{
try
{
results.last();
rows = results.getRow();
results.first();
if ((results.getString("Emplo yee ID")).equals(/*this.getId( )*/id) == true)
{
return true;
}
rows = rows - 1;
}
catch(SQLException ex)
{
System.out.println("SQLExc eption: " +ex);
}
}
return false;
}
// Get the Driver Used
public Driver getDriver()
{
return d;
}
// Register the Driver to be Used
public void registerDriver()
{
// REGISTER DRIVER
try
{
d = (Driver)Class.forName("sun .jdbc.odbc .JdbcOdbcD river").ne wInstance( );
//loginTextArea.append("JD BC-ODBC Driver registered successfully.\n");
System.out.println("JDBC-O DBC Driver registered successfully.\n");
}
catch (Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t oString()) ;
}
}
//Deregister the Driver Used
public void deregisterDriver()
{
// DEREGISTER DRIVER
try
{
DriverManager.deregisterDr iver(getDr iver());
//loginTextArea.append("JD BC-ODBC Driver deregistered successfully.\n");
System.out.println("JDBC-O DBC Driver deregistered successfully.\n");
}
catch (Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t oString()) ;
}
}
// Get the Connection Warnings
public void getConnectionWarnings()
{
// GET CONNECTION WARNINGS
try
{
warning = con.getWarnings();
if (warning == null)
{
System.out.println("No Warnings");
//loginTextArea.append("No Warnings.\n");
return;
}
while (warning != null)
{
//loginTextArea.append("Wa rnings." + warning);
System.out.println("Warnin gs." + warning);
warning = warning.getNextWarning();
}
}
catch (Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t oString()) ;
}
}
// Get the Connection
public void getConnection()
{
// GET CONNECTION
try
{
con = DriverManager.getConnectio n("jdbc:od bc:http//c vesc-labpc 09:8080/do c/temp",id ,password) ;
//loginTextArea.append("Co nnection established successfully. - Datasource: temp\n");
System.out.println("Connec tion established successfully. - Datasource: temp\n");
}
catch(Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t oString()) ;
}
}
// Close Connection
public void closeConnection()
{
// CLOSE CONNECTION
try
{
con.close();
System.out.println("Connec tion closed successfully. - Datasource: temp\n");
}
catch (Exception e)
{
System.out.println(e);
}
}
// Create Statement
public Statement createStatement()
{
// CREATE STATEMENT
try
{
stmt = con.createStatement();
System.out.println("Statem ent created successfully.");
}
catch (Exception e)
{
System.out.println(e);
}
return stmt;
}
// Close Statement
public void closeStatement()
{
// CLOSE STATEMENT
try
{
stmt.close();
System.out.println("Statem ent closed successfully.");
}
catch (Exception e)
{
System.out.println(e);
}
}
// Execute Query based on the SQL statement
public ResultSet executeQuery(String sql)
{
// EXECUTE QUERY
try
{
results = stmt.executeQuery(sql);
}
catch (Exception e)
{
System.out.println(e);
}
return results;
}
}
well i have a problem when using the appletviewer to test the applet.
Starting appletviewer for C:\LoginApplet.html
load: LoginApplet.class can't be instantiated.
java.lang.InstantiationExc eption: LoginApplet
at java.lang.Class.newInstanc e0(Native Method)
at java.lang.Class.newInstanc e(Class.ja va:239)
at sun.applet.AppletPanel.cre ateApplet( AppletPane l.java:532 )
at sun.applet.AppletPanel.run Loader(App letPanel.j ava:468)
at sun.applet.AppletPanel.run (Compiled Code)
at java.lang.Thread.run(Threa d.java:479 )
Besides when i try to run on a browser
the applet makes a connection back to the server that it was from.
It states a unsuitable driver.
I am using sun.jdbc.odbc.JdbcOdbcDriv er
well i have already did the part on the ODBC source and create a entry in it.
Email me at opabc@rocketmail.com
Please help!
import java.awt.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.URLConnection;
public class LoginApplet extends Applet implements ActionListener
{
//Graphical User Interface
//Labels, TextFields, Buttons and the TextArea
private Label noLabel;
private TextField noTextField;
private Label passLabel;
private TextField passTextField;
private Button clearButton, loginButton;
//JDBC
private String id; // User ID
private String password = null; // User Password
private String sql = null; // SQL for the queries
private String rst = null; // Results String based on the Column Name
private StringBuffer buf; // Buffer to store the data
private Connection con; // Connection to the Datasource
private Driver d; // Driver for the JDBC connection
private Enumeration en; // Driver
private Statement stmt = null; // Statements for the SQL queries
private SQLWarning warning = null; // SQL warning
private ResultSet results = null; // Results that were retrieved
public void init()
{
resize(280,100);
// Border Layout as overall
// NORTH
// Panel FlowLayout LEFT
// Panel GridLayout Labels
// Panel GridLayout TextFields
// Panel FlowLayout RIGHT
// Panel GridLayout Labels
// Panel GridLayout TextFields
// CENTER
// Panel FlowLayout CENTER
Panel mainPanel = new Panel();
mainPanel.setLayout(new BorderLayout());
Panel topPanel = new Panel();
topPanel.setLayout(new FlowLayout());
Panel labelPanel = new Panel();
labelPanel.setLayout(new GridLayout(2,0,10,5));
Panel textfieldPanel = new Panel();
textfieldPanel.setLayout(n
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENT
noLabel = new Label(" Staff / Admission No. : ");
labelPanel.add(noLabel);
noTextField = new TextField(10);
textfieldPanel.add(noTextF
passLabel = new Label(" Password : ");
labelPanel.add(passLabel);
passTextField = new TextField(5);
passTextField.setEchoChar(
textfieldPanel.add(passTex
loginButton = new Button("Login");
buttonPanel.add(loginButto
loginButton.addActionListe
clearButton = new Button("Cancel");
buttonPanel.add(clearButto
clearButton.addActionListe
topPanel.add(labelPanel);
topPanel.add(textfieldPane
mainPanel.add(topPanel, (BorderLayout.NORTH));
mainPanel.add(buttonPanel,
add(mainPanel);
}
/*
public void start()
{
LoginApplet applet = new LoginApplet();
JFrame login = new JFrame("Login"); //set the frame name
login.setContentPane(apple
login.setVisible(true);
}
*/
public void actionPerformed(ActionEven
{
if (ae.getSource().equals(cle
{
noTextField.setText("");
passTextField.setText("");
}
else // button clicked is the login button
{
String no = noTextField.getText();
String pass = passTextField.getText();
//char pass[] = passTextField.getText();
if (no.equals("") == true) // the staff/admission no textfield is empty
{
//loginTextArea.append("Pl
System.out.println("Please
noTextField.requestFocus()
}
else if (pass.equals("") == true) // the password textfield is empty
{
//loginTextArea.append("Pl
System.out.println("Please
passTextField.requestFocus
}
else // Proceed to the next screen
{
//loginTextArea.append("Pl
System.out.println("Please
LoginApplet database = new LoginApplet(no, pass);
// Check if the person exists in the database
if (database.checkDatabase() == true)
{
System.out.println("YES!")
//employee = new EmployeeApplet(database); // create the Employee GUI
//employee.setVisible(true
}
else
{ // The person does not exists in the database
System.out.println("Error!
noTextField.setText("");
passTextField.setText("");
database.closeStatement();
database.closeConnection()
database.deregisterDriver(
}
}
}
}
// Register the Driver that will be used
// Get the Connection Required to the ODBC datasource
// Check for any Connection Warnings
// Create the Statement
public LoginApplet(String id, String password)
{
this.id = id;
this.password = password;
registerDriver();
getConnection();
getConnectionWarnings();
}
// Check the database for the particular staff or student
public boolean checkDatabase()
{
int rows=-1;
stmt = this.createStatement(); // create statement for the query
results = this.executeQuery("select \"Employee ID\" from " + "\"" + "Employee Brief" + "\"" + " ;");
while (rows != 0)
{
try
{
results.last();
rows = results.getRow();
results.first();
if ((results.getString("Emplo
{
return true;
}
rows = rows - 1;
}
catch(SQLException ex)
{
System.out.println("SQLExc
}
}
return false;
}
// Get the Driver Used
public Driver getDriver()
{
return d;
}
// Register the Driver to be Used
public void registerDriver()
{
// REGISTER DRIVER
try
{
d = (Driver)Class.forName("sun
//loginTextArea.append("JD
System.out.println("JDBC-O
}
catch (Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t
}
}
//Deregister the Driver Used
public void deregisterDriver()
{
// DEREGISTER DRIVER
try
{
DriverManager.deregisterDr
//loginTextArea.append("JD
System.out.println("JDBC-O
}
catch (Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t
}
}
// Get the Connection Warnings
public void getConnectionWarnings()
{
// GET CONNECTION WARNINGS
try
{
warning = con.getWarnings();
if (warning == null)
{
System.out.println("No Warnings");
//loginTextArea.append("No
return;
}
while (warning != null)
{
//loginTextArea.append("Wa
System.out.println("Warnin
warning = warning.getNextWarning();
}
}
catch (Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t
}
}
// Get the Connection
public void getConnection()
{
// GET CONNECTION
try
{
con = DriverManager.getConnectio
//loginTextArea.append("Co
System.out.println("Connec
}
catch(Exception e)
{
System.out.println(e);
//loginTextArea.append(e.t
}
}
// Close Connection
public void closeConnection()
{
// CLOSE CONNECTION
try
{
con.close();
System.out.println("Connec
}
catch (Exception e)
{
System.out.println(e);
}
}
// Create Statement
public Statement createStatement()
{
// CREATE STATEMENT
try
{
stmt = con.createStatement();
System.out.println("Statem
}
catch (Exception e)
{
System.out.println(e);
}
return stmt;
}
// Close Statement
public void closeStatement()
{
// CLOSE STATEMENT
try
{
stmt.close();
System.out.println("Statem
}
catch (Exception e)
{
System.out.println(e);
}
}
// Execute Query based on the SQL statement
public ResultSet executeQuery(String sql)
{
// EXECUTE QUERY
try
{
results = stmt.executeQuery(sql);
}
catch (Exception e)
{
System.out.println(e);
}
return results;
}
}
well i have a problem when using the appletviewer to test the applet.
Starting appletviewer for C:\LoginApplet.html
load: LoginApplet.class can't be instantiated.
java.lang.InstantiationExc
at java.lang.Class.newInstanc
at java.lang.Class.newInstanc
at sun.applet.AppletPanel.cre
at sun.applet.AppletPanel.run
at sun.applet.AppletPanel.run
at java.lang.Thread.run(Threa
Besides when i try to run on a browser
the applet makes a connection back to the server that it was from.
It states a unsuitable driver.
I am using sun.jdbc.odbc.JdbcOdbcDriv
well i have already did the part on the ODBC source and create a entry in it.
Email me at opabc@rocketmail.com
Please help!
which browser u use? post exception stack trace
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
ANother problem,
you are constructing applet with parameter constructor.
Since your class extends Applet ( Base class ).
Base class will ne constructed before derived class.
Base class has no constructor taking
Applet(String,String).
So you can't instantiate applet like that.
IF you use parameter less constructor
in your class also,
Before init,Applet constructor will be executed .
Best of luck
ANother problem,
you are constructing applet with parameter constructor.
Since your class extends Applet ( Base class ).
Base class will ne constructed before derived class.
Base class has no constructor taking
Applet(String,String).
So you can't instantiate applet like that.
IF you use parameter less constructor
in your class also,
Before init,Applet constructor will be executed .
Best of luck