Hallo an Alle
Ich habe endlich ein Eclipse mit Steuerelemente auf die Paltte und benutze Swing Komponenten
ich habe auf ein JTabbedPane 3 Jtables und möchte sie mit Daten aus Datenbank ausfüllen jeweils mit einer anderen Tabelle (3 Tabellen aus Datenbank)
1. die Verbindung läuft gut
2. ich kriege die Daten auch aus dem Konsole Fenster
wie kann ich denn diese Daten in eine Jtable anzeigen lassen ?
Hier ist mein Code
package org.SwingTester.pkg;
public class SwinGTest extends JFrame {
private static void info(String nachricht){
System.out.println(nachricht);
}
private static void infohalten(String nachricht){
System.err.println(nachricht);
System.exit(99);
}
// @jve:decl-index=0:
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JTabbedPane jTabbedPane = null;
private JTable jTable = null;
private JTable jTable1 = null;
private JTable jTable2 = null;
private JButton jButton = null;
// private final String dbPath = "H:/JC/Qoute.mdb";
private final String dbPath = "H:/JC/DATENBANKFONDS.mdb";
private final String dbUser = ""; // @jve:decl-index=0:
private final String dbpasswd = ""; // @jve:decl-index=0:
// @jve:decl-index=0:
// @jve:decl-index=0:
/**
* This is the default constructor
*/
public SwinGTest() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(800, 400);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setFont(new Font("Dialog", Font.BOLD, 14));
jContentPane.setBackground(new Color(204, 204, 204));
jContentPane.add(getJPanel(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(3, 47, 740, 175));
jPanel.setBackground(new Color(153, 255, 153));
jPanel.setBorder(BorderFactory.createTitledBorder(null, "Input Faktoren", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
jPanel.add(getJTabbedPane(), gridBagConstraints);
}
return jPanel;
}
/**
* This method initializes jTabbedPane
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Stammdaten (Fonds)", null, getJTable(), null);
jTabbedPane.addTab("Stammdaten Benchmarks", null, getJTable1(), null);
jTabbedPane.addTab("Stammdaten Währungen", null, getJTable2(), null);
}
return jTabbedPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
}
return jTable;
}
/**
* This method initializes jTable1
*
* @return javax.swing.JTable
*/
private JTable getJTable1() {
if (jTable1 == null) {
jTable1 = new JTable();
}
return jTable1;
}
/**
* This method initializes jTable2
*
* @return javax.swing.JTable
*/
private JTable getJTable2() {
if (jTable2 == null) {
jTable2 = new JTable();
}
return jTable2;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(8, 229, 91, 109));
jButton.setIcon(new ImageIcon("H:/JC/logo/logo.jpg"));
jButton.setText("Connection");
jButton.setBackground(new Color(204, 204, 255));
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
connect();
}
});
}
return jButton;
}
public void connect()
{
Connection con = null;
ResultSet rs = null;
String StrSql = "";
final String dbDriverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
try {
Thread
.currentThread()
.getContextClassLoader()
.loadClass(dbDriverClass)
.newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info("Verbindung zu der Datenbank");
try {
final String dbURL =
"jdbcdbc: DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbPath;
con = DriverManager.getConnection(dbURL,dbUser,dbpasswd);
} catch (SQLException e) {
infohalten("Verbindung zu der Datenbank unmöglich");
}
info("Sql : Abfrage ");
StrSql = "select * from Stammdaten";
try {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(StrSql);
} catch (SQLException e) {
infohalten("Problem aufgetaucht die Abfrage kann nicht ausgeführt werden");
}
info("Daten ducrhlaufen");
ResultSetMetaData rsmd = null;
int clmCnt=-1;
try {
rsmd = rs.getMetaData();
clmCnt = rsmd.getColumnCount();
boolean encore = rs.next();
while (encore) {
for (int i = 1; i <= clmCnt; i++)
System.out.print(rs.getString(i) + " ");
System.out.println();
encore = rs.next();
}
rs.close();
} catch (SQLException e) {
infohalten(e.getMessage());
}
info("Ende des Programms");
System.exit(0);
}
}
Ich habe endlich ein Eclipse mit Steuerelemente auf die Paltte und benutze Swing Komponenten
ich habe auf ein JTabbedPane 3 Jtables und möchte sie mit Daten aus Datenbank ausfüllen jeweils mit einer anderen Tabelle (3 Tabellen aus Datenbank)
1. die Verbindung läuft gut
2. ich kriege die Daten auch aus dem Konsole Fenster
wie kann ich denn diese Daten in eine Jtable anzeigen lassen ?
Hier ist mein Code
package org.SwingTester.pkg;
public class SwinGTest extends JFrame {
private static void info(String nachricht){
System.out.println(nachricht);
}
private static void infohalten(String nachricht){
System.err.println(nachricht);
System.exit(99);
}
// @jve:decl-index=0:
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JTabbedPane jTabbedPane = null;
private JTable jTable = null;
private JTable jTable1 = null;
private JTable jTable2 = null;
private JButton jButton = null;
// private final String dbPath = "H:/JC/Qoute.mdb";
private final String dbPath = "H:/JC/DATENBANKFONDS.mdb";
private final String dbUser = ""; // @jve:decl-index=0:
private final String dbpasswd = ""; // @jve:decl-index=0:
// @jve:decl-index=0:
// @jve:decl-index=0:
/**
* This is the default constructor
*/
public SwinGTest() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(800, 400);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.setFont(new Font("Dialog", Font.BOLD, 14));
jContentPane.setBackground(new Color(204, 204, 204));
jContentPane.add(getJPanel(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(3, 47, 740, 175));
jPanel.setBackground(new Color(153, 255, 153));
jPanel.setBorder(BorderFactory.createTitledBorder(null, "Input Faktoren", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
jPanel.add(getJTabbedPane(), gridBagConstraints);
}
return jPanel;
}
/**
* This method initializes jTabbedPane
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Stammdaten (Fonds)", null, getJTable(), null);
jTabbedPane.addTab("Stammdaten Benchmarks", null, getJTable1(), null);
jTabbedPane.addTab("Stammdaten Währungen", null, getJTable2(), null);
}
return jTabbedPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
}
return jTable;
}
/**
* This method initializes jTable1
*
* @return javax.swing.JTable
*/
private JTable getJTable1() {
if (jTable1 == null) {
jTable1 = new JTable();
}
return jTable1;
}
/**
* This method initializes jTable2
*
* @return javax.swing.JTable
*/
private JTable getJTable2() {
if (jTable2 == null) {
jTable2 = new JTable();
}
return jTable2;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(8, 229, 91, 109));
jButton.setIcon(new ImageIcon("H:/JC/logo/logo.jpg"));
jButton.setText("Connection");
jButton.setBackground(new Color(204, 204, 255));
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
connect();
}
});
}
return jButton;
}
public void connect()
{
Connection con = null;
ResultSet rs = null;
String StrSql = "";
final String dbDriverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
try {
Thread
.currentThread()
.getContextClassLoader()
.loadClass(dbDriverClass)
.newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info("Verbindung zu der Datenbank");
try {
final String dbURL =
"jdbcdbc: DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbPath;
con = DriverManager.getConnection(dbURL,dbUser,dbpasswd);
} catch (SQLException e) {
infohalten("Verbindung zu der Datenbank unmöglich");
}
info("Sql : Abfrage ");
StrSql = "select * from Stammdaten";
try {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(StrSql);
} catch (SQLException e) {
infohalten("Problem aufgetaucht die Abfrage kann nicht ausgeführt werden");
}
info("Daten ducrhlaufen");
ResultSetMetaData rsmd = null;
int clmCnt=-1;
try {
rsmd = rs.getMetaData();
clmCnt = rsmd.getColumnCount();
boolean encore = rs.next();
while (encore) {
for (int i = 1; i <= clmCnt; i++)
System.out.print(rs.getString(i) + " ");
System.out.println();
encore = rs.next();
}
rs.close();
} catch (SQLException e) {
infohalten(e.getMessage());
}
info("Ende des Programms");
System.exit(0);
}
}