Results 1 to 3 of 3
Thread: javax.swing.table
- 02-01-2012, 02:29 PM #1
Senior Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 135
- Rep Power
- 0
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.
- 02-01-2012, 03:31 PM #2
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
Re: javax.swing.table
Use the "INSERT" or "DELETE" SQL statements to add or delete
- 02-01-2012, 09:33 PM #3
Senior Member
- Join Date
- Feb 2012
- Location
- Norway
- Posts
- 135
- Rep Power
- 0
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() ));
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; }
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); } } }
Similar Threads
-
package javax.swing does not exist
By timosoft in forum AWT / SwingReplies: 5Last Post: 02-15-2011, 02:17 AM -
javax.swing.RowSorter.SortKey
By new2java2009 in forum New To JavaReplies: 1Last Post: 04-11-2010, 04:26 AM -
download javax.swing.filechooser?
By spalax in forum New To JavaReplies: 5Last Post: 08-17-2009, 12:27 PM -
use of javax.swing.text.html
By newbieal in forum New To JavaReplies: 3Last Post: 10-09-2008, 07:45 PM -
map javax.swing.text.Element to javax.swing.text.View
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 08:02 PM
Bookmarks