Select statement in Java

public void search() throws Exception{
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);
        }

In my resultset rs = state ExecuteQuery () I use this 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");

First, I run this line (ptotaldue doesn't come to JTable.)

The second time I tried to display it, I did this:

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

Now that I've tried this, I think my code is really wrong BTW this column has a currency data type

Why didn't you show me the JTable? Anyone can tell me what's wrong with my code?

(there are questions in my answer) the public class queryonworkerthread extends the 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,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 );
    }
  }

Solution

Try this

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

Connection con =  DriverManager.getConnection(url,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});
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>