To use below example, create a DSN name of “ShivaEvening“, which point the Microsoft access database file provided in this article.
Download below files:
package com.shivasoft.event; import java.awt.Container; import java.awt.GridLayout; 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.Statement; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class JDBCSwing implements ActionListener { JLabel lblFName,lblLname,lblAddress,lblSalary,lblF,lblL,lblA,lblS, lblFVal,lblLVal,lblAVal,lblSVal; JTextField txtFName,txtLName,txtAddress,txtSalary; JButton btnAdd,btnUpdate,btnDelete,btnPrev,btnNext; ResultSet rs; public static void main(String[] args) { JDBCSwing obj = new JDBCSwing(); obj.createUI(); } private void createUI() { JFrame frame = new JFrame("JDBC All in One"); JPanel pnlInput = new JPanel(new GridLayout(4,2)); lblFName = new JLabel(" First Name : "); txtFName = new JTextField(15); lblLname = new JLabel(" Last Name : "); txtLName = new JTextField(); lblAddress = new JLabel(" Address : "); txtAddress = new JTextField(); lblSalary = new JLabel(" Salary : "); txtSalary = new JTextField(); pnlInput.add(lblFName); pnlInput.add(txtFName); pnlInput.add(lblLname); pnlInput.add(txtLName); pnlInput.add(lblAddress); pnlInput.add(txtAddress); pnlInput.add(lblSalary); pnlInput.add(txtSalary); JPanel pnlButton = new JPanel(new GridLayout(1,3)); btnAdd = new JButton("Add"); btnAdd.addActionListener(this); btnUpdate = new JButton("Update"); btnUpdate.addActionListener(this); btnDelete = new JButton("Delete"); btnDelete.addActionListener(this); pnlButton.add(btnAdd); pnlButton.add(btnUpdate); pnlButton.add(btnDelete); JPanel pnlNavigate = new JPanel(new GridLayout(1,2)); btnPrev = new JButton(" << "); btnPrev.setActionCommand("Prev"); btnPrev.addActionListener(this); btnNext = new JButton(" >> "); btnNext.setActionCommand("Next"); btnNext.addActionListener(this); pnlNavigate.add(btnPrev); pnlNavigate.add(btnNext); JPanel pnlNavAns = new JPanel(new GridLayout(4,2)); lblF = new JLabel(" First Name : "); lblFVal = new JLabel("Val"); lblL = new JLabel(" Last Name : "); lblLVal = new JLabel("Val"); lblA = new JLabel(" Address : "); lblAVal = new JLabel("Val"); lblS = new JLabel(" Salary : "); lblSVal = new JLabel("Val"); pnlNavAns.add(lblF); pnlNavAns.add(lblFVal); pnlNavAns.add(lblL); pnlNavAns.add(lblLVal); pnlNavAns.add(lblA); pnlNavAns.add(lblAVal); pnlNavAns.add(lblS); pnlNavAns.add(lblSVal); Container cn = frame.getContentPane(); cn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS)); frame.add(pnlInput); frame.add(pnlButton); frame.add(pnlNavAns); frame.add(pnlNavigate); //If this will not be written, the only frame will be closed // but the application will be active. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent evt) { String action = evt.getActionCommand(); if(action.equals("Add")) { addOperation(); }else if(action.equals("Update")) { updateOperation(); }else if(action.equals("Delete")) { deleteOperation(); }else if(action.equals("Prev")) { preNavigation(); }else if(action.equals("Next")) { nextNavigation(); } } private void addOperation() { try { //Load Jdbc Odbc Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening"); String sql = "INSERT INTO Employee (FName,LName,Address,Salary) " + "Values ('"+txtFName.getText()+"'," + "'"+txtLName.getText()+"'," + "'"+txtAddress.getText()+"'," + "'"+txtSalary.getText()+"')"; Statement st = con.createStatement(); st.execute(sql); JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added", JOptionPane.INFORMATION_MESSAGE); clearControls(); }catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } } private void updateOperation() { try { //Load Jdbc Odbc Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening"); String sql = "Update Employee " + "SET LName = '"+txtLName.getText()+"'," + "Address = '"+txtAddress.getText()+"'," + "Salary = '"+txtSalary.getText()+"'" + "Where FName = '"+txtFName.getText()+"'"; JOptionPane.showMessageDialog(null, sql,"Record Updated", JOptionPane.INFORMATION_MESSAGE); Statement st = con.createStatement(); st.execute(sql); JOptionPane.showMessageDialog(null, "Record Update Succesfully.", "Record Updated",JOptionPane.INFORMATION_MESSAGE); clearControls(); }catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } } private void deleteOperation() { int ans = JOptionPane.showConfirmDialog(null, "Are you sure to delete the Record ?", "Delete Record", JOptionPane.YES_NO_OPTION); if(ans == JOptionPane.YES_OPTION) { try{ //Load Jdbc Odbc Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening"); String sql = "Delete FROM Employee where FName = '"+txtFName.getText()+"'"; Statement st = con.createStatement(); st.execute(sql); }catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } JOptionPane.showMessageDialog(null, "Record Deleted","Success", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "Operation Canceled","Cancel", JOptionPane.INFORMATION_MESSAGE); } } private void preNavigation() { try{ if(rs == null) { //Load Jdbc Odbc Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening"); String sql = "SELECT * FROM Employee"; Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = st.executeQuery(sql); } if(rs.previous()) { populateValue(); } }catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } } private void nextNavigation() { try{ if(rs == null) { //Load Jdbc Odbc Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening"); String sql = "SELECT * FROM Employee"; Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = st.executeQuery(sql); } if(rs.next()) { populateValue(); } }catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } } private void populateValue() throws Exception { String fName = rs.getString("FName"); String lName = rs.getString("LName"); String add = rs.getString("Address"); String sal = rs.getString("Salary"); lblFVal.setText(fName); lblLVal.setText(lName); lblAVal.setText(add); lblSVal.setText(sal); txtFName.setText(fName); txtLName.setText(lName); txtAddress.setText(add); txtSalary.setText(sal); } private void clearControls() { txtFName.setText(""); txtLName.setText(""); txtAddress.setText(""); txtSalary.setText(""); } }
Please anybody help me. I could get my codes run. What should I do for the package com.shivasoft.event.
Hi Pradeep,
Remove the package declaration and from Control panel create DSN pointing Microsoft access database provided in code.
Regards,
Shivasoft team
thanks but i have used ms sql server. Will it work . I have problem at @override also .I get a illegal character message when i run.
I get following message too. I hope after solving @override error this will be automatically solved.
JDBCSwing should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in JDBCSwing
Which java version do you have? remove @override statement.
thanks for replying so fast.
I have jdk 1.4.
when i removed @override statement It get compiled. But while running i get the following message
java.lang.Error: Do not use javax.swing.JFrame.add() use javax.swing.JFrame.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:444)
at javax.swing.JFrame.addImpl(JFrame.java:470)
at java.awt.Container.add(Container.java:321)
at JDBCSwing.createUI(JDBCSwing.java:116)
at JDBCSwing.main(JDBCSwing.java:28)
Exception in thread “main” .
Ok.
Instead of JFrame.add(), use JFrame.getContentPane().add().
Because above program is run in JDK 5 environment.
sorry Shraddha it doesnot works.
Hi Pradeep,
Create one demo application with simple Jframe and button and check whether it runs or not?
import java.awt.Container;
import java.awt.GridLayout;
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.Statement;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
Class Test
{
public void main(String[] args)
{
JFrame f = new JFrame(“Test”);
JButton btn = new JButton();
f.add(btn);
f.setVisible(true);
}
}
No this donot compiled. Though there is a class.it give a message like class or interface excepted. And illegal character for JFrame.
Try this.
import javax.swing.JButton;
import javax.swing.JFrame;
class Test
{
public static void main(String[] args)
{
JFrame f = new JFrame(“Test”);
JButton btn = new JButton(“ShivaSoft”);
f.add(btn);
f.pack();
f.setVisible(true);
}
}
On JFrame i get the following mesage
illegal character: 8220
illegal character: 8221
‘)’ expected
And same for JButton
Check this URL:
http://www.skylit.com/javamethods/JM-Appendix-B.html#illegalchar
Its all because of Copy Paste.
what should i do then.Because i am using this example in my project.
Hi Pradeep,
Please try simple program of displaying JFrame first.
WHen u copy text from site, its getting converted into illegal char.
I have tried simple program also.But every program about JFrame give similar error. I think jdk1.4 donot supports this JFrame.
Tons of thanks for all your kind effort.. It’s done successfully.. You also took so much time to reply all queries.. It helped me a lot as I’m trying it for the first time.. I really appreciate your work 🙂
i understood it..Thank you very much..
Thank you very mach for publishing this code i had find several day this kind of Code Finally I found
i am indika from Sri Lanka
how to add a table in the form?
Thank you very much. This was very helpful. I like the way you comment on your code, it is a good way to guide people.Â
I have created one similar form with more facility and of course no bugs. I am going to upload the sourcecode on my website in the next two/three weeks. A snapshot can be found here : http://tuim.freeiz.com/images/12d2b1c7b282.jpg
and my website is here :
http://tinyurl.com/9hptaoj
(I used tinyurl because of spams)
I’m making a program for my project for database using java as front end. In this I’m using JLabel for displaying errors on JFrame. There are two buttons one-submit for validation & sending the data to database & another-cancel to erase all data written in JTextField. I would like to to know what should I do to make the foll things happen:-
1) When I click submit it should display error on JFrame using JLabel
2 )When I click cancel all data alongwith those error should be erased or removed &
3) When I click submit it should still show errors.
Please help
I can’t download Employee.mdb…
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
lets see
…….
how to execute above code
hi pls am using java jdk8 and the code is not working pls i need your help