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