|
You last visited: Today at 13:25
Advertisement
Resultset in DefaultTable anzeigen lassen
Discussion on Resultset in DefaultTable anzeigen lassen within the Java forum part of the Coders Den category.
11/18/2015, 22:48
|
#1
|
elite*gold: 0
Join Date: Apr 2012
Posts: 55
Received Thanks: 3
|
Resultset in DefaultTable anzeigen lassen
Hallooo soo ich habe jetzt einige Stunden damit zugebracht rauszufinden wie ich ein ResultSet in einer Defaulttable anzeigen lassen kann. Da ich im internet aber biss jetzt nix wirklich brauchbares gefunden habe wollt ich hier mal nachfragen ob es hier ein paar Tipps und Anregungen gibt :3
Soo leute habe jetzt mal was ausprobiert :
Das hier ist die Methode mit der ich daten einfügen will Testweise
Code:
public ResultSet ausfuehren(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
DefaultTableModel tableModel = new DefaultTableModel( );
String entry = null;
String item = null;
tableModel.addColumn( "Entry" );
tableModel.addColumn( "Item(ID)" );
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM npc_vendor");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM npc_vendor")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3));
// Now do something with the ResultSet ....
entry = rs.getString(1);
item = rs.getString(3);
int i;
for(i=0;i<3;i++){
Vector vector = new Vector();
vector.add(entry);
vector.add(item);
tableModel.addRow(vector);
}
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
Und das hier ist der teil in der Gui zum ausführen
Code:
conn.ausfuehren();
itemlist.setModel( tableModel );
Vom Gefühl müsste das funktionieren nur : anstatt nur die erste und dritte Spalte der Tabelle aus dem ResultSet rauszunehmen und in die JTable einzufügen nimmt er die ersten 3 und schmeißt mir die Exception Das er keine NULL werte einfügen kann raus..... was nu ?
edit: habe mal in der abfrage nur nach den spalten gefragt die ich auch haben will. dennoch das problem : Habe ich einen Fehler beim Transportieren oder beim richtigen auslesen des ressultsets ? :O
|
|
|
11/19/2015, 08:51
|
#2
|
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,226
|
Der Cursor beginnt bei 1, deine for-Schleife bei 0. Du solltest aufpassen, den in einer Datenbank der Index immer bei 1 beginnt, und nicht wie in Java und anderen Programmiersprachen wie üblich ab 0.
Du kannst es auf 2 Arten machen, die eine die du angesprochen hast, die andere wär, AbstractTableModel zu überschreiben, um halt eine eigene Table zu haben, die sich um das ganze kümmert.
So bleibst du dynamisch.
Code:
ResultSet rs = cl.execute(SQL_QUERY);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] column = new String[columnCount];
int rows = 0;
while(rs.next()) {
rows++;
}
for(int i = 1; i <= columnCount; i++) {
column[i - 1] = rsmd.getColumnName(i);
}
Object[][] erg = new Object[rows][columnCount];
rows = 0;
rs.beforeFirst();
while(rs.next()) {
for(int i = 1; i <= columnCount; i++) {
erg[rows][i - 1] = rs.getString(i);
}
rows++;
}
model = new DefaultTableModel(erg, column);
table.setModel(model);
rs.close();
rs = null;
Willst du aber mit einer eigenen Tabelle arbeiten(um sich sehr viel zu ersparren!), dann schaue dir AbstractTableModel an.
Aber ich zeige dir ein Beispiel, vielleicht findet man es nicht soo oft im Internet:
ResultSetTableModel
Code:
/**
* Our own ResultSetTableModel class. Our Table model works directly with the ResultSet object, which provides easy use and work
* with the JTable.
*
* @author Stefan Stokic / XxharCs
* @version 2.0
*/
public class ResultSetTableModel extends AbstractTableModel {
protected ResultSet rs;
protected int rowCount;
/**
* Constructor which takes the ResultSet object.
*
* @param rs the ResultSet object
* @throws SQLException if, when moving the cursor it fails
*/
public ResultSetTableModel(ResultSet rs) throws SQLException {
this.rs = rs;
rs.afterLast();
rs.previous();
this.rowCount = rs.getRow();
}
/**
* Returns the column count out of the meta data from the ResultSet object.
*/
@Override
public int getColumnCount() {
try {
return rs.getMetaData().getColumnCount();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the row count.
*/
@Override
public int getRowCount() {
return this.rowCount;
}
/**
* Returns the value at the specified row and columnIndex.
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
return rs.getObject(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Sets the specified value at the specified row and columnIndex.
*/
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
rs.updateObject(columnIndex + 1, aValue);
rs.updateRow();
} catch (SQLException e) {
//throw new RuntimeException(e);
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
/**
* Returns the column class of the specified columnIndex.
*/
@Override
public Class<?> getColumnClass(int columnIndex) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
String typeName = rsmd.getColumnClassName(columnIndex + 1);
return Class.forName(typeName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the column name of the specified column.
*/
@Override
public String getColumnName(int column) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
String columnName = rsmd.getColumnName(column + 1);
return columnName;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns if the cell is editable at the specified row and columnIndex.
*/
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
return rsmd.isWritable(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Notifies the table that it's data has changed cause of the search query. The search queries return is in the ResultSet object,
* so it's only needed to set the new ResultSet to change the data of the table model.
*
* @param rs the ResultSet object which contains the return of the search query
* @throws SQLException if the search query fails
*/
public void search(ResultSet rs) throws SQLException {
this.rs = rs;
rs.afterLast();
rs.previous();
this.rowCount = rs.getRow();
this.fireTableDataChanged();
}
}
In der Verwendung:
Code:
protected ResultSetTableModel defTableModel;
protected ConnectionLogic conLogic;
...
...
conLogic = new ConnectionLogic(this.hostname, this.username, this.password, this.dbName);
ResultSet rs = conLogic.execute(SQL_QUERY);
defTableModel = new ResultSetTableModel(rs); // creating our table model
table.setModel(defTableModel); // setting our table model to JTable
...
...
// example if we want to search something
/**
* This method gets called when we search something in our search field.
*
* @param searchedText the text we are searching for
*/
public void search(String searchedText) {
try {
ResultSet rs = conLogic.execute(searchedText);
defTableModel.search(rs);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "An error occured while searching in the table!");
}
}
|
|
|
11/19/2015, 22:10
|
#3
|
elite*gold: 0
Join Date: Apr 2012
Posts: 55
Received Thanks: 3
|
Soo habe mal deine erste möglichkeit versucht zu machen aus gack ^^ aber das
Code:
itemlist.setModel(model);
will er nicht da sagt er das er das net findet ka iwie ich versuch mal das andere
SOOO Schau mal ich wollte das jetzt mit deiner resultsettable class machen ne ich habe diesen code jetzt hier in einer methode dafür:
Code:
public ResultSet loaditems(){
Statement stmt = null;
ResultSet rs = null;
try{ stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM item_template");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM item_template")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
defTableModel = new ResultSetTableModel(rs); // creating our table model
itemlist.setModel(defTableModel); // setting our table model to JTable
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
Habe ich da iwas übersehen oder vergessen ? :O habe mit diesen JTable dingern echt noch nie gescriptet und dementsprechend auch nie an Erfahrung sammeln können . Falls du benötigst das ist die Connect Klasse von mir
Code:
package wowgmtool;
/**
*
* @author Cybertronnix
*/
import abiturklassen.listenklassen.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Connect {
Connection conn = null;
String query;//SQL-Abfrage
int attributAnzahl;//Anzahl der Spalten
int datensatzAnzahl;//Anzahl der Zeilen
List attributNamen;//Liste der Spaltenueberschriften
List attributWerte;//Achtung: Liste von Listen, die die Attributwerte
// fuer jeweils einen Datensatz enthalten.
String dbhost="localhost";
String dbport="3308";
String dbase = "world";
String dbuser = "root";
String dbpass = "";
private TableModel model;
protected ResultSetTableModel defTableModel;
private Object itemlist;
public static void main(String[] args) {
// TODO code application logic here
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex) {
// handle the error
}
}
public void connect(){
try {
//conn =DriverManager.getConnection("jdbc:mysql://127.0.0.1:3308/test?" + "user=root&password=pserver");
String ConnString;
ConnString = "jdbc:mysql://"+dbhost+":"+dbport+"/"+dbase+"?user="+dbuser+"&password="+dbpass;
conn = DriverManager.getConnection(ConnString);
System.out.println("Connection to database successful!");
attributAnzahl=0;
datensatzAnzahl=0;
attributNamen=new List();
attributWerte=new List();
query = "SELECT * FROM world;";
}
catch (SQLException ex) {
System.out.println("Verbindung ist Fehlgeschlagen");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public void setzeSQLAbfrage(String sql)
{
query=sql;
}
public ResultSet ausfuehren(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM item_template");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM item_template")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
public ResultSet abfrage(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM npc_vendor");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM npc_vendor")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
//Muss noch richtig gemacht werden
public ResultSet einfuegen(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("INSERT INTO npc_vendor");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("INSERT INTO npc_vendor ")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
//exestiert noch gar nicht
public void loeschen(){
}
//exestiert noch gar nicht
public ResultSet loaditems(){
Statement stmt = null;
ResultSet rs = null;
try{ stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM item_template");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM item_template")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
defTableModel = new ResultSetTableModel(rs); // creating our table model
itemlist.setModel(defTableModel); // setting our table model to JTable
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
//exestiert noch gar nicht
public void loadvendors(){
}
}
in der, der RsSet halt in das Tablemodel reingepackt werden soll ( da liegt auch der RsSet) und dann wollte ich diese methode dann in meiner GUI nutzen
|
|
|
11/20/2015, 10:32
|
#4
|
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,226
|
Quote:
Originally Posted by Wanamakbestserver
Soo habe mal deine erste möglichkeit versucht zu machen aus gack ^^ aber das
Code:
itemlist.setModel(model);
will er nicht da sagt er das er das net findet ka iwie ich versuch mal das andere
Habe ich da iwas übersehen oder vergessen ? :O habe mit diesen JTable dingern echt noch nie gescriptet und dementsprechend auch nie an Erfahrung sammeln können . Falls du benötigst das ist die Connect Klasse von mir
Code:
package wowgmtool;
/**
*
* @author Cybertronnix
*/
import abiturklassen.listenklassen.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Connect {
Connection conn = null;
String query;//SQL-Abfrage
int attributAnzahl;//Anzahl der Spalten
int datensatzAnzahl;//Anzahl der Zeilen
List attributNamen;//Liste der Spaltenueberschriften
List attributWerte;//Achtung: Liste von Listen, die die Attributwerte
// fuer jeweils einen Datensatz enthalten.
String dbhost="localhost";
String dbport="3308";
String dbase = "world";
String dbuser = "root";
String dbpass = "";
private TableModel model;
protected ResultSetTableModel defTableModel;
private Object itemlist;
public static void main(String[] args) {
// TODO code application logic here
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex) {
// handle the error
}
}
public void connect(){
try {
//conn =DriverManager.getConnection("jdbc:mysql://127.0.0.1:3308/test?" + "user=root&password=pserver");
String ConnString;
ConnString = "jdbc:mysql://"+dbhost+":"+dbport+"/"+dbase+"?user="+dbuser+"&password="+dbpass;
conn = DriverManager.getConnection(ConnString);
System.out.println("Connection to database successful!");
attributAnzahl=0;
datensatzAnzahl=0;
attributNamen=new List();
attributWerte=new List();
query = "SELECT * FROM world;";
}
catch (SQLException ex) {
System.out.println("Verbindung ist Fehlgeschlagen");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public void setzeSQLAbfrage(String sql)
{
query=sql;
}
public ResultSet ausfuehren(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM item_template");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM item_template")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
public ResultSet abfrage(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM npc_vendor");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM npc_vendor")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
//Muss noch richtig gemacht werden
public ResultSet einfuegen(){
// assume that conn is an already created JDBC connection (see previous examples)
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("INSERT INTO npc_vendor");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("INSERT INTO npc_vendor ")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
//exestiert noch gar nicht
public void loeschen(){
}
//exestiert noch gar nicht
public ResultSet loaditems(){
Statement stmt = null;
ResultSet rs = null;
try{ stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM item_template");
//System.out.println(rs);
// or alternatively, if you don't know ahead of time that
// the query will be a SELECT...
if (stmt.execute("SELECT * FROM item_template")) {
rs = stmt.getResultSet();
//System.out.println(rs);
}
while ( rs.next() )
System.out.printf( "%s, %s, %s%n", rs.getString(1),
rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) );
// Now do something with the ResultSet ....
defTableModel = new ResultSetTableModel(rs); // creating our table model
itemlist.setModel(defTableModel); // setting our table model to JTable
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
return rs;
}
//exestiert noch gar nicht
public void loadvendors(){
}
}
in der, der RsSet halt in das Tablemodel reingepackt werden soll ( da liegt auch der RsSet) und dann wollte ich diese methode dann in meiner GUI nutzen
|
Code:
itemList.setModel(model);
Funktioniert nicht/findet er nicht, weil itemList bei dir den Typ Object hat.
Du brauchst eine JTable und ein DefaultTableModel Variable.
Code:
private JTable table;
private DefaultTableModel model;
Und dann den Code von mir vom obrigen Post verwenden, um die Daten dynamisch zu holen und zu speichern und diese dann per:
Code:
model = new DefaultTableModel(erg, column);
table.setModel(model);
in die Tabelle zu setzen.
|
|
|
11/22/2015, 02:47
|
#5
|
elite*gold: 0
Join Date: Apr 2012
Posts: 55
Received Thanks: 3
|
joa wenn er bei mir erg und column erkennen würd ^^
|
|
|
11/22/2015, 11:38
|
#6
|
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,226
|
erg und column werden wie im ersten Post verwendet. Also sollte es kein Problem geben.
|
|
|
|
Similar Threads
|
-- Channelstatus anzeigen lassen --
08/11/2013 - Metin2 Private Server - 1 Replies
Hallo zusammen,
da ich aktuell an meinem Server arbeite ist mir folgendes aufgefallen: Egal ob der Server online ist oder nicht, bei der Serverauswahl und den verschiedenen Chs steht immer folgendes:
CH1 ....
CH2 ....
CH3 ....
CH4 ....
Wie lässt sich das ändern das dort NORM, BUSY oder FULL steht?? (je nach Spieleranzahl) Sufu wurde benutzt, aber habe nur veraltete Threads gefunden in denen teilweise Videos verlinkt waren, welche aber nichtmehr existieren.
|
Bonis anzeigen lassen?
07/02/2013 - Metin2 Private Server - 14 Replies
Hey Com,
ich grübel schon die ganze zeit wie ich auf ein Items die gewählten bonis anzeigen lassen kann^^
z.b bei dem großen Roten Trank wird ja angezeigt der Boni "Max. TP + 1200" oder
bei einem Rassenstein wird der Effekt dort ja auch angezeigt.
Auf dem Server Pedöbär2, der ja leider jetzt Off ist, hatte der Halbmondring dort auch gewisse Bonis angezeigt, vielleicht kennt ihr den ja :)
Das würde ich gerne auch bei einem beliebigen Item erscheinen lassen, nur leider hab ich keine...
|
[Help]Reich anzeigen lassen
06/17/2013 - Metin2 Private Server - 1 Replies
Wie kann man in einer Quest anzeigen lassen in welchem Reich man ist? :)
|
Schmiedechancen anzeigen lassen?
12/22/2011 - Metin2 Private Server - 2 Replies
Hallo,ich suche ein howto oder hilfe das ich bei mir die schmiedechancen anzeigen lassen kann,ich benutze den clienten von xxdemonenxx,habe schon etwas gefunden hier wo man in der uirefine.py eine zeile löschen bzw ändern soll,was ich auch getan habe erst geändert dann gelöscht aber keines von beiden funktioniert,gibt es noch ne möglichkeit?wäre für hilfe sehr dankbar
MFG
|
Passwort anzeigen lassen
09/08/2011 - Technical Support - 7 Replies
Hey,
ich habe meine Internetzugangsdaten verloren .... Wie kann ich diese auslesen ?
|
All times are GMT +1. The time now is 13:25.
|
|