我想要做的是使用我的applet页面在mysql数据库上添加数据。我的想法是填写文本字段然后当我点击保存按钮时,所有文本都将添加到mySQL数据库。但我有一个错误说
Access Denied: java.security.AccessControlException:access denied
(java.io.FilePermision "\C:\Program Files(x86)\MySQL\mysql-connector-java-5.1.30\mysql-connector-java-5.1.30-bin.jar" "read")
提前谢谢你。这是我的代码:((
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.JApplet;
import java.sql.*;
import java.net.*;
public class AddBook extends JApplet
{
JButton btnSave, btnCancel;
JLabel lblIsbn, lblTitle, lblCategory, lblAuthor, lblYPublish, lblPublisher, lblCopies, lblYear, lblMsg1;
JTextField txtTitle, txtCategory;
JTextField txtIsbn, txtAuthor, txtPublisher, txtCopies, txtYear;
String ttitle, tauthor, tpublisher, tcategory, tisbn, tyear, tqty;
public void init()
{
Font fonttxt = new Font("Calibri", Font.PLAIN, 16);
Font fontlbl = new Font("Calibri",Font.BOLD, 14);
Font fontbtn = new Font("Calibri",Font.BOLD, 16);
Font fontTitle = new Font("Arial ",Font.BOLD,30);
setLayout(null);
setSize(410, 400);
setBackground(Color.gray);
btnSave = new JButton("Save");
btnSave.setFont(fontbtn);
btnCancel = new JButton("Cancel");
btnCancel.setFont(fontbtn);
lblMsg1 = new JLabel ("ADD BOOK");
lblMsg1.setFont(fontbtn);
lblIsbn = new JLabel("ISBN :");
lblIsbn.setFont(fontlbl);
lblTitle = new JLabel ("Title :");
lblTitle.setFont(fontlbl);
lblCategory = new JLabel ("Category :");
lblCategory.setFont(fontlbl);
lblAuthor = new JLabel ("Author :");
lblAuthor.setFont(fontlbl);
lblYear = new JLabel ("Year Published :");
lblYear.setFont(fontlbl);
lblPublisher = new JLabel ("Publisher :");
lblPublisher.setFont(fontlbl);
lblCopies = new JLabel ("Number of Copies :");
lblCopies.setFont(fontlbl);
JTextField txtIsbn = new JTextField ("",100);
txtIsbn.setFont(fonttxt);
JTextField txtTitle = new JTextField ("",100);
txtTitle.setFont(fonttxt);
JTextField txtCategory = new JTextField ("",100);
txtCategory.setFont(fonttxt);
JTextField txtAuthor = new JTextField ("",100);
txtAuthor.setFont(fonttxt);
JTextField txtPublisher = new JTextField ("",100);
txtPublisher.setFont(fonttxt);
JTextField txtCopies = new JTextField ("",100);
txtCopies.setFont(fonttxt);
JTextField txtYear = new JTextField ("",100);
txtYear.setFont(fonttxt);
lblMsg1.setBounds(10,10,200,40);
lblIsbn.setBounds(10,50,250,30);
txtIsbn.setBounds(130,50,250,30);
lblTitle.setBounds(10,90,250,30);
txtTitle.setBounds(130,90,250,30);
lblCategory.setBounds(10,130,250,30);
txtCategory.setBounds(130,130,250,30);
lblAuthor.setBounds(10,165,250,30);
txtAuthor.setBounds(130,165,250,30);
lblPublisher.setBounds(10,200,250,30);
txtPublisher.setBounds(130,200,250,30);
lblCopies.setBounds(10,240,250,30);
txtCopies.setBounds(130,240,250,30);
lblYear.setBounds(10,280,250,30);
txtYear.setBounds(130,280,250,30);
btnSave.setBounds(40,320,150,35);
btnCancel.setBounds(200,320,150,35);
add(lblMsg1);
add(txtIsbn);
add(txtTitle);
add(txtCategory);
add(txtAuthor);
add(txtPublisher);
add(txtCopies);
add(txtYear);
add(lblIsbn);
add(lblTitle);
add(lblCategory);
add(lblAuthor);
add(lblPublisher);
add(lblCopies);
add(lblYear);
add(btnSave);
add(btnCancel);
tisbn = txtIsbn.getText();
tyear = txtYear.getText();
tqty = txtCopies.getText();
ttitle = txtTitle.getText();
tauthor = txtAuthor.getText();
tcategory = txtCategory.getText();
tpublisher = txtPublisher.getText();
btnSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
//Connection con = null;
//Statement st = null; 1
PreparedStatement pstmt;
Connection con;
if(ae.getSource() == btnSave)
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/library_system", "root", "password");
con.setAutoCommit(false);
pstmt = con.prepareStatement ("INSERT INTO book_records VALUES (?, ?, ?, ?, ?, ?, ?)");
pstmt.setString(1, tisbn);
pstmt.setString(2, ttitle);
pstmt.setString(3, tauthor);
pstmt.setString (4, tyear);
pstmt.setString(5, tpublisher);
pstmt.setString(6, tcategory);
pstmt.setString(7, tqty);
pstmt.executeUpdate();
con.commit();
pstmt.close();
}
catch(Exception err)
{
JOptionPane.showMessageDialog(null, "Access Denied: "+err );
}
}
}
}
});
}
}
答案 0 :(得分:0)
请你试试mysql连接
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
我认为你错过了港口。
答案 1 :(得分:0)
该错误仅表示"访问被拒绝"从Java程序到MySQL jar存在的目录的问题。 您不应该从Java应用程序链接(引用)另一个目录中的jar文件。
通过在应用程序的类路径中添加所需的mysql-connector-java-5.1.30-bin.jar
,可以简单地解决问题。