import java.sql.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MyJTable extends JFrame {
private DefaultTableModel tblDataModel = null;
private JPanel jPanel1;
private JPanel jPanel2;
private JScrollPane jScrollPane1;
private JTable jTable1;
public MyJTable(){}
public MyJTable(ResultSet rs) {
initComponents();
showTableContent(rs);
}
private void showTableContent(ResultSet rs){
try{
tblDataModel = new DefaultTableModel();
Vector clmHeader = new Vector();
Vector dataVector = new Vector();
ResultSetMetaData rsmd = rs.getMetaData();
int clmCnt = rsmd.getColumnCount();
for(int i = 1; i <= clmCnt;i++)
clmHeader.addElement(rsmd.getColumnName(i));
while(rs.next()){
Vector rowVector = new Vector();
for(int i = 1; i <= clmCnt; i++){
rowVector.addElement(rs.getString(i));
}
dataVector.addElement(rowVector);
}
tblDataModel.setDataVector(dataVector,clmHeader);
this.jTable1.setModel(tblDataModel);
this.jTable1.updateUI();
System.out.println("finished");
}catch(SQLException sqle){
sqle.printStackTrace();
}
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jPanel1.setLayout(new java.awt.GridBagLayout());
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jPanel1.setMinimumSize(new java.awt.Dimension(640, 500));
jPanel1.setPreferredSize(new java.awt.Dimension(640, 500));
jPanel2.setLayout(new java.awt.GridBagLayout());
jPanel2.setBackground(new java.awt.Color(153, 204, 255));
jPanel2.setMinimumSize(new java.awt.Dimension(640, 300));
jPanel2.setPreferredSize(new java.awt.Dimension(640, 300));
jScrollPane1.setMinimumSize(new java.awt.Dimension(640, 300));
jScrollPane1.setPreferredSize(new java.awt.Dimension(640, 300));
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
jScrollPane1.setViewportView(jTable1);
jPanel2.add(jScrollPane1, new java.awt.GridBagConstraints());
jPanel1.add(jPanel2, new java.awt.GridBagConstraints());
pack();
}
}
Und von da will ich es generieren lassen und in den Ergebnis-Tab anzeigen:
package zumposten;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
/* Used by MainFrame.java. */
public class AbfrageFrame extends JInternalFrame {
private JTabbedPane tabbedPane;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
private JTextField tfBegr = null;
private JLabel jlCb = null;
private JCheckBox cbUser = null;
private JLabel jlKBegrenzung = null;
private JButton jbStart = null;
private JButton jbStart1 = null;
public AbfrageFrame() {
super("Document #" + (++openFrameCount), true,
true,
true,
true);
initialize();
AbfragePane();
setSize(300, 300);
setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
}
private void initialize() {
this.setBackground(SystemColor.controlShadow);
}
public void AbfragePane() {
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
createPage1();
createPage2();
createPage3();
tabbedPane = new JTabbedPane();
tabbedPane.addTab("P suchen:", panel1);
tabbedPane.addTab("H suchen:", panel2);
tabbedPane.addTab("Ergebnis:", panel3);
topPanel.add(tabbedPane, BorderLayout.CENTER);
topPanel.setBounds(new Rectangle(100, 100, 400, 300));
}
public void createPage1() {
jlKBegrenzung = new JLabel();
jlKBegrenzung.setBounds(new Rectangle(91, 27, 115, 20));
jlKBegrenzung.setText("Begrenzung");
jlCb = new JLabel();
jlCb.setBounds(new Rectangle(39, 4, 110, 19));
jlCb.setText("alle User Anzeigen");
panel1 = new JPanel();
panel1.setLayout(null);
panel1.setPreferredSize(new Dimension(30, 30));
panel1.add(getTfBegr(), null);
panel1.add(jlCb, null);
panel1.add(getCbUser(), null);
panel1.add(jlKBegrenzung, null);
panel1.add(getJbStart1(), null);
}
public void createPage2() {
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(new JButton("North"), BorderLayout.NORTH);
panel2.add(new JButton("South"), BorderLayout.SOUTH);
panel2.add(new JButton("East"), BorderLayout.EAST);
panel2.add(new JButton("West"), BorderLayout.WEST);
panel2.add(new JButton("Center"), BorderLayout.CENTER);
}
public void createPage3() {
panel3 = new JPanel();
panel3.setLayout(new GridLayout(3, 2));
}
private JTextField getTfBegr() {
if (tfBegr == null) {
tfBegr = new JTextField();
tfBegr.setBounds(new Rectangle(13, 26, 71, 19));
}
return tfBegr;
}
private JCheckBox getCbUser() {
if (cbUser == null) {
cbUser = new JCheckBox();
cbUser.setBounds(new Rectangle(12, 5, 21, 18));
}
return cbUser;
}
private JButton getJbStart1() {
if (jbStart1 == null) {
jbStart1 = new JButton();
jbStart1.setBounds(new Rectangle(86, 53, 122, 26));
jbStart1.setText("Start!");
jbStart1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
abfrageStarten();
}
private void abfrageStarten() {
System.out.println("Tst " + tfBegr.getText());
System.out.println("Start");
String url = "jdbc:odbc:database";
java.sql.Connection con;
String query;
if (getCbUser().isSelected()) {
query = "SELECT * FROM [Table]";
} else {
query = "SELECT * FROM [Table] WHERE [User NR] like "
+ "\'" + tfBegr.getText() + "\'";
}
System.out.println("Treiber laden");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
System.out.println("getConnection()");
con = DriverManager.getConnection(url, "", "");
System.out.println("executeQuery");
Statement stmt = con.createStatement();
System.out.println(""+ query);
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int rowCount = 1;
MyJTable tabelle = new MyJTable(rs);
while (rs.next()) {
System.out.println("Row " + rowCount + ": ");
for (int i = 1; i <= numberOfColumns; i++) {
System.out.print(" Column " + i + ": ");
System.out.println(rs.getString(i));
}
System.out.println("");
rowCount++;
}
con.close();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
});
}
return jbStart1;
}
}