9
    public void search() throws Exception{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url = "jdbc:odbc:******";
                String user = "*****";
                String pass = "*****";
                Connection con =  DriverManager.getConnection(url, user, pass);
                Statement state = con.createStatement();
                ResultSet rs = state.executeQuery("");
                ResultSetMetaData rsmetadata = rs.getMetaData();
                int columns = rsmetadata.getColumnCount();
                DefaultTableModel dtm = new DefaultTableModel();
                Vector column_name = new Vector();
                Vector data_rows = new Vector();

                for (int i=1; i<columns;i++){
                    column_name.addElement(rsmetadata.getColumnName(i));
                }
                dtm.setColumnIdentifiers(column_name);

                while(rs.next()){
                    data_rows = new Vector();
                    for (int j=1; j<columns; j++){
                    data_rows.addElement(rs.getString(j));
                    }
                    dtm.addRow(data_rows);
                }
                tblPatient.setModel(dtm);
        }

在我的ResultSet rs = state.executeQuery()我用这个SQL

                              "SELECT "
                            + "pIDNo AS 'Patient ID',"
                            + "pLName AS 'Last Name'," 
                            + "pFName AS 'First Name',"
                            + "pMI AS 'M.I.',"
                            + "pSex AS 'Sex',"
                            + "pStatus AS 'Status',"
                            + "pTelNo AS 'Contact No.',"
                            + "pDocID AS 'Doctor ID',"
                            + "pAddr AS 'St. No.',"
                            + "pStreet AS 'St. Name',"
                            + "pBarangay AS 'Barangay',"
                            + "pCity AS 'City',"
                            + " pProvince AS 'Province',"
                            + " pLNameKIN AS 'Last Name',"
                            + "pFNameKIN AS 'First Name',"
                            + "pMIKIN AS 'M.I.',"
                            + "pRelationKIN AS 'Relation',"
                            + "pTotalDue AS 'Total Due'"
                            + " FROM dbo.Patients"); 

首先我运行这一行(pTotalDue 没有出现在 jTable 中。)

在我第二次尝试显示它时,我这样做:

"SELECT pTotalDue AS 'Total Due' FROM dbo.Patients"

现在我尝试了这个,我认为我的代码确实有问题。顺便说一句,此列具有 MONEY DATA TYPE

为什么它没有显示给我JTable?谁能告诉我我的代码有什么问题?

(给我的答案中的问题) public class QueryOnWorkerThread extends SwingWorker{ private final JTable tableToUpdate;

  public QueryOnWorkerThread( JTable aTableToUpdate ) {
    tableToUpdate = aTableToUpdate;
  }

  @Override
  protected TableModel doInBackground() throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:OJT_dsn";
    String user = "sa";
    String pass = "";
    Connection con =  DriverManager.getConnection( url, user, pass );
    Statement state = con.createStatement();
    ResultSet rs = state.executeQuery("");
    ResultSetMetaData rsmetadata = rs.getMetaData();
    int columns = rsmetadata.getColumnCount();
    DefaultTableModel dtm = new DefaultTableModel();
    Vector column_name = new Vector();
    Vector data_rows;

    //note the <= check iso the < check (as the count starts at index 1)
    for (int i=1; i<=columns;i++){
      column_name.addElement(rsmetadata.getColumnName(i));
    }
    dtm.setColumnIdentifiers(column_name);

    while(rs.next()){
      data_rows = new Vector();
      //note the <= check iso the < check (as the count starts at index 1)
      for (int j=1; j<=columns; j++){
        data_rows.addElement(rs.getString(j));
      }
      dtm.addRow(data_rows);
    }
    return dtm;
  }


        `@Override <<<<<<<<<<<<<<<<<<<<< I have a problem here it says : done() in javaapplication25.SearchPatient.QueryWorkerThread cannot override done() in javax.swing.SwingWorker overriden method does not throw java.lang.Exception , what does it mean sir?` 
  protected void done() throws Exception{
    //this method runs on the EDT, so it is safe to update our table here
    try {
      tableToUpdate.setModel( get() );
    } catch ( InterruptedException e ) {
      throw new RuntimeException( e );
    } catch ( ExecutionException e ) {
      throw new RuntimeException( e );
    }
  }
4

5 回答 5

8

尝试这个

DefaultTableModel dtm=(DefaultTableModel)table.getModel();
for (int i = dtm.getRowCount() - 1; i > -1; i--) {
dtm.removeRow(i);
}

Connection con =  DriverManager.getConnection(url, user, pass);
Statement state = con.createStatement();
ResultSet rs = state.executeQuery("Your SQL Query");

while(rs.next())
{
String str1=rs.getString(1);
String str2=rs.getString(2);
String str3=rs.getString(3);
String str4=rs.getString(4);
String str5=rs.getString(5);
:
:
:
dtm.addRow(new Object[]{str1,str2,str3,str4,str5});
}
于 2013-04-10T09:23:26.107 回答
6

在你的循环中,你的退出条件是

j<columns 

这意味着永远不会恢复最后一列。试试这个:

for (int j=1; j<=columns; j++)
于 2013-04-10T09:21:32.447 回答
5

正如@Joan 已经指出的那样,您最后一列没有出现的事实可能与您的循环语句有关。

但是,此代码存在更多问题。您应该只在 Event Dispatch Thread 上更新 Swing 组件,并且不应在该线程上执行长时间运行的操作。简而言之,混合 SQL 查询和更新JTable不应该发生在同一个线程上。有关更多信息,请参阅Swing 中的并发指南。

使用 aSwingWorker可以解决这个问题:

public class QueryOnWorkerThread extends SwingWorker<TableModel, Void>{
  private final JTable tableToUpdate;

  public QueryOnWorkerThread( JTable aTableToUpdate ) {
    tableToUpdate = aTableToUpdate;
  }

  @Override
  protected TableModel doInBackground() throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:******";
    String user = "*****";
    String pass = "*****";
    Connection con =  DriverManager.getConnection( url, user, pass );
    Statement state = con.createStatement();
    ResultSet rs = state.executeQuery("");
    ResultSetMetaData rsmetadata = rs.getMetaData();
    int columns = rsmetadata.getColumnCount();
    DefaultTableModel dtm = new DefaultTableModel();
    Vector column_name = new Vector();
    Vector data_rows;

    //note the <= check iso the < check (as the count starts at index 1)
    for (int i=1; i<=columns;i++){
      column_name.addElement(rsmetadata.getColumnName(i));
    }
    dtm.setColumnIdentifiers(column_name);

    while(rs.next()){
      data_rows = new Vector();
      //note the <= check iso the < check (as the count starts at index 1)
      for (int j=1; j<=columns; j++){
        data_rows.addElement(rs.getString(j));
      }
      dtm.addRow(data_rows);
    }
    return dtm;
  }

  @Override
  protected void done() {
    //this method runs on the EDT, so it is safe to update our table here
    try {
      tableToUpdate.setModel( get() );
    } catch ( InterruptedException e ) {
      throw new RuntimeException( e );
    } catch ( ExecutionException e ) {
      throw new RuntimeException( e );
    }
  }
}

SwingWorker可以通过调用启动

QueryOnWorkerThread worker = new QueryOnWorkerThread( tblPatient );
worker.execute();

请注意我如何更改代码中的循环

于 2013-04-12T11:03:20.660 回答
4

ResultSet.getBigDecimal()尝试通过而不是通过获取该列ResultSet.getString()。然后把你检索BigDecimal.toPlainString()到你的表格单元格。

例子:

data_rows.addElement(rs.getBigDecimal("pTotalDue").toPlainString());//Assuming your select returns a pTotalDue Column (e.g. SELECT pTotalDue,... FROM ...)
于 2013-04-12T12:05:37.530 回答
3

尝试使用 TableCellRenderer。

实现渲染器并以您希望的形式呈现具有货币类型的列。

问候, HL

于 2013-04-18T09:04:41.610 回答