{"id":936,"date":"2010-08-31T20:21:24","date_gmt":"2010-08-31T14:51:24","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=936"},"modified":"2010-08-31T20:21:24","modified_gmt":"2010-08-31T14:51:24","slug":"jdbc-example-with-microsoft-access-in-swing","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/jdbc-example-with-microsoft-access-in-swing\/","title":{"rendered":"JDBC Example with Microsoft Access in Swing, Next and Previous navigation"},"content":{"rendered":"<p>To use below example, create a DSN name of &#8220;<span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;\">ShivaEvening<\/span>&#8220;, which point the Microsoft access database file provided in this article.<\/p>\n<p>Download below files:<\/p>\n<p><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/JDBCAllinOne.java_.txt\">JDBCAllinOne.java<\/a><\/p>\n<p><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Employee.mdb\">Employee.mdb<\/a><\/p>\n<p><strong>Code:<\/strong><br \/>\n<a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/JDBC.bmp\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-939\" title=\"Java Database Connectivity -  JDBC\" src=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/JDBC.bmp\" alt=\"Java Database Connectivity -  JDBC\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.shivasoft.event;\n\nimport java.awt.Container;\nimport java.awt.GridLayout;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.Statement;\n\nimport javax.swing.BoxLayout;\nimport javax.swing.JButton;\nimport javax.swing.JFrame;\nimport javax.swing.JLabel;\nimport javax.swing.JOptionPane;\nimport javax.swing.JPanel;\nimport javax.swing.JTextField;\n\npublic class JDBCSwing implements ActionListener {\n\n\tJLabel lblFName,lblLname,lblAddress,lblSalary,lblF,lblL,lblA,lblS,\n\tlblFVal,lblLVal,lblAVal,lblSVal;\n\tJTextField txtFName,txtLName,txtAddress,txtSalary;\n\tJButton btnAdd,btnUpdate,btnDelete,btnPrev,btnNext;\n\tResultSet rs;\n\tpublic static void main(String&#x5B;] args) {\n\t\tJDBCSwing obj = new JDBCSwing();\n\t\tobj.createUI();\n\t}\n\tprivate void createUI()\n\t{\n\t\tJFrame frame = new JFrame(&quot;JDBC All in One&quot;);\n\n\t\tJPanel pnlInput = new JPanel(new GridLayout(4,2));\n\n\t\tlblFName = new JLabel(&quot;  First Name : &quot;);\n\t\ttxtFName = new JTextField(15);\n\n\t\tlblLname = new JLabel(&quot;  Last Name : &quot;);\n\t\ttxtLName = new JTextField();\n\n\t\tlblAddress = new JLabel(&quot;  Address : &quot;);\n\t\ttxtAddress = new JTextField();\n\n\t\tlblSalary = new JLabel(&quot;  Salary : &quot;);\n\t\ttxtSalary = new JTextField();\n\n\t\tpnlInput.add(lblFName);\n\t\tpnlInput.add(txtFName);\n\n\t\tpnlInput.add(lblLname);\n\t\tpnlInput.add(txtLName);\n\n\t\tpnlInput.add(lblAddress);\n\t\tpnlInput.add(txtAddress);\n\n\t\tpnlInput.add(lblSalary);\n\t\tpnlInput.add(txtSalary);\n\n\t\tJPanel pnlButton = new JPanel(new GridLayout(1,3));\n\n\t\tbtnAdd = new JButton(&quot;Add&quot;);\n\t\tbtnAdd.addActionListener(this);\n\n\t\tbtnUpdate = new JButton(&quot;Update&quot;);\n\t\tbtnUpdate.addActionListener(this);\n\n\t\tbtnDelete = new JButton(&quot;Delete&quot;);\n\t\tbtnDelete.addActionListener(this);\n\n\t\tpnlButton.add(btnAdd);\n\t\tpnlButton.add(btnUpdate);\n\t\tpnlButton.add(btnDelete);\n\n\t\tJPanel pnlNavigate = new JPanel(new GridLayout(1,2));\n\t\tbtnPrev = new JButton(&quot; &lt;&lt; &quot;);\n\t\tbtnPrev.setActionCommand(&quot;Prev&quot;);\n\t\tbtnPrev.addActionListener(this);\n\n\t\tbtnNext = new JButton(&quot; &gt;&gt; &quot;);\n\t\tbtnNext.setActionCommand(&quot;Next&quot;);\n\t\tbtnNext.addActionListener(this);\n\n\t\tpnlNavigate.add(btnPrev);\n\t\tpnlNavigate.add(btnNext);\n\n\t\tJPanel pnlNavAns = new JPanel(new GridLayout(4,2));\n\n\t\tlblF = new JLabel(&quot;  First Name : &quot;);\n\t\tlblFVal = new JLabel(&quot;Val&quot;);\n\n\t\tlblL = new JLabel(&quot;  Last Name : &quot;);\n\t\tlblLVal = new JLabel(&quot;Val&quot;);\n\n\t\tlblA = new JLabel(&quot;  Address : &quot;);\n\t\tlblAVal = new JLabel(&quot;Val&quot;);\n\n\t\tlblS = new JLabel(&quot;  Salary : &quot;);\n\t\tlblSVal = new JLabel(&quot;Val&quot;);\n\n\t\tpnlNavAns.add(lblF);\n\t\tpnlNavAns.add(lblFVal);\n\n\t\tpnlNavAns.add(lblL);\n\t\tpnlNavAns.add(lblLVal);\n\n\t\tpnlNavAns.add(lblA);\n\t\tpnlNavAns.add(lblAVal);\n\n\t\tpnlNavAns.add(lblS);\n\t\tpnlNavAns.add(lblSVal);\n\n\t\tContainer cn = frame.getContentPane();\n\t\tcn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS));\n\n\t\tframe.add(pnlInput);\n\t\tframe.add(pnlButton);\n\t\tframe.add(pnlNavAns);\n\t\tframe.add(pnlNavigate);\n\n\t\t\/\/If this will not be written, the only frame will be closed\n\t\t\/\/ but the application will be active.\n\t\tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n\t\tframe.pack();\n\t\tframe.setVisible(true);\n\t}\n\t@Override\n\tpublic void actionPerformed(ActionEvent evt) {\n\n\t\tString action = evt.getActionCommand();\n\t\tif(action.equals(&quot;Add&quot;))\n\t\t{\n\t\t\taddOperation();\n\t\t}else if(action.equals(&quot;Update&quot;))\n\t\t{\n\t\t\tupdateOperation();\n\t\t}else if(action.equals(&quot;Delete&quot;))\n\t\t{\n\t\t\tdeleteOperation();\n\t\t}else if(action.equals(&quot;Prev&quot;))\n\t\t{\n\t\t\tpreNavigation();\n\t\t}else if(action.equals(&quot;Next&quot;))\n\t\t{\n\t\t\tnextNavigation();\n\t\t}\n\t}\n\tprivate void addOperation()\n\t{\n\t\ttry\n\t\t{\n\t\t\t\/\/Load Jdbc Odbc Driver\n\t\t\tClass.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);\n\t\t\tConnection con = DriverManager.getConnection(&quot;jdbc:odbc:ShivaEvening&quot;);\n\n\t\t\tString sql = &quot;INSERT INTO Employee (FName,LName,Address,Salary) &quot; +\n\t\t\t\t\t&quot;Values ('&quot;+txtFName.getText()+&quot;',&quot; +\n\t\t\t\t\t\t\t&quot;'&quot;+txtLName.getText()+&quot;',&quot; +\n\t\t\t\t\t\t\t&quot;'&quot;+txtAddress.getText()+&quot;',&quot; +\n\t\t\t\t\t\t\t&quot;'&quot;+txtSalary.getText()+&quot;')&quot;;\n\n\t\t\tStatement st = con.createStatement();\n\t\t\tst.execute(sql);\n\n\t\t\tJOptionPane.showMessageDialog(null, &quot;Record Added Succesfully.&quot;,&quot;Record Added&quot;,\n                        JOptionPane.INFORMATION_MESSAGE);\n\t\t\tclearControls();\n\t\t}catch(Exception e)\n\t\t{\n\t\t\tJOptionPane.showMessageDialog(null, e.getMessage(),&quot;Error&quot;,\n                        JOptionPane.ERROR_MESSAGE);\n\t\t}\n\t}\n\tprivate void updateOperation()\n\t{\n\t\ttry\n\t\t{\n\t\t\t\/\/Load Jdbc Odbc Driver\n\t\t\tClass.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);\n\t\t\tConnection con = DriverManager.getConnection(&quot;jdbc:odbc:ShivaEvening&quot;);\n\n\t\t\tString sql = &quot;Update Employee &quot; +\n\t\t\t\t\t        &quot;SET LName = '&quot;+txtLName.getText()+&quot;',&quot; +\n\t\t\t\t\t\t\t&quot;Address = '&quot;+txtAddress.getText()+&quot;',&quot; +\n\t\t\t\t\t\t\t&quot;Salary = '&quot;+txtSalary.getText()+&quot;'&quot; +\n\t\t\t\t\t\t\t&quot;Where FName = '&quot;+txtFName.getText()+&quot;'&quot;;\n\n\t\t\tJOptionPane.showMessageDialog(null, sql,&quot;Record Updated&quot;,\n                        JOptionPane.INFORMATION_MESSAGE);\n\t\t\tStatement st = con.createStatement();\n\t\t\tst.execute(sql);\n\n\t\t\tJOptionPane.showMessageDialog(null, &quot;Record Update Succesfully.&quot;,\n                        &quot;Record Updated&quot;,JOptionPane.INFORMATION_MESSAGE);\n\t\t\tclearControls();\n\t\t}catch(Exception e)\n\t\t{\n\t\t\tJOptionPane.showMessageDialog(null, e.getMessage(),&quot;Error&quot;,\n                        JOptionPane.ERROR_MESSAGE);\n\t\t}\n\t}\n\tprivate void deleteOperation()\n\t{\n\t\tint ans = JOptionPane.showConfirmDialog(null,\n\t\t\t\t&quot;Are you sure to delete the Record ?&quot;, &quot;Delete Record&quot;,\n                           JOptionPane.YES_NO_OPTION);\n\t\tif(ans == JOptionPane.YES_OPTION)\n\t\t{\n\t\t\ttry{\n\t\t\t\/\/Load Jdbc Odbc Driver\n\t\t\tClass.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);\n\t\t\tConnection con = DriverManager.getConnection(&quot;jdbc:odbc:ShivaEvening&quot;);\n\n\t\t\tString sql = &quot;Delete FROM Employee where FName = '&quot;+txtFName.getText()+&quot;'&quot;;\n\n\t\t\tStatement st = con.createStatement();\n\t\t\tst.execute(sql);\n\t\t\t}catch(Exception e)\n\t\t\t{\n\t\t\t\tJOptionPane.showMessageDialog(null, e.getMessage(),&quot;Error&quot;,\n                                JOptionPane.ERROR_MESSAGE);\n\t\t\t}\n\t\t\tJOptionPane.showMessageDialog(null, &quot;Record Deleted&quot;,&quot;Success&quot;,\n                        JOptionPane.INFORMATION_MESSAGE);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tJOptionPane.showMessageDialog(null, &quot;Operation Canceled&quot;,&quot;Cancel&quot;,\n                        JOptionPane.INFORMATION_MESSAGE);\n\t\t}\n\t}\n\tprivate void preNavigation()\n\t{\n\t\ttry{\n\t\t\tif(rs == null)\n\t\t\t{\n\t\t\t\/\/Load Jdbc Odbc Driver\n\t\t\tClass.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);\n\t\t\tConnection con = DriverManager.getConnection(&quot;jdbc:odbc:ShivaEvening&quot;);\n\n\t\t\tString sql = &quot;SELECT * FROM Employee&quot;;\n\n\t\t\tStatement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,\n                        ResultSet.CONCUR_UPDATABLE);\n\t\t\trs = st.executeQuery(sql);\n\t\t\t}\n\t\t\tif(rs.previous())\n\t\t\t{\n\t\t\t\tpopulateValue();\n\t\t\t}\n\t\t\t}catch(Exception e)\n\t\t\t{\n\t\t\t\tJOptionPane.showMessageDialog(null, e.getMessage(),&quot;Error&quot;,\n                                JOptionPane.ERROR_MESSAGE);\n\t\t\t}\n\t}\n\tprivate void nextNavigation()\n\t{\n\t\ttry{\n\t\t\tif(rs == null)\n\t\t\t{\n\t\t\t\/\/Load Jdbc Odbc Driver\n\t\t\tClass.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);\n\t\t\tConnection con = DriverManager.getConnection(&quot;jdbc:odbc:ShivaEvening&quot;);\n\n\t\t\tString sql = &quot;SELECT * FROM Employee&quot;;\n\n\t\t\tStatement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,\n                        ResultSet.CONCUR_UPDATABLE);\n\t\t\trs = st.executeQuery(sql);\n\t\t\t}\n\t\t\tif(rs.next())\n\t\t\t{\n\t\t\t\tpopulateValue();\n\t\t\t}\n\t\t\t}catch(Exception e)\n\t\t\t{\n\t\t\t\tJOptionPane.showMessageDialog(null, e.getMessage(),&quot;Error&quot;,\n                                JOptionPane.ERROR_MESSAGE);\n\t\t\t}\n\t}\n\tprivate void populateValue() throws Exception\n\t{\n\t\tString fName = rs.getString(&quot;FName&quot;);\n\t\tString lName = rs.getString(&quot;LName&quot;);\n\t\tString add = rs.getString(&quot;Address&quot;);\n\t\tString sal = rs.getString(&quot;Salary&quot;);\n\n\t\tlblFVal.setText(fName);\n\t\tlblLVal.setText(lName);\n\t\tlblAVal.setText(add);\n\t\tlblSVal.setText(sal);\n\n\t\ttxtFName.setText(fName);\n\t\ttxtLName.setText(lName);\n\t\ttxtAddress.setText(add);\n\t\ttxtSalary.setText(sal);\n\t}\n\tprivate void clearControls()\n\t{\n\t\ttxtFName.setText(&quot;&quot;);\n\t\ttxtLName.setText(&quot;&quot;);\n\t\ttxtAddress.setText(&quot;&quot;);\n\t\ttxtSalary.setText(&quot;&quot;);\n\t}\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JDBC &#8211; Java Database Connectivity example in Swing<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[329,130,192],"class_list":["post-936","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-jdbc","tag-swing"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1767,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/xml-tree-viewer-using-sax-parser-in-java-with-jtreejfilechooser-component-of-swing\/","url_meta":{"origin":936,"position":0},"title":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing, How to refresh Jtree when content changes , Expand or collapse JTree","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"XML Tree Viewer using SAX Parser in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/XML-Tree-Viewer-using-SAX-Parser-in-JAVA.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1825,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-thread-timertask\/","url_meta":{"origin":936,"position":1},"title":"Java Thread &#8211; Executor framework, Timer and TimerTask","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Tutorial and example of Executor framework, Timer and TimerTask over Thread","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Animation of ProgressBar in Swing Using TimerTask","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Animation-of-ProgressBar-Swing-Using-TimerTask.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":867,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/textbox-button-anonymous-actionlistener\/","url_meta":{"origin":936,"position":2},"title":"Event Handling using Anonymous Class in Applet","author":"Jitendra","date":"August 25, 2010","format":false,"excerpt":"Source code to demonstrate the usage of anonymous class in Applet","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Event Handling using Anonymous Class in Applet ","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/AnonymousClass1.jpeg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":862,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/textbox-button-anonymous-actionlistner\/","url_meta":{"origin":936,"position":3},"title":"Event Handling using Inner Class in Applet","author":"Jitendra","date":"August 25, 2010","format":false,"excerpt":"Demonstrates the example for handling event using Inner Class in Java Applet","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Event Handling using Inner Class in Applet","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/InnerClassButton.jpeg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2120,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/validate-xml-schema-using-dom-parser-and-sax-parser-in-java-using-swing\/","url_meta":{"origin":936,"position":4},"title":"Validate XML Schema Using DOM Parser and SAX Parser in JAVA Using Swing","author":"Jitendra","date":"May 13, 2011","format":false,"excerpt":"Validate XML Schema Using DOM Parser and SAX Parser in JAVA Using Swing","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Input Screen - XML Schema Validation in JAVA Using DOM and SAX Parser","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/05\/Input-Screen-XML-Schema-Validation-in-JAVA-Using-DOM-and-SAX-Parser.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2691,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-soap-message-using-java\/","url_meta":{"origin":936,"position":5},"title":"Create SOAP message using Java","author":"Jitendra","date":"February 2, 2012","format":false,"excerpt":"Step by Step Example of creating SOAP Message using core Java API","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"SOAP Message Format for Web Services","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/02\/SOAP.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=936"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}