Hallo an Alle Meine Programm Fkt aber ich möchte die Daten in eine JTable ausfüllen
Ich habe auch dazu ein Tabelle Model schon vorbereitet : wie kann ich nur die Daten in Der JTable ausfüllen?
Hier ist der Code
[. CODE]
/*
* JTableDemo.java
*
* Created on 26. September 2007, 12:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package demo2;
import java.sql.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableDemo {
private static final String dbDriverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
static {
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();
}
}
/** Creates a new instance of JTableDemo */
public JTableDemo() {
}
/**
* @param args the command line arguments
*/
private static void zeigen(String message) {
System.out.println(message);
}
private static void Aufhoeren(String message) {
System.err.println(message);
System.exit(99);
}
public static void main(String[] args) {
final String dbPath ="H:/JC/DATENBANKFONDS.mdb";
//private final String dbUser = "Administrator";
String dbUser = "";
String dbpasswd = "";
Connection con = null;
ResultSet rset = null;
String Strsql = "";
try {
String DBurl = "jdbcdbc: DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbPath;
con = DriverManager.getConnection(DBurl);
con = DriverManager.getConnection(DBurl, dbUser, dbpasswd);
} catch (SQLException e) {
Aufhoeren("Verbindung zur Datenbankl unmöglich");
}
Strsql="select * from Stammdaten";
try {
Statement stmt = con.createStatement();
rset = stmt.executeQuery(Strsql);
} catch (SQLException e) {
Aufhoeren("Achtung die Abfrage kann nicht ausgeführt werden");
}
try {
ResultSetMetaData rsmd = rset.getMetaData();
int nbCols = rsmd.getColumnCount();
boolean encore = rset.next();
while (encore) {
for (int i = 1; i <= nbCols; i++)
System.out.print( rset.getString(i) + " ");
System.out.println();
encore = rset.next();
}
rset.close();
} catch (SQLException e) {
Aufhoeren(e.getMessage());
}
// Hier ich möchte nur die Daten Hier aüsfüllen bitte
String[] titles = new String[]{ "A", "B", "C", "D" ,"E", "F", "G", "H","I", "J", "K", "L","M", "N", "O", "P"};
final DefaultTableModel model = new DefaultTableModel( titles, 0 );
JTable table = new JTable( model );
JFrame frame = new JFrame( "Demo" );
Container content = frame.getContentPane();
content.add( new JScrollPane( table ), BorderLayout.CENTER );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
[/ CODE]
Vielen Danke
Ich habe auch dazu ein Tabelle Model schon vorbereitet : wie kann ich nur die Daten in Der JTable ausfüllen?
Hier ist der Code
[. CODE]
/*
* JTableDemo.java
*
* Created on 26. September 2007, 12:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package demo2;
import java.sql.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableDemo {
private static final String dbDriverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
static {
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();
}
}
/** Creates a new instance of JTableDemo */
public JTableDemo() {
}
/**
* @param args the command line arguments
*/
private static void zeigen(String message) {
System.out.println(message);
}
private static void Aufhoeren(String message) {
System.err.println(message);
System.exit(99);
}
public static void main(String[] args) {
final String dbPath ="H:/JC/DATENBANKFONDS.mdb";
//private final String dbUser = "Administrator";
String dbUser = "";
String dbpasswd = "";
Connection con = null;
ResultSet rset = null;
String Strsql = "";
try {
String DBurl = "jdbcdbc: DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbPath;
con = DriverManager.getConnection(DBurl);
con = DriverManager.getConnection(DBurl, dbUser, dbpasswd);
} catch (SQLException e) {
Aufhoeren("Verbindung zur Datenbankl unmöglich");
}
Strsql="select * from Stammdaten";
try {
Statement stmt = con.createStatement();
rset = stmt.executeQuery(Strsql);
} catch (SQLException e) {
Aufhoeren("Achtung die Abfrage kann nicht ausgeführt werden");
}
try {
ResultSetMetaData rsmd = rset.getMetaData();
int nbCols = rsmd.getColumnCount();
boolean encore = rset.next();
while (encore) {
for (int i = 1; i <= nbCols; i++)
System.out.print( rset.getString(i) + " ");
System.out.println();
encore = rset.next();
}
rset.close();
} catch (SQLException e) {
Aufhoeren(e.getMessage());
}
// Hier ich möchte nur die Daten Hier aüsfüllen bitte
String[] titles = new String[]{ "A", "B", "C", "D" ,"E", "F", "G", "H","I", "J", "K", "L","M", "N", "O", "P"};
final DefaultTableModel model = new DefaultTableModel( titles, 0 );
JTable table = new JTable( model );
JFrame frame = new JFrame( "Demo" );
Container content = frame.getContentPane();
content.add( new JScrollPane( table ), BorderLayout.CENTER );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
[/ CODE]
Vielen Danke