Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: DB application error, who has a clue what it is about?

  1. #1
    Member
    Join Date
    Jun 2013
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default DB application error, who has a clue what it is about?

    I am studying some Java DB applications of the JavaCore vol II book.
    Running a large sample application of that book gives my the following error:

    Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: learningdatabase.DataPanel.<init>(Ljavax/sql/RowSetV
    at learningdatabase.ViewDBFrame.showTable(ViewDB.java :154)
    at learningdatabase.ViewDBFrame$1.actionPerformed(Vie wDB.java:54)

    Who can give me a clue what this error is about?
    Next see the class attached to concerning error number (it is an independent class at the same file).

    154 dataPanel = new DataPanel(crs);

    class DataPanel extends JPanel
    {
       /**
        * Constructs the data panel.
        * @param rs the result set whose contents this panel displays
        */
       public DataPanel(RowSet rs) throws SQLException
       {
          fields = new ArrayList<JTextField>();
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
     
          ResultSetMetaData rsmd = rs.getMetaData();
          for (int i = 1; i <= rsmd.getColumnCount(); i++)
          {
             gbc.gridy = i - 1;
     
             String columnName = rsmd.getColumnLabel(i);
             gbc.gridx = 0;
             gbc.anchor = GridBagConstraints.EAST;
             add(new JLabel(columnName), gbc);
     
             int columnWidth = rsmd.getColumnDisplaySize(i);
             JTextField tb = new JTextField(columnWidth);
             if (!rsmd.getColumnClassName(i).equals("java.lang.String"))
                tb.setEditable(false);
     
             fields.add(tb);
     
             gbc.gridx = 1;
             gbc.anchor = GridBagConstraints.WEST;
             add(tb, gbc);
          }
       }
     
       /**
        * Shows a database row by populating all text fields with the column values.
        */
       public void showRow(ResultSet rs) throws SQLException
       {
          for (int i = 1; i <= fields.size(); i++)
          {
             String field = rs.getString(i);
             JTextField tb = (JTextField) fields.get(i - 1);
             tb.setText(field);
          }
       }
     
       /**
        * Updates changed data into the current row of the row set
        */
       public void setRow(RowSet rs) throws SQLException
       {
          for (int i = 1; i <= fields.size(); i++)
          {
             String field = rs.getString(i);
             JTextField tb = (JTextField) fields.get(i - 1);
             if (!field.equals(tb.getText()))
                rs.updateString(i, tb.getText());
          }
          rs.updateRow();
       }
     
       private ArrayList<JTextField> fields;
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: DB application error, who has a clue what it is about?

    Hello.
    Please provide your source code for ViewDB.java.
    I think the forum admins should disable emoticons in the messages.
    As we see in the above problem, some text got replaced by emoticons. This could be irritating.

    Syed.

  3. #3
    Member
    Join Date
    Jun 2013
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: DB application error, who has a clue what it is about?

    I found a correct working code on the net, here it is (a previous post with a link of that code dis appeared....
    eaten by that funny face?)


    import com.sun.rowset.*;
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import javax.sql.*;
    import javax.sql.rowset.*;
     
    /**
     * This program uses metadata to display arbitrary tables in a database.
     * @version 1.31 2007-06-28
     * @author Cay Horstmann
     */
    public class ViewDB
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(new Runnable()
             {
                public void run()
                {
                   JFrame frame = new ViewDBFrame();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setVisible(true);
                }
             });
       }
    }
     
    /**
     * The frame that holds the data panel and the navigation buttons.
     */
    class ViewDBFrame extends JFrame
    {
       public ViewDBFrame()
       {
          setTitle("ViewDB");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
     
          tableNames = new JComboBox();
          tableNames.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   showTable((String) tableNames.getSelectedItem());
                }
             });
          add(tableNames, BorderLayout.NORTH);
     
          try
          {
             readDatabaseProperties();
             Connection conn = getConnection();
             try
             {
                DatabaseMetaData meta = conn.getMetaData();
                ResultSet mrs = meta.getTables(null, null, null, new String[] { "TABLE" });
                while (mrs.next())
                   tableNames.addItem(mrs.getString(3));
             }
             finally
             {
                conn.close();
             }
          }
          catch (SQLException e)
          {
             JOptionPane.showMessageDialog(this, e);
          }
          catch (IOException e)
          {
             JOptionPane.showMessageDialog(this, e);
          }
     
          JPanel buttonPanel = new JPanel();
          add(buttonPanel, BorderLayout.SOUTH);
     
          previousButton = new JButton("Previous");
          previousButton.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   showPreviousRow();
                }
             });
          buttonPanel.add(previousButton);
     
          nextButton = new JButton("Next");
          nextButton.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   showNextRow();
                }
             });
          buttonPanel.add(nextButton);
     
          deleteButton = new JButton("Delete");
          deleteButton.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   deleteRow();
                }
             });
          buttonPanel.add(deleteButton);
     
          saveButton = new JButton("Save");
          saveButton.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   saveChanges();
                }
             });
          buttonPanel.add(saveButton);
       }
     
       /**
        * Prepares the text fields for showing a new table, and shows the first row.
        * @param tableName the name of the table to display
        */
       public void showTable(String tableName)
       {
          try
          {
             // open connection
             Connection conn = getConnection();
             try
             {
                // get result set
                Statement stat = conn.createStatement();
                ResultSet result = stat.executeQuery("SELECT * FROM " + tableName);
                // copy into cached row set
                crs = new CachedRowSetImpl();
                crs.setTableName(tableName);
                crs.populate(result);
             }
             finally
             {
                conn.close();
             }
     
             if (scrollPane != null) remove(scrollPane);
             dataPanel = new DataPanel(crs);
             scrollPane = new JScrollPane(dataPanel);
             add(scrollPane, BorderLayout.CENTER);
             validate();
             showNextRow();
          }
          catch (SQLException e)
          {
             JOptionPane.showMessageDialog(this, e);
          }
       }
     
       /**
        * Moves to the previous table row.
        */
       public void showPreviousRow()
       {
          try
          {
             if (crs == null || crs.isFirst()) return;
             crs.previous();
             dataPanel.showRow(crs);
          }
          catch (SQLException e)
          {
             for (Throwable t : e)
                t.printStackTrace();
          }
       }
     
       /**
        * Moves to the next table row.
        */
       public void showNextRow()
       {
          try
          {
             if (crs == null || crs.isLast()) return;
             crs.next();
             dataPanel.showRow(crs);
          }
          catch (SQLException e)
          {
             JOptionPane.showMessageDialog(this, e);
          }
       }
     
       /**
        * Deletes current table row.
        */
       public void deleteRow()
       {
          try
          {
             Connection conn = getConnection();
             try
             {
                crs.deleteRow();
                crs.acceptChanges(conn);
                if (!crs.isLast()) crs.next();
                else if (!crs.isFirst()) crs.previous();
                else crs = null;
                dataPanel.showRow(crs);
             }
             finally
             {
                conn.close();
             }
          }
          catch (SQLException e)
          {
             JOptionPane.showMessageDialog(this, e);
          }
       }
     
       /**
        * Saves all changes.
        */
       public void saveChanges()
       {
          try
          {
             Connection conn = getConnection();
             try
             {
                dataPanel.setRow(crs);
                crs.acceptChanges(conn);
             }
             finally
             {
                conn.close();
             }
          }
          catch (SQLException e)
          {
             JOptionPane.showMessageDialog(this, e);
          }
       }
     
       private void readDatabaseProperties() throws IOException
       {
          props = new Properties();
          FileInputStream in = new FileInputStream("database.properties");
          props.load(in);
          in.close();
          String drivers = props.getProperty("jdbc.drivers");
          if (drivers != null) System.setProperty("jdbc.drivers", drivers);
       }
     
       /**
        * Gets a connection from the properties specified in the file database.properties
        * @return the database connection
        */
       private Connection getConnection() throws SQLException
       {
          String url = props.getProperty("jdbc.url");
          String username = props.getProperty("jdbc.username");
          String password = props.getProperty("jdbc.password");
     
          return DriverManager.getConnection(url, username, password);
       }
     
       public static final int DEFAULT_WIDTH = 400;
       public static final int DEFAULT_HEIGHT = 200;
     
       private JButton previousButton;
       private JButton nextButton;
       private JButton deleteButton;
       private JButton saveButton;
       private DataPanel dataPanel;
       private Component scrollPane;
       private JComboBox tableNames;
       private Properties props;
       private CachedRowSet crs;
    }
     
    /**
     * This panel displays the contents of a result set.
     */
    class DataPanel extends JPanel
    {
       /**
        * Constructs the data panel.
        * @param rs the result set whose contents this panel displays
        */
       public DataPanel(RowSet rs) throws SQLException
       {
          fields = new ArrayList<JTextField>();
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
     
          ResultSetMetaData rsmd = rs.getMetaData();
          for (int i = 1; i <= rsmd.getColumnCount(); i++)
          {
             gbc.gridy = i - 1;
     
             String columnName = rsmd.getColumnLabel(i);
             gbc.gridx = 0;
             gbc.anchor = GridBagConstraints.EAST;
             add(new JLabel(columnName), gbc);
     
             int columnWidth = rsmd.getColumnDisplaySize(i);
             JTextField tb = new JTextField(columnWidth);
             if (!rsmd.getColumnClassName(i).equals("java.lang.String"))
                tb.setEditable(false);
     
             fields.add(tb);
     
             gbc.gridx = 1;
             gbc.anchor = GridBagConstraints.WEST;
             add(tb, gbc);
          }
       }
     
       /**
        * Shows a database row by populating all text fields with the column values.
        */
       public void showRow(ResultSet rs) throws SQLException
       {
          for (int i = 1; i <= fields.size(); i++)
          {
             String field = rs.getString(i);
             JTextField tb = (JTextField) fields.get(i - 1);
             tb.setText(field);
          }
       }
     
       /**
        * Updates changed data into the current row of the row set
        */
       public void setRow(RowSet rs) throws SQLException
       {
          for (int i = 1; i <= fields.size(); i++)
          {
             String field = rs.getString(i);
             JTextField tb = (JTextField) fields.get(i - 1);
             if (!field.equals(tb.getText()))
                rs.updateString(i, tb.getText());
          }
          rs.updateRow();
       }
     
       private ArrayList<JTextField> fields;
    }

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: DB application error, who has a clue what it is about?

    Hello.
    Your code is fine. I just executed it on my machine by creating the required environment (database.properties file, jdbc jar, etc).
    I didn't get the exception which you had.
    So run your code again and let me know.
    By the way, there is one exception I got. That has to do with AutoCommit. Thats different.
    Let me know if you still had the previous exception. If yes, write briefly the steps you have performed during runtime to get that exception.

    Syed.

  5. #5
    Member
    Join Date
    Jun 2013
    Posts
    61
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: DB application error, who has a clue what it is about?

    Yes that code is working and I do not know what happened with the previous code error.
    I did some research and found this (maybe it had something to do with the error):

    The problem is that the reference implementation of CachedRowSet (com.sun.rowset.CachedRowSetImpl) contains a bug: When you retrieve a column by name, it uses the columnName, and not the columnLabel, therefor going against the rest of the JDBC specification which uses the columnLabel to retrieve values. This bug makes it impossible to retrieve values from the rowset by the columnLabel.

    The bug at Oracle is Bug Database, but (surprise, surprise) they have made it unavailable for public viewing.

    There are two potential workaround. One is to check if your driver provides a property to also have the ResultSetMetaData.getColumnName(..) method return the columnLabel value, the second workaround would be to create a subclass of CachedRowSetImpl (which unfortunately requires a lot of overriding methods).

    The version below is copied from this message: Yahoo! Groups

Similar Threads

  1. Syntax error with simple application.
    By THE ULTIMATE CYBER MACHIN in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2013, 05:04 AM
  2. Give me some clue for my project.
    By maronski in forum Java Theory & Questions
    Replies: 10
    Last Post: January 19th, 2013, 03:02 PM
  3. [SOLVED] I have no clue what is wrong
    By Java_noob333 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 23rd, 2012, 12:37 AM
  4. chat application login error
    By felixnoriel in forum Java Networking
    Replies: 4
    Last Post: January 4th, 2012, 01:38 PM
  5. Swing Dialog Boxes --- Have no clue why its not working
    By jap2008 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2010, 04:20 PM