2012-06-26 29 views
-2

我已經做了一個基本的應用程序向MySQL服務器輸入數據。由於某種原因,不能訪問驅動程序來連接到數據庫。這是代碼。預先感謝所有的答案Swing應用程序到小應用程序

import javax.swing.*; 
import java.awt.event.*; 
import java.sql.*; 

public class Action extends JApplet { 

    public void init() { 
    } 

    public Action() { 
    JButton button = new JButton("Click here"); 
    button.addActionListener(new EventHandler()); 
    add(button); 
    } 
} 

public class EventHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent e) { 
    try{ 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     String url = "jdbc:mysql://localhost:3306/testgui"; 
     Connection con= DriverManager.getConnection(url,"root", null); 
     String str = JOptionPane.showInputDialog(null,"Enter type"); 
     String abc = JOptionPane.showInputDialog(null,"Enter number"); 
     Statement st= con.createStatement(); 
     st.executeUpdate("insert into tb1 values (null,'"+str+"',"+abc+")");  
    } 
    catch(Exception e1){ 
     e1.printStackTrace(); 
    } 
    } 
} 
+3

完成你得到錯誤信息?異常? – John3136

+0

另外,您可能需要在try塊中添加finally子句,以關閉語句和連接。 – Morfic

+0

@John 3136我沒有得到任何例外或消息。當我在eclipse中運行它時,它工作正常,但是當我在瀏覽器上運行它時,它不會在MySQL服務器數據庫中添加數據 – user1481851

回答

3

請閱讀What Applets Can and Cannot Do

未簽名的小程序無法執行以下操作:

  • 他們不能訪問客戶端資源,如本地文件系統,可執行文件系統剪貼板和打印機。
  • 他們無法連接到任何第三方服務器或從任何第三方服務器檢索資源(除了源自它的服務器以外的任何服務器)。
  • 它們無法加載本機庫。
  • 他們不能更改SecurityManager。
  • 他們無法創建ClassLoader。
  • 他們無法讀取某些系統屬性。有關禁止系統屬性的列表,請參閱System Properties

也許簡單可行的方法是看Java Web Start,通過@Andrew Thompson