Models for Swing

Hello everybody.I need connect swing (Jtable and co.) with JDBC, using models (like TableModel).Where can i find libraries or frameworks with these models, or components for swing and jdbc)?Are there swing components that connect directed with jdbc?Thank you.
[294 byte] By [TechGSisa] at [2007-11-26 14:43:01]
# 1

I'm sure there are 3rd party package that you can buy that provide tight coupling with the database and a JTable, but I don't know of any.

Its not too hard to do it on your own. Here is a simple example to get you started:

import java.awt.*;

import java.io.*;

import java.sql.*;

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;

public class TableFromDatabase extends JFrame

{

public TableFromDatabase()

{

Vector columnNames = new Vector();

Vector data = new Vector();

try

{

// Connect to the Database

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

//String url = "jdbc:odbc:Teenergy"; // if using ODBC Data Source name

String url =

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/database.mdb";

String userid = "";

String password = "";

Class.forName( driver );

Connection connection = DriverManager.getConnection( url, userid, password );

// Read data from a table

String sql = "Select * from Activity";

Statement stmt = connection.createStatement();

ResultSet rs = stmt.executeQuery( sql );

ResultSetMetaData md = rs.getMetaData();

int columns = md.getColumnCount();

// Get column names

for (int i = 1; i <= columns; i++)

{

columnNames.addElement( md.getColumnName(i) );

}

// Get row data

while (rs.next())

{

Vector row = new Vector(columns);

for (int i = 1; i <= columns; i++)

{

row.addElement( rs.getObject(i) );

}

data.addElement( row );

}

rs.close();

stmt.close();

}

catch(Exception e)

{

System.out.println( e );

}

// Create table with database data

JTable table = new JTable(data, columnNames)

{

public Class getColumnClass(int column)

{

for (int row = 0; row < getRowCount(); row++)

{

Object o = getValueAt(row, column);

if (o != null)

{

return o.getClass();

}

}

return Object.class;

}

};

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

JPanel buttonPanel = new JPanel();

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

}

public static void main(String[] args)

{

TableFromDatabase frame = new TableFromDatabase();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

}

camickra at 2007-7-8 8:30:38 > top of Java-index,Desktop,Core GUI APIs...
# 2
Camickr,Could you update your example to close the connection?
cotton.ma at 2007-7-8 8:30:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Could you update your example to close the connection?

Not anymore, you just replied to my posting so I can't edit it :)

In keeping with the nature of the simple example I guess the code should be changed to:

rs.close();

stmt.close();

connection.close(); // just added

However, in a real application the proper way to do this would be to add a finally block and close all the resources there in case an exception was generated in the code.

camickra at 2007-7-8 8:30:38 > top of Java-index,Desktop,Core GUI APIs...
# 4

when i try to run the code below, i get this error

org.postgresql.util.PSQLException: The column index is out of range.

at org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData.getField(AbstractJdbc2ResultSetMetaData.java:454)

Hata Var org.postgresql.util.PSQLException: The column index is out of range.

at org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData.getColumnName(AbstractJdbc2ResultSetMetaData.java:245)

at guidevelop.adresler.jButton2MouseClicked(adresler.java:277)

at guidevelop.adresler.access$200(adresler.java:12)

at guidevelop.adresler$3.mouseClicked(adresler.java:154)

at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)

at java.awt.Component.processMouseEvent(Component.java:5491)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3901)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1778)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

and the code is like this :

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {

String user = "";

String pass = "";

String url = "jdbc:postgresql:test";

String query = "select * from test1;";

Vector columnNames = new Vector();

Vector data = new Vector();

try{

Class.forName("org.postgresql.Driver");

Connection con = DriverManager.getConnection(url,user,pass);

Statement state = con.createStatement();

ResultSet rs = state.executeQuery(query);

ResultSetMetaData md = rs.getMetaData();

int columns = md.getColumnCount();

//get column names

for(int i=0; i<= columns; i++) {

columnNames.addElement(md.getColumnName(i));

}

//get row data

while(rs.next()) {

Vector row = new Vector(columns);

for(int i=0; i<columns; i++) {

row.addElement(rs.getObject(i));

}

data.addElement(row);

}

rs.close();

state.close();

con.close();

}catch(Exception e) {

e.printStackTrace();

System.out.println("Hata Var " + e);

}

dbTable = new JTable(data, columnNames);

}

where might be the problem?

Message was edited by:

sheriffdaone>

sheriffdaonea at 2007-7-8 8:30:38 > top of Java-index,Desktop,Core GUI APIs...
# 5
Quit cluttering this thread. You have your own thread on this topic.Why did you change the demo code that works? Compare your code with the working code to see what you've changed.. Learn how to use the "Code Formatting" tags when posting code.
camickra at 2007-7-8 8:30:38 > top of Java-index,Desktop,Core GUI APIs...
# 6

Sorry but you send me to this thread to take a look and i did modify your code to mine, but sorry again i am not trying to clutter this thread.

But when i run your code it works well but i write the same thing under button clicked event. Then it doesn't work, could it be because of i already have JTable at Design-time?

Sorry again, just want to learn and examine the codes.

sheriffdaonea at 2007-7-8 8:30:38 > top of Java-index,Desktop,Core GUI APIs...