ResultSet ScrollPane problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lost1
    New Member
    • Oct 2007
    • 14

    ResultSet ScrollPane problem

    I am having trouble adding a result set to a scroll pane. no matter what I do using getText I get an error. Can someon give me a clue?

    Code:
    import java.util.*;
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.table.DefaultTableModel;
    
    public class MusicLibraryForm extends JFrame
    {
        //create music library frame
        private JFrame MusicLibrary = new JFrame();
    
        //for north panel in border layout
        private JLabel SearchLabel = new JLabel("Enter search information below", SwingConstants.CENTER);
        private JTextField SearchText = new JTextField();
        private JButton SearchButton = new JButton("Search");
        private JButton NextRecord = new JButton("Next");
        private JButton PreviousRecord = new JButton("Previous");
    
        //for center panel in border layout
        private JScrollPane SearchResult = new JScrollPane();
        private JTable ResultTable = new JTable();
        private DefaultTableModel TableModel = new DefaultTableModel();
    
        Border NotesBorder = new LineBorder(Color.GRAY, 1);
        Border SearchBorder = new LineBorder(Color.BLACK, 1);
    
        private MusicLibraryForm()
        {
            JPanel SearchInputPanel = new JPanel(new GridLayout(2, 1, 5, 5));
            //add search criteria panel and  search input panel to north panel
            SearchInputPanel.add(SearchLabel);
            SearchInputPanel.add(SearchText);
            SearchText.setBorder(SearchBorder);
    
            JPanel SearchButtonPanel = new JPanel(new BorderLayout(5, 5));
            SearchButtonPanel.add(SearchButton, BorderLayout.WEST);
            SearchButtonPanel.add(NextRecord, BorderLayout.EAST);
            SearchButtonPanel.add(PreviousRecord, BorderLayout.CENTER);
            
            //create complete search panel
            JPanel NorthPanel = new JPanel(new BorderLayout(5, 5));
            //add search criteria panel and search input panel to north panel
            NorthPanel.add(SearchInputPanel, BorderLayout.WEST);
            NorthPanel.add(SearchButtonPanel, BorderLayout.EAST);
            
            JPanel WestPanel = new JPanel(new GridLayout(1,1,5,5));
            ResultTable.setModel(TableModel);
            SearchResult.add(ResultTable);
            WestPanel.add(SearchResult, BorderLayout.WEST);
    
            //set frame layout
            setLayout(new BorderLayout(5,5));
            //add north, centereast and south panel to the frame 
            add(NorthPanel, BorderLayout.NORTH);
            add(WestPanel, BorderLayout.CENTER);
        }
    
        public static void main(String[] args)
        {
            MusicLibraryForm frame = new MusicLibraryForm();
            frame.setTitle("Music Library");
            frame.setSize(500, 400);
            frame.setLocation(50, 50);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        private void SearchTable()
        {
            Connection con = null;
                
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String source = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Music Library.mdb";
                con = DriverManager.getConnection(source);
    
                //create the statement
                Statement LibraryStatement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    
                //use the statement to select the data from the MusciLibraryTable.
                final ResultSet rs = LibraryStatement.executeQuery("SELECT * " + "FROM MusicLibraryTable");
            
                final String UserInput = SearchText.getText();
    
                SearchButton.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            boolean found = false;
                        try
                        {
                            //iterate over the ResultSet
                            while(rs.next())
                            {
                                /*I don't know exactly how to get the result set to show up here*/
                            }
                        }
                        catch (SQLException ex)
                        {
                            ex.printStackTrace();
                        }
                            if(!found)
                                System.out.println("information not found");
                        }
                    });
    
             //close the ResultSet
            rs.close();
            }
    
            catch (SQLException sqle)
            {System.err.println(sqle.getMessage());}
    
            catch (ClassNotFoundException cnfe)
            {System.err.println(cnfe.getMessage());}
    
            catch (Exception e)
            {System.err.println(e.getMessage());}
    
            finally
            {
                try
                {
                    if ( con != null )
                    {
                        //close the connection no matter what
                        con.close();
                    }
                }
                catch (SQLException sqle)
                {System.err.println(sqle.getMessage());}
            }
        }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    "Get an error" is too vague. Exactly what error?

    I would also generally suggest you break your code up into logical components. You shouldn't have any class that mentions both the GUI and a database. Try the following.

    1. Write a class the represents one row from that table. Call it CD (I'm guessing what your rows represent!)
    2. Write a class that can select the data from that table and create a list of CD objects. If you need more functionality than a java.util.List, you may want to design your own collection class, too.

    Stop and test your code -- can you read from the table and just dump out the results to System.out?

    3. Separately to that, design a GUI that can display CDs. Test it with a hard coded list of CD objects.

    When the GUI front end and the database back end components both pass their tests, you can combine them. The trick is to take small steps and test each one.

    Comment

    • lost1
      New Member
      • Oct 2007
      • 14

      #3
      unfortunately I'm under a little bit of a time crunch and this is the last part of this code and I'm stuck. I guess what I mean by error is that fact that I simply don't know how to get the result set to print in the scroll pane.

      Code:
                  final String UserInput = SearchText.getText();
       
                  SearchButton.addActionListener(new ActionListener()
                      {
                          public void actionPerformed(ActionEvent e)
                          {
                              boolean found = false;
                          try
                          {
                              //iterate over the ResultSet
                              while(rs.next())
                              {
                                  /*I don't know exactly how to get the result set to show up here*/
                              }
                          }
                          catch (SQLException ex)
                          {
                              ex.printStackTrace();
                          }
                              if(!found)
                                  System.out.println("information not found");
                          }
                      });
      Last edited by lost1; Dec 13 '07, 04:19 PM. Reason: added code that is focus of problem

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        I would still go with creating a class to represent a row from the database table.

        Comment

        • lost1
          New Member
          • Oct 2007
          • 14

          #5
          outside of my comment do you see anyhting wrong with the second block of code I posted? I don't have time to rewrite this, but if I cna't fix what I have I'll try.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by lost1
            outside of my comment do you see anyhting wrong with the second block of code I posted? I don't have time to rewrite this, but if I cna't fix what I have I'll try.
            First, I think you never call the method searchTable! That method needs to be divided into separate parts. You also haven't specified what your code is supposed to do and how you want to be able to interact with it.

            Comment

            Working...