Java Applet 不使用 jaybird 执行 class.forName()



我在 eclipse 中开发一个小程序,当从 eclipse 的 AppletViewer 执行时,它可以正常工作,但当从.html文件执行时,它不会执行 class.forName() 方法。

这里是.html

<!DOCTYPE html>
<html>
<body>
<p>Tarjetero.</p>
<applet 
codebase="classes"
code="ap.class"
width=234 height=274
archive="jaybird-full-2.2.0.jar">
</applet>
</body>
</html>

在同一个文件夹中,我拥有所有创建的.class和 Jaybird .jar存档

这里 java 文件

美联社.java

import javax.swing.JApplet;

public class ap extends JApplet {
/**
 * 
 */
private static final long serialVersionUID = 1L;
ui inter = new ui();
@Override
public void init() {
    // TODO Auto-generated method stub
    //JPanel p = new JPanel();
    //p.add(new JLabel("prueba"));
    inter.setVisible(true);
    this.add(inter);
}
@Override
public void start() {
    // TODO Auto-generated method stub
    super.start();
}
}

用户界面.java

import javax.swing.JPanel;

public class ui extends JPanel {
static JButton btnNewButton;
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * Create the panel.
 */
public ui() {
    setLayout(null);
     btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                btnNewButton.setText("1");
            ConexFirebird cf = new ConexFirebird();
            btnNewButton.setText("2");
            ResultSet rs = cf.EjecutarSentencia("SELECT nombre FROM CLIENTES;");
            btnNewButton.setText("3");

                if(rs.next())
                {
                    btnNewButton.setText("datos");
                }
                else
                {
                    btnNewButton.setText("nodatos");
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                btnNewButton.setText(e.getMessage());
                e.printStackTrace();
            }
        }
    });
    btnNewButton.setBounds(12, 12, 117, 24);
    add(btnNewButton);
    JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
    rdbtnNewRadioButton.setBounds(8, 44, 149, 22);
    add(rdbtnNewRadioButton);
    JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
    chckbxNewCheckBox.setBounds(18, 70, 129, 22);
    add(chckbxNewCheckBox);
}
}

康尼斯火鸟.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.*;
import javax.swing.JOptionPane;

/**
*
* @author Jose
*/
public class ConexFirebird {
// static File directorio= new File("//");
//static String DireccionBD="jdbc:firebirdsql:192.168.47.254/3050:/BBDD/ArenasTiradoJoseMTarjetero.fdb";
static String DireccionBD="jdbc:firebirdsql:localhost/3050:/var/lib/firebird/2.5/data/tarjetavisitas.fdb";
static String Usuario="sysdba";
//static String Contrasena="persiza";
String Contrasena="masterkey";
static String NombreDriver="org.firebirdsql.jdbc.FBDriver";
static Connection Conexion;
static Statement Consulta;
static ResultSet Resultado;
public ConexFirebird()
{
        try
    {
            ui.btnNewButton.setText("10");
       Class.forName(NombreDriver) ;
       ui.btnNewButton.setText("11");
       Conexion = DriverManager.getConnection(DireccionBD, Usuario, Contrasena);           
       ui.btnNewButton.setText("12");
       Consulta = Conexion.createStatement();
       ui.btnNewButton.setText("13");

    }
    catch (ClassNotFoundException e) {
        // TODO: handle exception
        ui.btnNewButton.setText("classNfound");
    }
    catch(Exception e)
    {
        ui.btnNewButton.setText("23");
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
}
public void EjecutarInsert(String Sentencia)
{
    try {
        Consulta.execute(Sentencia);
        JOptionPane.showMessageDialog(null, "Guardado correctamente", "Guardado", JOptionPane.INFORMATION_MESSAGE);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, "Ese DNI ya se encuentra en la base de datos", "Error", JOptionPane.INFORMATION_MESSAGE);
    }
}

public ResultSet EjecutarSentencia(String Sentencia)
{
    try
    {
    Resultado = Consulta.executeQuery(Sentencia);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e.getMessage()+"ejecutar sentencia");
    }
    return Resultado;
}

public void EjecutarUpdate(String Sentencia) throws SQLException
{
   try
    {
    Consulta.executeUpdate(Sentencia);

    }
    catch(SQLException e)
    {
        //JOptionPane.showMessageDialog(null, e.getMessage());
        throw new SQLException("");
    } 
}
public void CerrarConexion()
{
    try
    {
    Consulta.close();
    }
    catch(Exception e)
    {
    }
}
public void insertarConBlob(String nombre, String dni, String correo, File fD, File fA)
{
    try {
        PreparedStatement ps;
        if(fD != null && fA != null)
        {
            ps = Conexion.prepareStatement("INSERT INTO CLIENTES (ID, NOMBRE, DNI, CORREO, FOTODELANTE, FOTODETRAS ) VALUES ( gen_id(id_clientes, 1), '"+nombre+"', '"+dni+"', '"+correo+"', ?, ?)");
            try{
            ps.setBlob(1, new FileInputStream(fD));
            ps.setBlob(2, new FileInputStream(fA));
            ps.execute();
            JOptionPane.showMessageDialog(null, "Guardado correctamente", "Guardado", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(FileNotFoundException e)
            {
                JOptionPane.showMessageDialog(null, "No se encontro el archivo", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(SQLException e)
            {
                JOptionPane.showMessageDialog(null, "Ese DNI ya se encuentra en la base de datos", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if (fD != null)
        {
            ps = Conexion.prepareStatement("INSERT INTO CLIENTES (NOMBRE, DNI, CORREO, FOTODELANTE) VALUES ( '"+nombre+"', '"+dni+"', '"+correo+"', ?)");
            try{
            ps.setBlob(1, new FileInputStream(fD));
            ps.execute();
            }
            catch(FileNotFoundException e)
            {
                JOptionPane.showMessageDialog(null, "No se encontro el archivo", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if (fA != null)
        {
            ps = Conexion.prepareStatement("INSERT INTO CLIENTES (NOMBRE, DNI, CORREO, FOTODETRAS) VALUES ( '"+nombre+"', '"+dni+"', '"+correo+"', ?)");
            try{
            ps.setBlob(1, new FileInputStream(fA));
            ps.execute();
            }
            catch(FileNotFoundException e)
            {
                JOptionPane.showMessageDialog(null, "No se encontro el archivo", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }



    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

我知道所有这些java文件都可以改进。我必须使用该按钮标签更改以进行调试,因为我无法在iceweasel中找到控制台

操作系统:Debian浏览器: 冰鼬Java JDK 1.6Jaybird 2.2.0

提前感谢,您只需要回答问题就问吧! 谢谢!

Jaybird 在设计时并没有考虑到小程序,据我所知,它会生成错误(在浏览器中运行时),因为它会尝试读取系统属性。它将在AppletViewer中工作,因为afaik - 以所有权限运行。

另请参阅:

  • http://tracker.firebirdsql.org/browse/JDBC-254
  • NoClassDefFoundError with jdbc applet

还要确保将jaybird-2.2.1.jarconnector-api-1.5.jarjaybird-full-2.2.1.jar(包含连接器 API 中的类)结合使用,因为没有它,驱动程序将无法工作。

  • 如何为小程序启用控制台

  • 如果您的跟踪中有access denied,则意味着您应该对小程序进行签名

您的启动代码应如下所示

<APPLET 
    codebase="classes" 
    code="ap.class" 
    width=234 height=274 
    archive="jaybird-full-2.2.0.jar">
</APPLET>

最新更新