AccessControlException

AccessControlException

本文介绍了小程序使用JDBC - java.security.AccessControlException:拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  // JDBC使用小程序
/ *
<小程序code = JDBC1高度= 170宽度= 350>
< /小程序>
* /进口的javax.swing *。 // JApplet的,JLabel的,一个JButton导入的java.applet。*; //小程序
进口java.awt中的*。 //布局java.awt.event中导入*。 //活动导入的java.sql *。 // JDBC公共类JDBC1扩展的Applet实现Runnable,的ActionListener
{
JFrame的框架;
JPanel的PANEL1;
JLabel的lblNo,lbl​​Name,lblBdate;
JTextField的txtNo,txtName的,txtBdate;
JButton的BTN;声明ST1;
连接CN;
ResultSet的RS;    公共JDBC1()
    {
        尝试
        {
            PANEL1 =新JPanel();
            panel1.setLayout(新的GridLayout(0,2));            lblNo =新的JLabel(卷号:);
            panel1.add(lblNo);            txtNo =新的JTextField(15);
            panel1.add(txtNo);            lblName =新的JLabel(姓名:);
            panel1.add(lblName);            txtName的=新的JTextField(15);
            panel1.add(txtName的);            lblBdate =新的JLabel(出生日期:);
            panel1.add(lblBdate);            txtBdate =新的JTextField(15);
            panel1.add(txtBdate); //添加文本区域到面板上。            加(PANEL1); //添加面板的aaplet。            BTN =的新的JButton(第一);
            加(BTN); //添加按钮的小程序。
            btn.addActionListener(本);            BTN =的新的JButton(下一步);
            加(BTN);
            btn.addActionListener(本);            BTN =的新的JButton(最后);
            加(BTN);
            btn.addActionListener(本);            BTN =的新的JButton(preV);
            加(BTN);
            btn.addActionListener(本);            BTN =的新的JButton(保存);
            加(BTN);
            btn.addActionListener(本);            BTN =的新的JButton(重置);
            加(BTN);
            btn.addActionListener(本);            BTN =的新的JButton(删除);
            加(BTN);
            btn.addActionListener(本);            BTN =的新的JButton(更新);
            加(BTN);
            btn.addActionListener(本);            的Class.forName(sun.jdbc.odbc.JdbcOdbcDriver中); //加载JDBC-ODBC桥驱动
            CN =的DriverManager.getConnection(JDBC:ODBC:STUDENT_MYDSN); //连接如要DATABSE            ST1 = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);            查询字符串=从学生选择*;
            RS = st1.executeQuery(查询);        }        赶上(例外五)
        {
            e.printStackTrace();
        }
    }    公共无效的init()
    {
        尝试
        {
            主题日=新主题(本);
            th.start();
        }
        赶上(异常前)
        {
            的System.out.println(ex.toString());
        }
    }
    公共无效的run()
    {
        尝试
        {            而(真)
            {
                JDBC1我=新JDBC1();
                重绘();
                视频下载(1000);
            }
        }
        赶上(异常前)
        {
            的System.out.println(ex.toString());
        }
    }    公共无效漆(图形G)
    {
        super.paint(G);
        java.util.Date DT =新java.util.Date();
        字符串数值指明MyDate = DT +;
        g.drawString(数值指明MyDate,100,150);
    }    无效getRecord()
    {
        尝试
        {
            txtNo.setText((字符串)rs.getObject(1));
        }        赶上(例外五)
        {
            的System.out.println(e.toString());
        }
    }    公共无效的actionPerformed(ActionEvent的五)
    {
        尝试
        {
            字符串名称=((JButton的)e.getSource())的getText()。            如果(名称==第一)
            {                的System.out.println(第一);
            }            如果(名称==preV)
            {
                的System.out.println(preV);
            }            如果(名称==下一步)
            {
                的System.out.println(下一步);
            }            如果(名称==一个)
            {
                的System.out.println(第一);
            }            如果(名称==保存)
            {
                的System.out.println(第一);
            }            如果(名称==删除)
            {
                的System.out.println(第一);
            }            如果(名称==更新)
            {
                的System.out.println(第一);
            }            如果(名称==复位)
            {
                的System.out.println(第一);
            }
        }        赶上(异常前)
        {
            的System.out.println(ex.toString());
            ex.printStackTrace();
        }
    }}

通过使用这种小程序使用JDBC,我会越来越低于误差

  java.security.AccessControlException:访问被拒绝(java.util.PropertyPermission的file.encoding
            在java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
            在java.security.AccessController.checkPermission(AccessController.java:560)
            在java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
            在java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1302)
            在java.lang.System.getProperty(System.java:706)
            在sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:142)
            在sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
            在java.sql.DriverManager.getConnection(DriverManager.java:579)
            在java.sql.DriverManager.getConnection(DriverManager.java:243)
            在JDBC1<&初始化GT;(JDBC1.java:87)
            在JDBC1.run(JDBC1.java:123)
            在java.lang.Thread.run(Thread.java:722)


解决方案

您将无法在一个applet不采取措施,给予适当的小程序权限运行的JDBC。该主题进行了详细的覆盖。

//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();
        }
    }

}

By using this Applet with JDBC , I will getting below error

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)
解决方案

You won't be able to run JDBC in an applet without taking steps to give the applet appropriate permissions. The topic is covered in detail by this tutorial from Oracle.

这篇关于小程序使用JDBC - java.security.AccessControlException:拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:34