Applet在netbeans IDE 8.0中运行良好,而不是在浏览器中运行

时间:2014-04-17 18:39:17

标签: java mysql swing netbeans applet

这是将applet连接到数据库的简单Applet代码。 在Netbeans IDE 8.0中尝试此代码时工作正常。它没有显示任何错误。 但在浏览器中显示错误!

这是java applet代码。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;



public class test_applet extends javax.swing.JApplet {

private Connection con;
private Statement st;

@Override
public void init() {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(test_applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(test_applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(test_applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(test_applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the applet */
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                initComponents();
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    bt_clickme = new javax.swing.JButton();
    lab_disp = new javax.swing.JLabel();

    bt_clickme.setText("Click me");
    bt_clickme.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bt_clickmeActionPerformed(evt);
        }
    });

    lab_disp.setText("Click Buttton to connect to database");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(118, 118, 118)
                    .addComponent(bt_clickme, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(110, 110, 110)
                    .addComponent(lab_disp)))
            .addContainerGap(115, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(51, 51, 51)
            .addComponent(lab_disp, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
            .addComponent(bt_clickme)
            .addGap(113, 113, 113))
    );
}// </editor-fold>                        

private void bt_clickmeActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    try
    {
          Class.forName("com.mysql.jdbc.Driver");


         con=DriverManager.getConnection("jdbc:mysql://localhost/sas","root","");
         st=con.createStatement();

         lab_disp.setText("Database is connected successfully");
    }
    catch(Exception e)
            {
                JOptionPane.showMessageDialog(null,"Error found when button is clicked "+e);
            }
}                                          


// Variables declaration - do not modify                     
private javax.swing.JButton bt_clickme;
private javax.swing.JLabel lab_disp;
// End of variables declaration                   
 }

这是由netbeans

生成的html文件
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>



<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

<P>
<APPLET codebase="classes" code="test_applet.class" width=350 height=200></APPLET>
</P>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

整天都在寻找错误! 谁能告诉我这是什么问题? 谢谢

0 个答案:

没有答案