嵌入 jsp 时,数据库值未显示在 applet 中

发布于 2025-01-06 03:05:14 字数 8042 浏览 1 评论 0原文

我的小程序运行良好,并显示了我需要的数据库中的所有数据。但是,问题是当我将创建的 jar 文件嵌入到 jsp 上时。它没有显示数据库中的数据。

<applet code="myform.Form" archive="applet.jar" width="600" height="480"/>

小程序正在 jsp 页面上运行,但组合框中的项目(来自数据库)无法选择。 jar 文件位于 jsp 页面所在的同一目录中。 提前致谢。

这是我的小程序代码

package myform;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JComboBox;

public class Form extends javax.swing.JApplet {

    /** Initializes the applet Form */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            String connstr="jdbc:oracle:thin:@localhost:1521/XE";
            String uname="epolicia";
            String pass="admin";
            Class.forName("oracle.jdbc.OracleDriver").newInstance();
            Connection connect =DriverManager.getConnection(connstr,uname,pass);
            // System.out.println("Oracle driver is loaded");
            Statement state=connect.createStatement();
            ResultSet rs = state.executeQuery("SELECT * FROM ZONE");

            while (rs.next()) {
                zoneList.addItem(rs.getString(2)+" ("+rs.getString(1)+")");
                //System.out.println(rs.getString(1));
            }
        } catch(Exception ex) {
            //list.addItem(ex);
        }
    }

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

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        zoneList = new javax.swing.JComboBox();
        addDistrict = new javax.swing.JButton();
        cancelDistrict = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel1.setText("District ID :");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel2.setText("District Name :");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel3.setText("Zone :");

        addDistrict.setFont(new java.awt.Font("Times New Roman", 1, 12));
        addDistrict.setText("Add");


        addDistrict.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLabel5.setText("index: " + zoneList.getSelectedItem());//addDistrictActionPerformed(evt);
            }
        });

        cancelDistrict.setFont(new java.awt.Font("Times New Roman", 1, 12));
        cancelDistrict.setText("Cancel");
        cancelDistrict.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelDistrictActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel4.setText("Add New District");

        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(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                            .addComponent(jTextField2)
                            .addComponent(zoneList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(addDistrict)
                        .addGap(28, 28, 28)
                        .addComponent(cancelDistrict))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(163, 163, 163)
                        .addComponent(jLabel5)))
                .addContainerGap(87, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel4)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zoneList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addDistrict)
                    .addComponent(cancelDistrict))
                .addGap(35, 35, 35)
                .addComponent(jLabel5)
                .addContainerGap(77, Short.MAX_VALUE))
        );

        jLabel5.getAccessibleContext().setAccessibleName("lbl");
    }// </editor-fold>                        

    private void addDistrictActionPerformed(java.awt.event.ActionEvent evt) {                                            

    }                                           

    private void cancelDistrictActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        System.exit(0);
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton addDistrict;
    private javax.swing.JButton cancelDistrict;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JComboBox zoneList;
    // End of variables declaration                   

}

My applet is running well and showing all the data from the database, which i required. But, the problem is when i embed the created jar file on jsp. it didn't show the data from the database.

<applet code="myform.Form" archive="applet.jar" width="600" height="480"/>

applet is running on the jsp page, but in the combo box items (from the database) are not available to select.
jar file is located in the same directory, where jsp page is located.
Thanks in advance.

this is my applet code

package myform;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JComboBox;

public class Form extends javax.swing.JApplet {

    /** Initializes the applet Form */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            String connstr="jdbc:oracle:thin:@localhost:1521/XE";
            String uname="epolicia";
            String pass="admin";
            Class.forName("oracle.jdbc.OracleDriver").newInstance();
            Connection connect =DriverManager.getConnection(connstr,uname,pass);
            // System.out.println("Oracle driver is loaded");
            Statement state=connect.createStatement();
            ResultSet rs = state.executeQuery("SELECT * FROM ZONE");

            while (rs.next()) {
                zoneList.addItem(rs.getString(2)+" ("+rs.getString(1)+")");
                //System.out.println(rs.getString(1));
            }
        } catch(Exception ex) {
            //list.addItem(ex);
        }
    }

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

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        zoneList = new javax.swing.JComboBox();
        addDistrict = new javax.swing.JButton();
        cancelDistrict = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel1.setText("District ID :");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel2.setText("District Name :");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel3.setText("Zone :");

        addDistrict.setFont(new java.awt.Font("Times New Roman", 1, 12));
        addDistrict.setText("Add");


        addDistrict.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLabel5.setText("index: " + zoneList.getSelectedItem());//addDistrictActionPerformed(evt);
            }
        });

        cancelDistrict.setFont(new java.awt.Font("Times New Roman", 1, 12));
        cancelDistrict.setText("Cancel");
        cancelDistrict.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelDistrictActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel4.setText("Add New District");

        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(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                            .addComponent(jTextField2)
                            .addComponent(zoneList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(addDistrict)
                        .addGap(28, 28, 28)
                        .addComponent(cancelDistrict))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(163, 163, 163)
                        .addComponent(jLabel5)))
                .addContainerGap(87, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel4)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zoneList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addDistrict)
                    .addComponent(cancelDistrict))
                .addGap(35, 35, 35)
                .addComponent(jLabel5)
                .addContainerGap(77, Short.MAX_VALUE))
        );

        jLabel5.getAccessibleContext().setAccessibleName("lbl");
    }// </editor-fold>                        

    private void addDistrictActionPerformed(java.awt.event.ActionEvent evt) {                                            

    }                                           

    private void cancelDistrictActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        System.exit(0);
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton addDistrict;
    private javax.swing.JButton cancelDistrict;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JComboBox zoneList;
    // End of variables declaration                   

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2025-01-13 03:05:15

在服务器端进行 JDBC 访问并为小程序提供 Web 服务是更好的方法:this线程很好地涵盖了这个主题。

作为答案发布,因为评论变得非常拥挤,但这可能应该作为重复项关闭。

Doing your JDBC access on the server side and providing a web service for the applet is the better approach: this thread covers the topic pretty well.

Posting as an answer because the comments are getting pretty crowded, but this should probably be closed as a duplicate.

梦年海沫深 2025-01-13 03:05:15

您是否没有将 Oracle JDBC 驱动程序与 JAR 一起部署?

要包含 Oracle 驱动程序依赖项,您需要将 ojdbc6.jar (或其他版本)放在服务器上的 applet.jar 旁边(或者放在本地文件夹中,如果您这样做的话)重新在本地运行)。

然后你需要让你的浏览器 Java 类路径知道它。您可以通过将其添加到逗号后的 archive 属性来实现此目的:

或将其添加到 applet.jar 的 Manifest 文件中(如果有多个 JAR,请用空格分隔它们): Class -小路: ojdbc6.jar

编辑
Oracle 在其文档中特别提到可以通过 Applet 使用其 JDBC 驱动程序。然而,他们提到了安全问题等。

ojdbc14.jar 给出的异常可能意味着您正在尝试访问另一台数据库服务器 - 而不是来自那个小程序。如果您从本地文件系统运行小程序,这是非常合乎逻辑的。

为了避免此错误,您需要按照 他们的文档第 24.2.2 节

Could it be that you are not deploying Oracle JDBC drivers along with your JAR?

To include Oracle driver dependency you will need to put ojdbc6.jar (or another version) next to the applet.jar on the server (or in the local folder if you're running locally).

Then you need to make your browser Java classpath aware of it. You can do that by either adding it to the archive attribute after comma:

<applet code="myform.Form" archive="applet.jar,ojdbc6.jar" width="600" height="480"/>

or by adding it to applet.jar's Manifest file (if there are more than one JARs separate them by space): Class-Path: ojdbc6.jar

EDIT:
Oracle has specifically mentioned in their documentation that it was possible to use their JDBC driver from Applets. However they mention security issues etc.

The exception given by the ojdbc14.jar could mean that you're trying to access a different DB server - not the one applet is coming from. If you're running applet from local filesystem that's pretty logical.

To avoid this error you will need to sign your applet as described in section 24.2.2 of their doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文