工作,我有一個小程序,當我在Eclipse測試運行工作完全但當我進入這似乎並沒有工作的瀏覽器。Applet的SQL INSERT而非瀏覽器
我已閱讀已經是小程序沒有使用太多了,但我還是想了解他們的各種原因。
這是我的代碼。就像我說的那樣,它在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;
}
}
是否需要設置一些額外的瀏覽器設置?也。我正在導入JDBC,所以我可以使用SQL,我需要添加這個不知何故?
您可能會遇到權限異常,例如Applets在安全管理器下運行的瀏覽器。你需要指定你正在得到什麼異常,以便人們可以更有效地幫助你 – ipolevoy 2012-04-10 06:27:37
1)一定要檢查Java控制檯是否有錯誤。 2)數據庫是否與applet位於同一臺服務器上? 3)不要在這個千年中使用基於AWT的組件,請使用Swing。 – 2012-04-10 06:54:28
哈哈謝謝你的提示。我讀過一些人更喜歡瀏覽器applet中的swing,但我會將其轉換爲swing。 – Bullyen 2012-04-10 22:53:43