2012-04-10 5 views
0

애플릿이 있습니다. 이클립스에서 테스트 할 때 완벽하게 작동하지만 브라우저로 이동할 때 작동하지 않습니다.애플릿 애플릿 SQL INSERT가 브라우저에서 작동하지 않습니다.

애플릿이 더 이상 많이 사용되지는 않지만 이미 여러 가지 이유로 배울 점이 있습니다.

여기 내 코드입니다. 내가 말했듯이 이클립스에서 실행될 때 의도대로 작동합니다.

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; 
} 

} 

설정해야 할 추가 브라우저 설정이 있습니까? 또한. SQL을 사용할 수 있도록 JDBC를 가져오고 있습니다. 어떻게 든 추가해야합니까?

+1

브라우저에서와 같이 권한 예외가 발생할 수 있습니다. 애플릿은 보안 관리자로 실행됩니다. 사람들이보다 효과적으로 도움을 줄 수 있도록 어떤 예외를 지정해야합니까? – ipolevoy

+0

1) Java 콘솔에서 오류를 확인하십시오. 2) DB가 애플릿과 동일한 서버에 있습니까? 3) AWT 기반 컴포넌트를이 천년기에 사용하지 말고 Swing을 사용하십시오. –

+0

lol 팁 주셔서 감사합니다. 어떤 사람들은 브라우저 애플릿에서 AWT를 선호하지만 나는 이것을 스윙으로 바꿀 것이라고 읽었습니다. – Bullyen

답변

1

아마 브라우저에서 자바 코드가 데이터베이스에 연결할 수 없습니다.

응용 프로그램 구조를 변경하고 애플릿 부분과 서버 부분을 분리해야합니다. 애플릿 부분에 모든 사용자 인터페이스 로직을 넣은 다음 RMI 또는 웹 서비스를 통해 서버 부분에 연결하여 데이터를 보내고 서버 측에서 데이터베이스에 연결합니다.

클라이언트가 데이터베이스에 연결할 수있는 경우 다른 지점 이벤트, 연결 URL이 jdbc:mysql://localhost/wps 인 애플릿을 릴리스하면 모든 클라이언트는 애플릿을 실행하는 동일한 시스템의 데이터베이스에 연결을 시도합니다. 즉, 클라이언트가 10 명인 경우 localhost을 사용하기 때문에 모든 시도가 자체 데이터베이스를 검색합니다.

그러나 위의 두 부분에서 응용 프로그램 로그인을 분리하는 것이 좋습니다.

+0

DB에 연결하거나 다른 Java 클래스를 의미하는 PHP 또는 Javascript 스크립트를 작성 하시겠습니까? 나는 Java에 매우 익숙하므로 바보가되지 않기를 바랍니다. – Bullyen

+1

더 잘 알고 있다면 PHP로 서버 부분을 작성하십시오. 그런 다음 자바에서 PHP 페이지를 호출하여 예를 들어 JSON으로 데이터를 보냅니다. – dash1e

+0

좋은 답변입니다. 그날의 2 번째 마지막 투표 (다음 3 시간, 어쨌든)를 +1했습니다. ;) –

관련 문제