Hi, I a having a problem with some code I am trying to run. What I want to happen is to be able to select a row that is displayed in a JTable and view the results for that row in a DOS window. When I select a row I get the error message in the Subject line. I think the problem is that I am not passing the PersonID I need for the SQL statement. I will first paste in the valueChanged method and then paste in my full code if that helps. Thank you for your time. Method: // I am having a problem here getting the selected row to display results in the DOS window
public void valueChanged(ListSelectionEvent e) { try { String selectedPerson = "SELECT * FROM Person Where PersonID = ?"; PreparedStatement displayPerson = connection.prepareStatement(selectedPerson);
int selectedRow = table.getSelectedRow(); displayPerson.setInt(1, selectedRow); ResultSet queryInfo = displayPerson.executeQuery(); //output the results set data to DOS window to make sure SQL is working System.out.println(queryInfo.getString("LastName") +" " + queryInfo.getString("FirstName") + " " + queryInfo.getInt("PersonID"));
//Button Pane JButton newButton = new JButton("New"); JButton deleteButton = new JButton("Delete"); JButton saveButton = new JButton("Save"); //Box for Buttons JPanel buttonPane = new JPanel(); buttonPane.add(newButton); buttonPane.add(deleteButton); buttonPane.add(saveButton); getContentPane().add(buttonPane, BorderLayout.SOUTH); //add event listeners newButton.addActionListener(this); deleteButton.addActionListener(this); saveButton.addActionListener(this); rowSM.addListSelectionListener(this); pack(); setVisible(true); show(); openConnection(); }
public void actionPerformed(ActionEvent ae) { String cmd = ae.getActionCommand(); if (cmd.equalsIgnoreCase("NEW")) {
} else if (cmd.equalsIgnoreCase("DELETE")) {
} else if (cmd.equalsIgnoreCase("SAVE")) { } }
public void openConnection() { try { //code to open the connection Class.forName(driver); connection = DriverManager.getConnection(url); statement = connection.createStatement();//statement for query tableModel.setResultSet( statement.executeQuery("Select PersonID AS ID, LastName +', ' + FirstName AS NAME, Email FROM Person")); } catch(ClassNotFoundException cnfe) { System.err.println(cnfe); } catch(SQLException sqle) { System.err.println(sqle); } }
// I am having a problem here getting the selected row to display results in the DOS window
public void valueChanged(ListSelectionEvent e) { try { String selectedPerson = "SELECT * FROM Person Where PersonID = ?"; PreparedStatement displayPerson = connection.prepareStatement(selectedPerson);
int selectedRow = table.getSelectedRow(); displayPerson.setInt(1, selectedRow); ResultSet queryInfo = displayPerson.executeQuery(); //output the results set data to DOS window to make sure SQL is working System.out.println(queryInfo.getString("LastName") +" " + queryInfo.getString("FirstName") + " " + queryInfo.getInt("PersonID"));
} catch (SQLException sqle) { System.err.println(sqle); } } //inner class defining handler for window events class WindowHandler extends WindowAdapter { //handler for window closing event public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }
I think you're getting an invalid cursor error because the cursor is not set on any row in the ResultSet. Try changing your method code to add this: while (queryInfo.next()) { System.out.println(queryInfo.getString("LastName") +" " + queryInfo.getString("FirstName") + " " + queryInfo.getInt("PersonID")); } The next() function will return false if there is no row to move to (empty ResultSet) or will move the cursor to the first row.
Hi Mike, I think that you should try out using resultset.next() method. As the resultset doesn't have the pointer on the first record & hence you need to use the next() method for the first record. Try this...... while(queryInfo.next()) { queryInfo.getString("FirstName") + " " + queryInfo.getInt("PersonID")); } Good Luck, Rajeev
Angela Lamb
Ranch Hand
Joined: Feb 22, 2001
Posts: 156
posted
1
Rajeev, that is almost word for word what I said. Did you bother to read the rest of the posts before you answered?