在Java中获取错误java.lang.reflect.InvocationTargetException

时间:2014-04-22 07:00:55

标签: java html mysql jdbc applet

我在applet中工作,当我使用Netbeans运行它时我没有任何问题但是当我尝试在我的Firefox浏览器上加载applet时,它给了我这个错误:

java.lang.reflect.InvocationTargetException  

这是我的java代码

package com.org.login;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class LoginApplet extends JApplet{
static JTextField name,pass;
static JButton submit;
private static class Handler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String user = name.getText();
        String strpass = pass.getText();
        try {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginApplet.class.getName())
            .log(Level.SEVERE, null, ex);
        }
        try {
            Connection con = DriverManager.getConnection
            ("jdbc:mysql://localhost/login", "root", "");
            Statement st = con.createStatement();
            String query = "SELECT pass FROM info where username='"+user+"'";
            System.out.println(query);
            ResultSet rs = st.executeQuery(query);

            if(rs.next())
            {
                String dbpass = rs.getString(1);
                if(dbpass.equals(strpass)){
                JOptionPane.showMessageDialog
                (null,"Login Successful! ","Success",
                 JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog
                (null,"Login Unsuccessful!","Error",1);
            }
           }
            else
            {
                JOptionPane.showMessageDialog
                (null,"Username not found","Error",1);
            }
        } catch (SQLException ex) {
            Logger.getLogger
            (LoginApplet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
LoginApplet(){
    JFrame main = new JFrame("Login Form");
    main.setBounds(350,150,500,500);
    main.setVisible(true);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    name = new JTextField(10);
    pass = new JTextField(10);
    main.setLayout(new GridLayout(0,1));
    JPanel pane = new JPanel();
    main.add(pane);
    pane.add(new JLabel("Username: "));
    pane.add(name);
    pane.add(new JLabel("Password: "));
    pane.add(pass);
    submit = new JButton("Submit");
    pane.add(submit);
    submit.addActionListener(new Handler());

}
public static void main(String[] args) {
    // TODO code application logic here
    new LoginApplet();
}
}    

我的Applet代码HTML代码

<applet code="com.org.login.LoginApplet.class" 
archive="LoginApplet.jar" width="300" height="300">
</applet>    

尝试在浏览器中运行时出现错误,如下所示

enter image description here

详细错误

Java Plug-in 10.55.2.14
Using JRE version 1.7.0_55-b14 Java HotSpot(TM) Client VM
User home directory = C:\Users\dhineshr
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage 
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
Trace level set to 5: all ... completed.Clear classloader cache ... completed.
basic: PluginMain.unregisterApplet: 1 from mananger 
sun.plugin2.applet.Applet2Manager@11f74aa

如何克服此错误?

1 个答案:

答案 0 :(得分:0)

1)构建jar文件后。如果没有找到签名jar的链接,你是否在浏览器上部署jar之前签了jar。

Signing Jar file

2)清除浏览器上的所有chache。从Java控制面板中选择Temerory Internet文件。

希望这有帮助。