package sa00366_surrey_ac_uk;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
*
* @author I am CS
*/
public class CopyOfStartForm {
private static final String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim();
public static final String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb";
private static Connection con = null;
private static Statement stat = null;
ResultSet rs;
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
public JButton btnInsert;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
CopyOfStartForm window = new CopyOfStartForm();
window.frame.setVisible(true);
} catch (Exception e) {
}
}
});
}
/**
* Create the application.
*/
public CopyOfStartForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void insertperson() throws Exception {
String sql = "INSERT INTO person (code, name , income , gender) values ("
+ textField.getText() +
",'" + textField_1.getText() +
",'" + textField_2.getText() +
",'" + textField_3.getText() + "')";
System.out.print(sql);
try {
Class.forName(MSAccessDriver);
String db = "jdbc:odbc:MyDatabase"; // db = database string stored in the database
con = DriverManager.getConnection(db);
stat = con.createStatement();
stat.execute(sql);
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textField.setText("");
textField_1.setText("");
textField_2.setText("");
textField_3.setText("");
JOptionPane.showMessageDialog(null, "Save Complete Successfully");
}
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Person Code");
lblNewLabel.setBounds(30, 30, 61, 14);
lblNewLabel.setToolTipText("Person Code");
frame.getContentPane().add(lblNewLabel);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(30, 67, 46, 14);
frame.getContentPane().add(lblFullName);
JLabel lblNewLabel_1 = new JLabel("Income");
lblNewLabel_1.setBounds(30, 102, 46, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(30, 139, 46, 14);
frame.getContentPane().add(lblGender);
textField = new JTextField();
textField.setBounds(122, 27, 124, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(122, 64, 124, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(122, 99, 124, 20);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(122, 136, 124, 20);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);
JButton btnInsert = new JButton("Insert");
btnInsert.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
insertperson();
} catch (Exception e) {
}
}
});
btnInsert.setBounds(63, 189, 89, 23);
frame.getContentPane().add(btnInsert);
}
}