0

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? 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

  • I don't think that a ResultSet has any values if the Statement fails to return any rows. You should check for this. – NomadMaker Dec 04 '20 at 10:25
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. But well, NPEs are (sort of) always the same. You have to understand what they mean in general, and then you apply that knowledge to your specific case. – GhostCat Dec 04 '20 at 10:44
  • 1
    The MCVE part: you should separate the "reading of data from the DB" from the GUI side completely. Start with a small method that only connects to the DB, runs the query, and PRINTS the results (and all important steps in-between) to the console. Dont burden yourself (or us here) with GUI code that hasnt to do anything with the DB reading part. And then, when you can read your data successfully, then add the code that puts in a GUI. – GhostCat Dec 04 '20 at 10:46
  • I did that just before posting the question first I had a Null pointer exception while converting a double array to a string array for the JTable, after that I got this error result set object has no set row – abhinav bandaru Dec 04 '20 at 10:55
  • This was originally closed as a duplicate of: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it. The OP has since restated the question remove the comment about a NPE. The problem is now defined as: `the ResultSet object has no set row?` – camickr Dec 04 '20 at 15:41

2 Answers2

0

a null pointer exception is usually something that isn't getting initialized with a value and getting a null instead. Try adding breakpoints to check where that null is and once found add:

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

   //some code

}
dan
  • 53
  • 7
0

the ResultSet object has no set row?

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

You can't just access a row in the ResultSet directly. You first need to point to a row in the Result set.

This is done by invoking the next() method of the ResultSet.

Then typically you would use looping code to read each row of data in the 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.

Also, you should use a PreparedStatement to build the SQL query. It will allow you to more easily set the parameters of the query. Search the forum for more information and example code.

camickr
  • 321,443
  • 19
  • 166
  • 288