Search Posts

cannot insert values in my MS access using java

how to – cannot insert values in my MS access using java

i am creating a shipping agency application that’s supposed to add new companies, (insert) data, modify existing companies(modify) and delete companies from the database but unfortunately i am unable to do all that. however i was able to link the whole application to my existing MS Access database.
PLEASE HELP, I appreciated your quick responses on my previous query.

import java.awt.Container;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

    //
    //
   //
  //
  //  Generated by StarUML(tm) Java Add-In
 //
 //  @ Project : Untitled
 //  @ File Name : Companies.java
//  @ Date : 3/22/2018
//  @ Author : izukanji Simumba
//
 //



public class Company extends System {


                public static JFrame Cframe = new JFrame("Companies");
                public static JLabel ca = new JLabel(" Record Name :");     
                public static JLabel cb= new JLabel(" Address :");
                public static JLabel cc = new JLabel(" Mailing List :");
                public static JLabel cd = new JLabel(" Location :");
                public static JLabel ce = new JLabel(" Cost :");
                public static JTextField txtca = new JTextField(10);
                public static JTextField txtcb = new JTextField(10);
                public static JTextField txtcc = new JTextField(10);
                public static JTextField txtcd = new JTextField(10);
                public static JTextField txtce = new JTextField(10);                    
                public static JButton btnadd1 = new JButton(" add ");
                public static JButton btnadd2 = new JButton(" modify ");
                public static JButton btnadd3 = new JButton(" delete ");

                public static void connect(){
                    Connection conn = null;
                    try {
                        //db parameters
                        String path = "jdbc:ucanaccess://C:/sqlite/db1.accdb";
                        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                        conn = DriverManager.getConnection(path);



                        JOptionPane.showMessageDialog(null, "Connection to Sqlite has been established");

                            } catch (SQLException | ClassNotFoundException e) {
                                java.lang.System.out.println(e.getMessage());

                            } finally {
                                try {
                                    if (conn != null) {
                                        conn.close();
                                    }

                                } catch (SQLException ex){
                                    java.lang.System.out.println(ex.getMessage());
                            }  }
                }


                     public Company() {


                            JPanel Companypanel = new JPanel();


                             Cframe.setSize(620,365);
                             Cframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                             Cframe.setVisible(true);
                            Cframe.setBackground(Color.PINK);

                            JPanel Cpanel = new JPanel();  
                            Cpanel.setBackground(new Color(238, 232, 170));
                           Cpanel.setLayout(null);
                           ca.setBounds(30, 51, 115, 14);


                           Cpanel.add(ca);
                           txtca.setBounds(155, 48, 86, 20);
                           Cpanel.add(txtca);
                           cb.setBounds(30, 95, 115, 14);
                           Cpanel.add(cb);
                           txtcb.setBounds(155, 92, 86, 20);
                           Cpanel.add(txtcb);
                           cc.setBounds(335, 51, 112, 14);
                           Cpanel.add(cc);
                           txtcc.setBounds(457, 48, 86, 20);
                           Cpanel.add(txtcc);
                           cd.setBounds(335, 95, 112, 14);
                           Cpanel.add(cd);
                           txtcd.setBounds(457, 92, 86, 20);
                           Cpanel.add(txtcd);
                           ce.setBounds(335, 146, 112, 14);
                           Cpanel.add(ce);
                           txtce.setBounds(457, 143, 86, 20);
                           Cpanel.add(txtce);                                   
                           btnadd1.setBounds(67, 217, 95, 23);
                           Cpanel.add(btnadd1);
                           btnadd2.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                try {
                                    String path = "UPDATE Companies Set NAME = ? , "

                                            + " ADDRESS= ? "
                                         + " LOCATION= ? "
                                      + " COST= ? "
                                            + "WHERE MAILINGLIST = ?";
                                    pst = conn.prepareStatement(path);
                                    pst.setString(1, txtca.getText());
                                    pst.setString(2, txtcb.getText());
                                    pst.setString(3, txtcc.getText());
                                    pst.setString(4, txtcd.getText());
                                    pst.setString(5, txtce.getText());
                                 pst.executeUpdate(path);
                                } catch (Exception ex) {
                                    JOptionPane.showMessageDialog(null, ex);
                                }
                            }
                           });
                           btnadd2.setBounds(214, 217, 115, 23);
                           Cpanel.add(btnadd2);
                           btnadd3.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                try {

                                        String path = "DELETE From COMPANY Set NAME = ? , "
                                                + " ADDRESS= ? "
                                             + " LOCATION= ? "
                                          + " COST= ? "
                                                + "WHERE MAILINGLIST = ?";
                                    pst = conn.prepareStatement(path);
                                    pst.setString(1, txtca.getText());
                                    pst.setString(2, txtcb.getText());
                                    pst.setString(3, txtcc.getText());
                                    pst.setString(4, txtcd.getText());
                                    pst.setString(5, txtce.getText());
                                 pst.executeUpdate(path);
                                } catch (Exception ex) {
                                    JOptionPane.showMessageDialog(null, ex);
                                }
                            }
                           });
                           btnadd3.setBounds(391, 217, 95, 23);
                           Cpanel.add(btnadd3);

                        Cframe.getContentPane().add(Cpanel);
                        btnAction();             






            btnadd1.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent e){
                try {
                    String path="insert into Companies(MAILINGLIST, NAME, LOCATION, ADDRESS, COST) values (?,?,?,?,?)";
                    pst = conn.prepareStatement(path);
                    pst.setString(1, txtca.getText());
                    pst.setString(2, txtcb.getText());
                    pst.setString(3, txtcc.getText());
                    pst.setString(4, txtcd.getText());
                    pst.setString(5, txtce.getText());
                    pst.executeUpdate(path);
                    JOptionPane.showMessageDialog(null, "New Company Added");
                    rs.close();
                    pst.close();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex);
                }
                new Company();
                Cframe.dispose();
            }
        });




                            }

private void btnAction() {
                        // TODO Auto-generated method stub
}





        private String RecordName;
        private String Address;
        private String Location;
        private Integer Cost;
        private String MailingList;
        public void getRecordName() {

        }

        public void setRecordName(String crname) {

        }

        public void getAddress() {

        }

        public void setAddress(String caddress) {

        }

        public void getLocation() {

        }

        public void setLocation(String clocation) {

        }

        public void getCost() {

        }

        public void setCost(Integer ccost) {

        }

        public void getMailingList() {

        }

        public void setMailingList(String cmailinglist) {

        }
    }

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *