Results 1 to 3 of 3
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default javax.swing.table

    Hi,

    I am a totally noob to Java programming, but I think I'm a fast learner.
    Gor introduced to this language by a friend. I have tried a few other, but starting to like this very much.

    I use Netbeans and have created at form containing a table, which get data from a Mysql database.
    Work very well, however I can't find how to enable adding og deleting database content from my table.

    I could really need som hints in the right direction.

    Thanks in advance.

  2. #2
    rremedios is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default Re: javax.swing.table

    Use the "INSERT" or "DELETE" SQL statements to add or delete

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: javax.swing.table

    I have these lines of code:
    Java Code:
            try{
                vectordata = LedgerTable.SelectAll();
            }catch(SQLException ex){}
            LedgerGrid.setModel(new javax.swing.table.DefaultTableModel(vectordata.getData(), vectordata.getColumns()
            ));
    I have a file called Mysql.Tables.LedgerTable which have the statements, but I am not sure how to call them in the table..

    Here is the lines with SelectAll:
    Java Code:
    public static VectorData SelectAll() throws SQLException
        {
            Connection connection = Connector.getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;     
            VectorData vectorData = null;
    
            String query = "SELECT * FROM LedgerTable ORDER BY Account";
            try {
                ps = connection.prepareStatement(query);
                rs = ps.executeQuery();
                ResultSetMetaData meta = rs.getMetaData();              
                int columnCount = meta.getColumnCount();
                int fechSize = rs.getFetchSize();
                Vector data = new Vector();
                Vector row;
                Vector columns = new Vector();
    
                for (int i = 1; i <= columnCount; i++) {
                    columns.add(meta.getColumnName(i));
                }
    
                while (rs.next()) {
                    row = new Vector(columnCount);
    
                        for (int i = 1; i <= columnCount; i++) {
                            Object object = rs.getObject(i);
                            if (object instanceof Integer) {
                                row.add(Integer.parseInt(object.toString()));
                            }else if (object instanceof Double) {
                                row.add(Double.parseDouble(object.toString()));
                            }else if (object instanceof String) {
                                row.add(object);
                            }else {
                                row.add(object);
                            }
                        }
                        data.add(row);
                    }
                    vectorData = new VectorData(data, columns);
                    return vectorData;                       
                
            } catch (SQLException ex) {
                Logger.getLogger(LedgerTable.class.getName()).log(Level.SEVERE, null, ex);           
            } finally {
                connection.close();
            }
            return vectorData;
        }
    Insert statement:
    Java Code:
    public static void insert(Ledger ledger)
        {
            Connection connection = Connector.getConnection();
            PreparedStatement ps = null;
    
            String query = "INSERT INTO LedgerTable(Account, Name, Type, Balance)"
                            +" VALUES(?,?,?,?)";
            
            try {
                
                ps = connection.prepareStatement(query);
                ps.setInt(1, ledger.getaccount());
                ps.setString(2, ledger.getaccountName());
                ps.setString(3, ledger.getaccountType());
                ps.setDouble(4, ledger.getbalance());               
                ps.executeUpdate();
                
            } catch (SQLException ex){            
                Logger.getLogger(LedgerTable.class.getName()).log(Level.SEVERE, null, ex);  
                
            } finally{
                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(LedgerTable.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    So, how could I call the Insert statement into my table?


Similar Threads

  1. package javax.swing does not exist
    By timosoft in forum AWT / Swing
    Replies: 5
    Last Post: 02-15-2011, 01:17 AM
  2. javax.swing.RowSorter.SortKey
    By new2java2009 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 03:26 AM
  3. download javax.swing.filechooser?
    By spalax in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 11:27 AM
  4. use of javax.swing.text.html
    By newbieal in forum New To Java
    Replies: 3
    Last Post: 10-09-2008, 06:45 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 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
  •