努力从访问中获取数据库搜索结果以显示在JTable(JAVA)中

时间:2017-06-21 11:39:49

标签: java database swing user-interface

这是我的客户端应用中的代码。我无法通过客户端GUI获取搜索结果,然后将其显示在JTable中。

package clientapp;

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class ClientApp {

Socket clientSocket;
Connection con;
String driverName;
String sourceURL;
PreparedStatement psmt;
ResultSet resultSet;

public ClientApp() {
    try {
        clientSocket = new Socket("127.0.0.1", 16000);
    } catch (IOException e) {
        System.err.println("Error occurred " + e);
    }
}

public void checkAnimal(String text) throws SQLException, 
ClassNotFoundException {
    String selectQuery = "SELECT animal_ID, animal_name, description, 
speciesID FROM animalTable WHERE animal_name LIKE ?";

    driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
    sourceURL = "jdbc:odbc:WildlifeDatabase";
    Class.forName(driverName);
    con = DriverManager.getConnection(sourceURL, "", "");

    psmt = con.prepareStatement(selectQuery);
    psmt.setString(1, "%s%");
    resultSet = psmt.executeQuery();

    try {
        if (true) {
            while (resultSet.next()) {
                int animal_ID = resultSet.getInt(1);
                String animal_name = resultSet.getString(2);
                String description = resultSet.getString(3);
                int speciesID = resultSet.getInt(4);
                String field = (Integer.toString(animal_ID) + " , " + 
animal_name + " , " + description + " , " + Integer.toString(speciesID));
                JOptionPane.showMessageDialog(null, field);
            }
        }else {
            JOptionPane.showMessageDialog(null, "No Results 
found","Search",JOptionPane.WARNING_MESSAGE);
        }

        }catch (SQLException ex) {
        Logger.getLogger(ClientApp.class.getName()).log(Level.SEVERE, null, 
ex);
    }
    }
}

我创建了一个GUI,在其中我有一个搜索框,可以在其中键入动物的名称并获得结果。

1 个答案:

答案 0 :(得分:0)

您需要从查询中获取列和数据,然后以JTable的形式将其插入TableModel

//...
        ResultSet resultSet = psmt.executeQuery();
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

        Vector<String> columnNames = new Vector<>();

        int columnCount = resultSetMetaData.getColumnCount();

        for (int column = 1; column <= columnCount; column++) {

            columnNames.add( resultSetMetaData.getColumnName(column) );

        }

        Vector<Vector<Object>> tableData = new Vector<>();

        while ( resultSet.next() ) {
            Vector<Object> vector = new Vector<>();

            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {

                vector.add( resultSet.getObject(columnIndex) );

            }

            tableData.add(vector);
        }

        JTable table = new JTable();
        table.setModel( new DefaultTableModel(tableData, columnNames) );
//...

而且我没有看到你甚至试图展示你是如何使用JTable的,以及你的问题究竟是什么。

if (true) - 根据else中发生的情况,我了解您要检查resultSet是否包含查询中的任何数据。您可以查看if(resultSet.isClosed())。它将在没有数据时关闭。