带有JDBC的Applet - java.security.AccessControlException:访问被拒绝

时间:2012-11-23 18:32:44

标签: java security jdbc applet

//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)

2 个答案:

答案 0 :(得分:1)

如果不采取措施为applet提供适当的权限,您将无法在applet中运行JDBC。 Oracle的this tutorial详细介绍了该主题。

答案 1 :(得分:0)

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

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

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

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