
Q.How to insert data into ms access data in java using swing ? Q.How to slove (access denided ("java.lang.RuntimePermission" "accessClassInPackage.sun.jdbc.odbc")" these exception ?

Here is an example that connect MS Access database with java swing. But first of all you need to set the connection.
Follow these steps:
1)Go to the start->Control Panel->Administrative Tools-> data sources.
2)Click Add button and select the driver Microsoft Access Driver(*.mdb).
3)After selecting the driver, click finish button.
4)Then give Data Source Name and click ok button.
5)Your DSN will get created.
6) Restart your compiler and compile your java code.
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class InsertData{
public static void main(String[] args){
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab1=new JLabel("Name : ");
JLabel lab2=new JLabel("Address: ");
final JTextField text=new JTextField(20);
final JTextArea area=new JTextArea(5,20);
JScrollPane pane=new JScrollPane(area);
JButton b=new JButton();
lab1.setBounds(10,10,100,20);
text.setBounds(120,10,150,20);
lab2.setBounds(10,40,100,20);
area.setBounds(120,40,150,60);
b.setBounds(120,110,100,20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String st1=text.getText();
String st2=area.getText();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:roseindia");
PreparedStatement pstmt = conn.prepareStatement("insert into data(name,address) values(?,?)");
pstmt.setString(1, st1);
pstmt.setString(2, st2 );
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
catch(Exception ex){}
}
});
f.add(lab1);
f.add(text);
f.add(lab2);
f.add(area);
f.add(b);
f.setVisible(true);
f.setSize(350,200);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.