Hi,
ich versuche den Inhalt einer oracle Tabelle in einer JTable anzuzeigen.
Ich bekomme es hin das er mir den Inhalt grundsätzlich zeigt aber er macht mir für jede zeile in der DB eine eigene Tabelle.
Wie kann ich meine Code anpassen das ich das alles in einer einzigen JTable ausgebe?
:rtfm:
ich versuche den Inhalt einer oracle Tabelle in einer JTable anzuzeigen.
Ich bekomme es hin das er mir den Inhalt grundsätzlich zeigt aber er macht mir für jede zeile in der DB eine eigene Tabelle.
Wie kann ich meine Code anpassen das ich das alles in einer einzigen JTable ausgebe?
Java:
package GUI;
import db_input.Db_select;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
/**
*
* @author ich :)
*/
public class JTable_Ausgabe {
public static void main( String[] args ) throws Exception{
Connection conn = getConnection();
String tableName = "DMT_TESTTABLE1";
String tabelle = tabelle(conn, tableName);
try {
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM " + tableName); //tableName);
int count = 1;
while (rs.next() ) {
String s1 = rs.getString(1);
String s2 = rs.getString(1);
String s3 = rs.getString(1);
String s4 = rs.getString(1);
String s5 = rs.getString(1);
String s6 = rs.getString(1);
s1 = rs.getString("VID");
s2 = rs.getString("VALUE");
s3 = rs.getString("FILTER");
s4 = rs.getString("SHORTTEXT");
s5 = rs.getString("UNUSED");
s6 = rs.getString("ORDINAL");
System.out.println("Zeile: " + count);
System.out.println(s1 + " | " + s2 + "\n");
Vector data = new Vector();
Vector rowA = new Vector();
rowA.add( s1 );
rowA.add( s2 );
rowA.add( s3 );
rowA.add( s4 );
rowA.add( s5 );
rowA.add( s6 );
//
data.add( rowA );
// Die Titel für das Table
Vector title = new Vector();
title.add( "VID" );
title.add( "VALUE" );
title.add( "FILTER" );
title.add( "SHORTTEXT" );
title.add( "UNUSED" );
title.add( "ORDINAL" );
// Das JTable initialisieren
JTable table = new JTable( data, title );
JFrame frame = new JFrame( "Demo" );
frame.getContentPane().add( new JScrollPane( table ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
} catch (Exception e){}
}
// ###################### DB Connection ###############################################################
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@xxxxxxxxxxxx:1521:xxxxx"; // NISTEST1
String username = "xxxxx";
String password = "xxxxx";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
// ##################### Result ########################################################
String s1 = null;
public static String tabelle(Connection conn, String tableName) throws SQLException {
String resultTable = "";
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM " + tableName); //tableName);
while (rs.next() ) {
String s1 = rs.getString(1);
String s2 = rs.getString(2);
s1 = rs.getString("VID");
s2 = rs.getString("VALUE");
}
resultTable = rs.getString(resultTable);
} catch (SQLException e) {
} finally {
rs.close();
stmt.close();
}
return resultTable;
}
// #################### Count auf Tabelle #####################################################
public static int countRows(Connection conn, String tableName) throws SQLException {
// select the number of rows in the table
Statement stmt = null;
ResultSet rs = null;
int rowCount = -1;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT count(*) FROM " + tableName); //tableName);
// get the number of rows from the result set
rs.next();
rowCount = rs.getInt(1);
} finally {
rs.close();
stmt.close();
}
return rowCount;
}
}
:rtfm: