它不会添加数据,列名工作,我很疲惫我不知道从哪里开始。没有错误消息只是不会这样做,它显示对象而不是字符串也不知道为什么它不会工作,因为model.addRow()要求一个对象[] [],我相信我提供?
initComponents();
try {
PreparedStatement stat = Domein.dConnection().prepareStatement("SELECT domainid , domain FROM nabilpx84_hamchi.dnsp_domains");
ResultSet resultaat = stat.executeQuery();
ResultSetMetaData naam = resultaat.getMetaData();
Object[] kolommen = new Object[2];
Object[][] data = null ;
DefaultTableModel model = new DefaultTableModel(null,data);
// kolom namen
for (int i = 1; i <= naam.getColumnCount(); ++i) {
model.addColumn(naam.getColumnName(i));
}
//records
data = new Object[1][2];
while(resultaat.next())
{
data[0][0] = resultaat.getInt(1);
data[0][1] = resultaat.getString(2);
if(resultaat.isFirst())
{
model.addRow(data);
}
}
uitvoerTabel.setModel(model);
} catch (SQLException ex) {
Logger.getLogger(Domein.class.getName()).log(Level.SEVERE, null, ex);
}
答案 0 :(得分:2)
你有一些看起来很时髦的代码,你似乎在将两次数据添加到你的表模型中。为什么不创建一个DefaultTableModel,其列标题为Vector或String数组,行数为0,然后在while循环中添加Object数组行或Vector行?
此外,您必须为每一行创建一个新的Object数组或Vector。你在while循环中没有这样做。
类似的东西:
ResultSet resultaat = stat.executeQuery();
ResultSetMetaData naam = resultaat.getMetaData();
Vector<String> columnVector = new Vector<String>();
for (int i = 1; i <= naam.getColumnCount(); ++i) {
columnVector.addElement(naam.getColumnName(i));
}
DefaultTableModel model = new DefaultTableModel(columnVector, 0);
while(resultaat.next()) {
// you must create a new Object array or Vector for each row
Object[] rowData = new Object[2];
rowData[0] = resultaat.getInt(1);
rowData[1] = resultaat.getString(2);
if(resultaat.isFirst()) { // not sure what this is for
model.addRow(rowData);
}
}
另外,请查看Rob Camick在数据库博客中的表格,您可以在此处找到:Table From Database
答案 1 :(得分:2)
你可以尝试这样的事情:
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
DefaultTableModel model = new DefaultTableModel(data, columnNames);
另一方面,服务层可以帮助您在Swing应用程序中解耦或拥有一个或多个存储库的服务外观。
答案 2 :(得分:1)
我有自己的班级,我从头痛中幸存下来 如果你需要填写任何jtable,试试这个:
SQLGenerat sql = new SQLGenerat ();
jTable1.setModel(new DefaultTableModel(sql.getRows("SQL code"), sql.getColumns("SQl CODE")));
这是您可以使用它的类
import java.sql.*;
import java.util.Vector;
/**
*
* @author Azad
*/
public class SQLGenerat {
Connection con;
Statement st ;
ResultSet rs ;
public Vector getColumnNames(String sql)
{
Vector columnNames = new Vector();
/*
*dont foeget to wirte your connection
*before executing any sql statement
*/
try{
st = con.createStatement();
rs= st.executeQuery(sql);
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
while(rs.next())
{
Vector column = new Vector(columns);
for(int i = 0 ; i <= columns-1 ; i++){
column.addElement(metaData.getColumnName(i));
}
columnNames.addElement(column);
}
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
return columnNames;
}
public Vector getRowDatas(String sql)
{
Vector rowDatas = new Vector ();
/*
*dont foeget to wirte your connection
*before executing any sql statement
*/
try{
st = con.createStatement();
rs=st.executeQuery(sql);
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
while(rs.next())
{
Vector rows = new Vector(columns);
for(int i = 0 ; i <= columns -1 ;i++)
{
rows.addElement(rs.getObject(i));
}
rowDatas.addElement(rows);
}
rs.close();
st.close();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return rowDatas;
}
}