Hallo allerseits,
wirklich starkes Forum habt ihr hier!
bisher reichte mir das mitlesen, aber nun "muss" ich nun auch mal aktiv nachhacken..
Mein Problem liegt darin, dass ich nicht so recht mit abstract table models klar komme.
Ich habe eine Tabellenklasse und eine AbstractModelClass.
|
Java Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package gfx.client;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.table.AbstractTableModel;
public class mymodel extends AbstractTableModel {
//Initiierung div. datenbankspezifischer Varbiablen
static final String LOG_FILE = "jdbc.log";
static final String USER = "root";
static final String PW = "root";
static final String URL = "jdbc:mysql://localhost/db";
String sql = "select * from tb1";
MysqlDataSource mds = new MysqlDataSource();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
public int getColumnCount() {...}
public int getRowCount() {...}
public Object getValueAt( int row, int col) {...}
public String getColumnName(int c){...}
public boolean isCellEditable( int row, int col) {...}
}
|
Leider weiß ich absolut nicht, wie ich die o.g. Methoden modifizieren soll, damit mein Resultset in der Tabelle dargestellt wird.
Bei meiner Googlearie bin ich auf folgendes Bsp. gestoßen, was aber nicht mit einem tablemodell arbeitet:
|
Java Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package gfx.client;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableDB extends JInternalFrame
{
public static String sql_preview ;
public TableDB( String sql_preview){
Vector columnNames = new Vector();
Vector data = new Vector();
try
{
// Connect to the Database
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:mysql://localhost/hotel";
String userid = "root";
String password = "root";
Class.forName( driver );
Connection connection = DriverManager.getConnection( url, userid, password );
// Read data from a table
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql_preview );
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)
{
return getValueAt(0, column).getClass();
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
TableDB frame = new TableDB( sql_preview );
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
private void jbInit() throws Exception {
}
}
|
Leider weiß ich nicht so recht, wie ich weiter vorgehen soll. Das "How to use JTable" von Sun hat mir auch nicht weiter geholfen, bzw. habe ich dazu weder ein gutes Tut, noch ein nettes Beispiel gefunden.
Hoffe, dass mir hier geholfen werden kann.
Juanito