简体   繁体   English

如何将 sql 查询的结果导入 JTable?

[英]How can I make import the results of an sql query into a JTable?

I am trying to import the results from an SQL query into a JTable, but when I run it, it tells me that the ResultSet object has no set row?我正在尝试将 SQL 查询的结果导入 JTable,但是当我运行它时,它告诉我 ResultSet object 没有设置行? What does this mean?这是什么意思? how Can I fix this?我怎样才能解决这个问题? here is what I've tried:这是我尝试过的:



import javax.swing.*;
import java.sql.*;


public class GUI {


    // frame
    JFrame f;
    // Table
    JTable j;

    // Constructor
    GUI()
    {
        ResultSet result;
        // Frame initiallization
        f = new JFrame();

        // Frame Title
        f.setTitle("SQL to JTable");
        try{
            String dbURL = "jdbc:sqlserver://DESKTOP-TSKUNOE\\MSSQLSERVER:1433;databaseName=LMD";
            String user = "programer151";
            String password = "abhinav@123";
            Connection connection = DriverManager.getConnection(dbURL, user, password);
            String code = "select * from dbo.LMD_Table where [DATE] = '2019-02-01'";
            Statement statement = connection.createStatement();
            result = statement.executeQuery(code);

            // Data to be displayed in the JTable
            double[] data =
                    {result.getFloat(2), result.getFloat(3), result.getFloat(4), result.getFloat(5), result.getFloat(6), result.getFloat(7), result.getFloat(8), result.getFloat(9), result.getFloat(10), result.getFloat(11), result.getFloat(12), result.getFloat(13)};
            ;
            String[][] data1 = new String[13][0];

            for (int i = 0; i<=13; i++) {
                data1[i][0] = String.valueOf(data[i]);
            }
            String[] columnNames = {  "data", "data","data", "data", "data","data", "data", "data","data", "data", "data","data" };
            j = new JTable(data1,columnNames);
            j.setBounds(30, 40, 200, 300);

            // adding it to JScrollPane
            JScrollPane sp = new JScrollPane(j);
            f.add(sp);
            // Frame Size
            f.setSize(500, 200);
            // Frame Visible = true
            f.setVisible(true);

        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    // Driver method
    public static void main(String[] args)
    {
        new GUI();
    }
}



the output I want is a JTable which gives me the results of an sql query我想要的 output 是 JTable ,它给了我 sql 查询的结果

a null pointer exception is usually something that isn't getting initialized with a value and getting a null instead. null 指针异常通常是没有使用值初始化并获得 null 的东西。 Try adding breakpoints to check where that null is and once found add:尝试添加断点来检查 null 的位置,一旦找到添加:

if (variable != null) { //variable could be an object

   //some code

}

the ResultSet object has no set row? ResultSet object 没有设置行?

result = statement.executeQuery(code);
double[] data = {result.getFloat(2), result.getFloat(3), ...};

You can't just access a row in the ResultSet directly.您不能直接访问 ResultSet 中的一行。 You first need to point to a row in the Result set.您首先需要指向结果集中的一行。

This is done by invoking the next() method of the ResultSet .这是通过调用ResultSetnext()方法来完成的。

Then typically you would use looping code to read each row of data in the ResultSet :然后通常您会使用循环代码来读取ResultSet中的每一行数据:

result = statement.executeQuery(code);

while (result.next())
{
    // get the data from the current row of the ResultSet 
}

See: How to get a DefaultTableModel object's data into a subclass of DefaultTableModel for a more complete example of how to create your DefaultTableModel from a ResultSet.有关如何从 ResultSet 创建 DefaultTableModel 的更完整示例,请参阅: 如何将 DefaultTableModel 对象的数据获取到 DefaultTableModel 的子类中

Also, you should use a PreparedStatement to build the SQL query.此外,您应该使用PreparedStatement来构建 SQL 查询。 It will allow you to more easily set the parameters of the query.它将允许您更轻松地设置查询的参数。 Search the forum for more information and example code.在论坛中搜索更多信息和示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM