1
//JDBC using Applet
/*
<applet code = JDBC1 height = 170 width = 350 >
</applet>
*/

import javax.swing.*; //JApplet,JLabel,JButton

import java.applet.*; //Applet
import java.awt.*; //Layout

import java.awt.event.*; //Events

import java.sql.*; //JDBC

public class JDBC1 extends Applet implements Runnable,ActionListener
{
JFrame frame ;
JPanel panel1;
JLabel lblNo,lblName,lblBdate ;
JTextField txtNo,txtName,txtBdate;
JButton btn;

Statement st1;
Connection cn;
ResultSet rs;

    public JDBC1()
    {
        try
        {
            panel1 = new JPanel ();
            panel1.setLayout(new GridLayout(0,2));

            lblNo = new JLabel ("Roll No : ");
            panel1.add(lblNo);

            txtNo = new JTextField(15);
            panel1.add(txtNo);

            lblName = new JLabel ("Name : ");
            panel1.add(lblName);

            txtName = new JTextField(15);
            panel1.add(txtName);

            lblBdate = new JLabel ("Birth Date : ");
            panel1.add(lblBdate);

            txtBdate = new JTextField(15);
            panel1.add(txtBdate); //Add textarea to the panel.

            add(panel1); //Add panel to the aaplet.

            btn = new JButton ("First");
            add(btn); //Add button to the applet.
            btn.addActionListener(this);

            btn = new JButton ("Next");
            add(btn);
            btn.addActionListener(this);

            btn = new JButton ("Last");
            add(btn);
            btn.addActionListener(this);

            btn = new JButton ("Prev");
            add(btn);
            btn.addActionListener(this);

            btn = new JButton ("Save");
            add(btn);
            btn.addActionListener(this);

            btn = new JButton ("Reset");
            add(btn);
            btn.addActionListener(this);

            btn = new JButton ("Delete");
            add(btn);
            btn.addActionListener(this);

            btn = new JButton ("Update");
            add(btn);
            btn.addActionListener(this);

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load the JDBC-ODBC bridge driver
            cn = DriverManager.getConnection("jdbc:odbc:STUDENT_MYDSN"); // conection to databse

            st1 = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

            String query = "select * from Student";
            rs = st1.executeQuery(query);

        }

        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void init()
    {
        try
        {
            Thread th = new Thread (this);
            th.start(); 
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }


    public void run ()
    {
        try
        {

            while(true)
            {
                JDBC1 my = new JDBC1();
                repaint();
                Thread.sleep(1000);
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    public void paint (Graphics g)
    {
        super.paint(g);
        java.util.Date dt = new java.util.Date();
        String myDate = dt+"";
        g.drawString(myDate,100,150);
    }

    void getRecord()
    {
        try
        {
            txtNo.setText((String) rs.getObject(1));
        }

        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        try
        {
            String name = ((JButton) e.getSource()).getText();

            if (name == "First")
            {

                System.out.println("First");
            }

            if (name == "Prev")
            {
                System.out.println("Prev");
            }

            if (name == "Next")
            {
                System.out.println("Next");
            }

            if (name == "Last")
            {
                System.out.println("First");
            }

            if (name == "Save")
            {
                System.out.println("First");
            }

            if (name == "Delete")
            {
                System.out.println("First");
            }

            if (name == "Update")
            {
                System.out.println("First");
            }

            if (name == "Reset")
            {
                System.out.println("First");
            }
        }

        catch (Exception ex)
        {
            System.out.println(ex.toString());
            ex.printStackTrace();
        }
    }

}

通过将此 Applet 与 JDBC 一起使用,我将遇到以下错误

java.security.AccessControlException: access denied ("java.util.PropertyPermission" "file.encoding"
            at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
            at java.security.AccessController.checkPermission(AccessController.java:560)
            at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
            at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1302)
            at java.lang.System.getProperty(System.java:706)
            at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:142)
            at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
            at java.sql.DriverManager.getConnection(DriverManager.java:579)
            at java.sql.DriverManager.getConnection(DriverManager.java:243)
            at JDBC1.<init>(JDBC1.java:87)
            at JDBC1.run(JDBC1.java:123)
            at java.lang.Thread.run(Thread.java:722)
4

2 回答 2

1

如果不采取措施为小程序授予适当的权限,您将无法在小程序中运行 JDBC。Oracle的本教程详细介绍了该主题。

于 2012-11-23T18:54:17.827 回答
0

您将无法从(不受信任的)小程序访问 ODBC 驱动程序,就像您无法读取本地文件系统一样。

有只需要套接字权限的“纯 Java”JDBC 驱动程序。如果数据库服务器可通过与交付小程序的 Web 服务器相同的来源(“相同来源策略”)获得(并且网络上没有任何干扰),则这将起作用。

但是,通过网络向最终用户公开数据库可能是个坏主意。通常,您会通过 Web 服务器公开安全接口。此外,如果小程序是通过网络加载的,您不太可能想要选择本地配置的数据库。

(与表达问题的最小完整程序相比,问题中的代码似乎非常长,但我可以继续批评代码。)

于 2012-11-24T14:42:28.227 回答