Results 1 to 1 of 1
  1. #1
    Handler is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Connection to .mdb from within a package

    Java Code:
    package myDB;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.Timer;
    import javax.swing.table.*;
    import java.sql.*;
    public class CarSearchDataBase implements ActionListener
    {
    
        Connection con;
        private static final String DATABASE_FILE_NAME = "Database1.mdb"; //DATABASE SE NAAM
        private static final String DRIVER = "jdbc:odbc:DRIVER=" +
            "Microsoft Access Driver (*.mdb)};" + "DBQ=" + new File (DATABASE_FILE_NAME).getAbsolutePath ();
        static boolean printErr = false;
    
    
    
        JFrame frame = new JFrame ("Car Search");
        Container pane = frame.getContentPane ();
    
        myPanel mypane = new myPanel ();
        JPanel searchPane = new JPanel ();
        JLabel lbl1 = new JLabel ("Make");
        JLabel lbl2 = new JLabel ("Model");
        JLabel lbl3 = new JLabel ("Year");
        JLabel lbl4 = new JLabel ("Min Price");
        JLabel lbl5 = new JLabel ("Max Price");
        JLabel lbl6 = new JLabel ("         ");
        JLabel lbl7 = new JLabel ("         ");
        JComboBox make = new JComboBox ();
        JComboBox model = new JComboBox ();
        JComboBox year = new JComboBox ();
        JTextField minp = new JTextField (10);
        JTextField maxp = new JTextField (10);
        JButton search = new JButton ("Search");
        JButton clear = new JButton ("Clear");
    
        String makeS;
        String modelS;
        String yearS;
        String priceS;
    
        public CarSearchDataBase ()
        {
            myUtil.println ("begin");
            frame.setSize (700, 500);
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            pane.add (searchPane, "North");
            pane.add (mypane, "Center");
            searchPane.setLayout (new GridLayout (2, 6));
    
            searchPane.add (lbl1);
            searchPane.add (lbl2);
            searchPane.add (lbl3);
            searchPane.add (lbl4);
            searchPane.add (lbl5);
            searchPane.add (search);
    
            searchPane.add (make);
            searchPane.add (model);
            searchPane.add (year);
            searchPane.add (minp);
            searchPane.add (maxp);
            searchPane.add (clear);
    
            make.addActionListener (this);
            model.addActionListener (this);
            year.addActionListener (this);
            search.addActionListener (this);
            clear.addActionListener (this);
    
            frame.setVisible (true);
            openDB ();
            checkCombo (make, 0);
            checkCombo (model, 1);
            checkCombo (year, 2);
        }
    
    
        void openDB ()
        {
            try
            {
                Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
            }
            catch (java.lang.ClassNotFoundException e)
            {
                System.err.print ("ClassNotFoundException: ");
                System.err.println (e.getMessage ());
            }
            try
            {
                System.out.println (DRIVER);
                con = DriverManager.getConnection (DRIVER);
            }
            catch (SQLException ex)
            {
                System.err.println ("SQLException: " + ex.getMessage ());
            }
        }
    
    
        private String[] getResult (int line)
        {
            String query = "Select * from Cars";
            try
            {
                int row = 0;
                Statement st = con.createStatement ();
                ResultSet rs = st.executeQuery (query);
                while (rs.next ())
                {
                    row++;
                    if (line == row)
                    {
                        String make = rs.getString ("Make");
                        String model = rs.getString ("Model");
                        String year = rs.getString ("YearMade");
                        String price = rs.getString ("Price");
                        String colour = rs.getString ("Colour");
                        String owner = rs.getString ("Owner");
                        String result[] = {make, model, year, colour, price, owner};
                        return result;
                    }
                }
                return null;
            }
            catch (SQLException ew)
            {
                JOptionPane.showMessageDialog (null, "SQLException: " + ew.getMessage ());
                return null;
            }
        }
    
    
        public void checkCombo (JComboBox box, int pos)
        {
            myUtil.println ("##Begin of checkCombo");
            box.removeAllItems ();
            box.addItem ("");
            String query = "Select * from Cars";
            try
            {
                int x = 0;
                Statement st = con.createStatement ();
                ResultSet rs = st.executeQuery (query);
                while (rs.next ())
                {
                    String make = rs.getString ("Make");
                    String model = rs.getString ("Model");
                    String year = rs.getString ("YearMade");
                    String price = rs.getString ("Price");
                    String colour = rs.getString ("Colour");
                    String owner = rs.getString ("Owner");
                    String result[] = {make, model, year, colour, price, owner};
                    box.addItem (result [pos]);
                    for (int y = 0 ; y < box.getItemCount () ; y++)
                        for (int z = 0 ; z < box.getItemCount () ; z++)
                            if (z != y)
                                if (box.getItemAt (y).equals (box.getItemAt (z)))
                                    box.removeItemAt (z);
    
    
                }
            }
            catch (SQLException ew)
            {
                JOptionPane.showMessageDialog (null, "SQLException: " + ew.getMessage ());
            }
        }
    
    
        public void actionPerformed (ActionEvent e)
        {
            //Make combo
            if (e.getSource () == make)
            {
                String temp = (String) make.getSelectedItem ();
                if (temp != null && !temp.equals (""))
                {
                    makeS = temp;
                    model.removeAllItems ();
                    model.addItem ("");
                    try
                    {
                        int row = 0;
    
                        while (true)
                        {
                            row++;
                            String[] result = getResult (row);
    
                            if (result [0].equals (makeS))
                                model.addItem (result [1]);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    checkCombo (model, 1);
                }
            }
            //model combo
            if (e.getSource () == model)
            {
                String temp = (String) model.getSelectedItem ();
                if (temp != null && !temp.equals (""))
                {
                    modelS = temp;
                    year.removeAllItems ();
                    year.addItem ("");
                    try
                    {
                        int row = 0;
    
                        while (true)
                        {
                            row++;
                            String[] result = getResult (row);
    
                            if (result [1].equals (makeS))
                                model.addItem (result [2]);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.out.println (e);
                    }
                }
                else
                {
                    checkCombo (year, 2);
                }
            }
            //year combo
            if (e.getSource () == year)
            {
                String temp = (String) year.getSelectedItem ();
                if (temp != null && !temp.equals (""))
                {
                    yearS = (String) year.getSelectedItem ();
                }
            }
            //search
            if (e.getSource () == search)
            {
                mypane.removeAllRows ();
                try
                {
                    int row = 0;
                    while (true)
                    {
                        row++;
                        String[] result = getResult (row);
                        String[] toSearch = {"", "", "", ""};
    
                        if (makeS != null)
                        {
                            toSearch [0] = makeS;
                        }
                        else if (modelS != null)
                        {
                            toSearch [1] = modelS;
                        }
                        else if (yearS != null)
                        {
                            toSearch [2] = yearS;
                        }
                        if (result [0].startsWith (toSearch [0]) && result [1].startsWith (toSearch [1]) && result [2].startsWith (toSearch [2]) && priceC (result [4]))
                        {
                            result [4] = "R " + result [4];
                            printArray (result);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                clear (0);
            }
            //clear
            if (e.getSource () == clear)
            {
                clear (1);
            }
        }
    
    
    
        private void clear (int x)
        {
            makeS = null;
            modelS = null;
            yearS = null;
            priceS = null;
    
            minp.setText ("");
            maxp.setText ("");
    
            checkCombo (make, 0);
            checkCombo (model, 1);
            checkCombo (year, 2);
            if (x == 1)
            {
                mypane.removeAllRows ();
            }
        }
    
    
    
        private boolean priceC (String ps)
        {
            try
            {
                int price = Integer.parseInt (ps);
                int min = Integer.parseInt (minp.getText ());
                int max = Integer.parseInt (maxp.getText ());
                if (ps != null)
                    if (price >= min && price <= max)
                        return true;
                return false;
            }
            catch (Exception e)
            {
                return true;
            }
        }
    
    
        private void printArray (String[] result)
        {
            mypane.addRow (result);
        }
    
    
        public static void main (String args[])
        {
            CarSearchDataBase con = new CarSearchDataBase ();
        }
    }
    class myUtil
    { //myUtil.println();
        public static void println (Object obj)
        {
            if (CarSearchDataBase.printErr)
            {
                System.out.println (obj + "");
            }
        }
    }
    Java Code:
    package myDB;
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    
    class myPanel extends JPanel
    {
        Vector head = new Vector ();
        Vector data = new Vector ();
        Vector rowData = new Vector ();
    
        JTable mytable;
        JScrollPane jsp;
        myPanel ()
        {
    
            head.add ("Manufacturer:");
            head.add ("Model:");
            head.add ("Colour:");
            head.add ("Year:");
            head.add ("Price:");
            head.add ("Owner:");
    
            mytable = new JTable (data, head);
            jsp = new JScrollPane (mytable);
            jsp.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            add (jsp);
        }
    
    
        public void addRow (String[] result)
        {
            Vector rowData = new Vector ();
            for (int x = 0 ; x < 6 ; x++)
            {
                rowData.add (result [x]);
            }
            data.add (rowData);
            mytable.updateUI ();
        }
    
    
        public void removeAllRows ()
        {
            while (data.size () > 0)
            {
                data.remove (0);
            }
            mytable.updateUI ();
        }
    }
    .mdb file attached
    NOW:
    my problem,
    the above code runs perfectly without the package myDB; at the top

    when i add the package piece it gives me this error:
    Java Code:
    //printing DRIVER
    jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb)};DBQ=E:\Documents and Settings\New user\My Documents\Database1.mdb
    //the actual error
    SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

    AGAIN i say, the program works perfectly without the package piece
    the 2 .java files and the .mdb file are all in a folder named myDB
    Attached Files Attached Files

Similar Threads

  1. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  2. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 11:52 AM
  3. Replies: 7
    Last Post: 11-09-2009, 07:51 AM
  4. Replies: 7
    Last Post: 10-20-2009, 10:34 AM
  5. Regarding package
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •