how to insert java gui form data into ms access database?
843859May 25 2010 — edited Nov 20 2014Hi,
I've the following code wrote to save the form data into a test file. could any one help me the code how to add this data in ms access database with name car.mdb. I've setup the System DSN as carDSN but unable to write the exact code.
Can anyone add me the code in the below to connect to the access db,
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class CRegistration {
private static JTextField eNumberTxt ;
private static JTextField modelTxt ;
private static JTextField makerTxt ;
private static JTextField ownerTxt;
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Car Regtistration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame.setDefaultLookAndFeelDecorated(true);
//textfields and labels
eNumberTxt = new JTextField();
modelTxt = new JTextField();
makerTxt = new JTextField();
ownerTxt = new JTextField();
JLabel eNumberLabel = new JLabel("Engine Number");
JLabel modelLabel = new JLabel("Model");
JLabel makerLabel = new JLabel("Maker");
JLabel ownerLabel = new JLabel("Owner");
JButton saveButton = new JButton("Save");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(5,2,5,5));
frame.add(eNumberLabel);
frame.add(eNumberTxt);
frame.add(modelLabel);
frame.add(modelTxt);
frame.add(makerLabel);
frame.add(makerTxt);
frame.add(ownerLabel);
frame.add(ownerTxt);
frame.add(saveButton);
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
saveButtonActionPerformed(evt);
}catch(Exception e){
e.printStackTrace();
}
}
private void saveButtonActionPerformed(ActionEvent evt) throws IOException{
File f = new File("C:/File.txt");
// FileOutputStream fos = new FileOutputStream(f);
FileWriter fw = new FileWriter(f);
String datawrite = "Engine Number : " + eNumberTxt.getText() +
"\nModel : " + modelTxt.getText() +
"\nMaker : " + makerTxt.getText() +
"\nOwner : " + ownerTxt.getText() ;
for (int i = 0; i < datawrite.length(); i++) {
fw.write((int) datawrite.charAt(i));
}
System.out.println("File Save in C:/File.txt");
fw.close();
}
});
//Display the window.
frame.setSize(400, 400);
frame.setLocation(400, 250);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}