I have an applet which when I test run in Eclipse works perfectly yet when I move into the browser it doesn’t seem to work.
I have read already that applets are not used much anymore but I still want to learn about them for a variety of reasons.
Here is my code. Like I said it works as intended when run in eclipse.
public class WPStool extends Applet implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 6920052961843268403L;
Label lblCustNum, lblCustName, lblAccount, lblEmpID, lblSuccess;
TextField txtCustNum, txtCustName, txtAccount, txtEmpID;
Button bEnter;
boolean blnCorrect;
public void init()
{
lblCustNum = new Label("Customer #");
add(lblCustNum);
txtCustNum = new TextField(20);
add(txtCustNum);
lblCustName = new Label("Customer Name");
add(lblCustName);
txtCustName = new TextField(20);
add(txtCustName);
lblAccount = new Label("Account Type");
add(lblAccount);
txtAccount = new TextField(20);
add(txtAccount);
lblEmpID = new Label("EmployeeID");
add(lblEmpID);
txtEmpID = new TextField(20);
add(txtEmpID);
bEnter = new Button("Enter");
add(bEnter);
bEnter.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bEnter)
{
//registerUser();
int custNum = Integer.parseInt(txtCustNum.getText());
int empID = Integer.parseInt(txtEmpID.getText());
enterInfo(custNum, txtCustName.getText(), txtAccount.getText(), empID);
txtCustNum.setText("");
txtCustName.setText("");
txtAccount.setText("");
txtEmpID.setText("");
}
}
public void enterInfo(int custNuma, String custNamea, String accounta, int empIDa)
{
Connection con = getConnection();
try
{
Statement s = con.createStatement();
String select = "INSERT INTO customerInfo (custNum , custName, account ,empID) VALUES ("+ custNuma +", '"+ custNamea +"', '"+ accounta +"', "+ empIDa +")";
//String select = "INSERT INTO customerInfo (custNum , custName, account ,empID) VALUES (123, 'Jim John Joe', 'New Account', 1234)";
s.executeUpdate(select);
lblSuccess = new Label("Success!");
add(lblSuccess);
}
catch (SQLException e)
{
System.out.println("getClasses method: " + e.getMessage());
lblSuccess = new Label("FAIL!");
add(lblSuccess);
}
}
private Connection getConnection()
{
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/wps";
String user = "root";
String pw = "";
con = DriverManager.getConnection(url, user, pw);
}
catch (ClassNotFoundException e)
{
System.out.println("getConnection ClassNotFound: " + e.getMessage());
System.exit(0);
}
catch (SQLException e)
{
System.out.println("getConnection SQL: " + e.getMessage());
System.exit(0);
}
return con;
}
}
Is there some extra browser settings I need to setup? Also. I am importing the JDBC so I can use sql, do I need to add this somehow?
Maybe from the browser the java code is not able to connect to the database.
You have to change your application structure, and separate the applet part from the server part. Put in the applet part all the user interface logic, then connect to the server part, via RMI or Web Service, sending data, and on server side connect to the database.
Another point event if your client would be able to connect to the database, if you release the applet with connection url
jdbc:mysql://localhost/wpsthen every client will try to connect to a database on the same machine that run applet. That is, if you have 10 clients, every try will search for the database on itself, because you uselocalhost.However I suggest you to separate your application login in the two part I describe you above.