1

can you help me. I got stock in my project.. when I retrieve Resultset, it doesn't appear in field when I click on the table.. the code is below..

private void table_lectureKeyPressed(java.awt.event.KeyEvent evt) {                                         
  if(evt.getKeyCode()==KeyEvent.VK_DOWN || evt.getKeyCode()==KeyEvent.VK_UP ){

    try {
      int row=table_lecture.getSelectedRow();
      String Table_Clicked=table_lecture.getModel().getValueAt(row, 0).toString();
      String sql="Select * from Lectures where Lecture_ID='"+Table_Clicked+"' ";
      ps=con.prepareStatement(sql);
      rs=ps.executeQuery();    

      if (rs.next()) {
       try {
         String add1=rs.getString("Lecture_ID").toString();
         txt_LectureNO.setText(add1);
         String add2=rs.getString("Lecture_Name");
         txt_lecture.setText(add2);
         String add3=rs.getString("Lecture_Date");
         dtc_Date.setDateFormatString(add3);
         String add4=rs.getString("Lecture_Time");
         txt_Time.setText(add4);
         String add5=rs.getString("Lecturer");
         txt_Lecturer.setText(add5);
         String add6=rs.getString("Lecture_Place");
         txt_Place.setText(add6);
         String add7=rs.getString("Audiance");
         txt_Audiance.setText(add7);
         String add8=rs.getString("Remark");
         txp_Remarks.setText(add8);
       } catch(Exception e) {
          JOptionPane.showMessageDialog(null, e.getMessage());
        }
      }
    } catch(Exception e ){
      JOptionPane.showMessageDialog(null, e.getMessage());
    }          
  }   
}  

Any help appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    your question is unclear to me, did you get any error message? is yes then post it. – ravibagul91 Feb 03 '14 at 10:23
  • yes I got { Microsoft [ODBC Microsoft Access Drver] Data type Mismatch criteria Expression – user3265292 Feb 03 '14 at 11:01
  • 1) Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..` 2) For better help sooner, post a [Minimal Complete Tested and Readable Example](http://stackoverflow.com/help/mcve) (MCTRE). – Andrew Thompson Feb 03 '14 at 11:09
  • my entire project seem has same problem. can anyone see it http://www.gulfup.com/?GVqln1 – user3265292 Feb 03 '14 at 11:09
  • Tip: Add @Jugadu (or whoever - the `@` is important) to *notify* them of a new comment. – Andrew Thompson Feb 03 '14 at 11:11
  • 1
    *"can anyone see it"* Many people cannot or will not follow external links. Post an MCTRE as an [edit to the question](http://stackoverflow.com/posts/21524191/edit). – Andrew Thompson Feb 03 '14 at 11:12

1 Answers1

4

Note your SQL statement:

"Select * from Lectures where Lecture_ID='"+Table_Clicked+"' "

This statement will be parsed in something like this:

Select * from Lectures where Lecture_ID = '1'

Tipically an ID is a numerical data type, so I suspect the problem is in your SQL statement giving you an empty result set (or causing an SQLException). In addition your query is vulnerable to SQL injection attacks.

To avoid these issues you need to use PreparedStatement properly:

int row = table_lecture.getSelectedRow();
Object tableClicked = table_lecture.getValueAt(row, 0);
String sql = "SELECT * FROM Lectures WHERE Lecture_ID = ?";
ps = con.prepareStatement(sql);
ps.setObject(1, tableClicked);
rs = ps.executeQuery();    

Some hints on your code:

int row = table_lecture.getSelectedRow();
String Table_Clicked = table_lecture.getModel().getValueAt(row, 0).toString();

Here row is the selected row number in the view not the model, so you should request the proper value. You have two options:

Request value to the table

int row = table_lecture.getSelectedRow();
String Table_Clicked = table_lecture.getValueAt(row, 0).toString();

Convert view index in its respective model index

int row = table_lecture.getSelectedRow();
int modelIndex = table_lecture.convertRowIndexToModel(row);
String Table_Clicked = table_lecture.getModel().getValueAt(modelIndex , 0).toString();

It's preferable use Keybinding over KeyListeners when you're working with Swing for the reasons discussed in this topic: Key bindings vs. key listeners in Java.

In this particular case I wouldn't use keybinding either but use ListSelectionListener instead:

table_lecture.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
     @Override
     public void valueChanged(ListSelectionEvent e) {
         // your code here
     }
 });

Database calls are time consuming tasks and may block the Event Dispatch Thread (a.k.a. EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.

I'd suggest you take a look to the hints exposed in this answer. Summarized:

  • Wrap data in a domain class.
  • Use a SwingWorker to do the database calls in a background thread and update Swing components in the EDT.

Finally but not less important, alway follow Java Code Conventions

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • I'm not sure if you've struck the answer here (mostly because of uncertainty about the question) but +1 for the slew of excellent advice. – Andrew Thompson Feb 03 '14 at 11:14
  • @AndrewThompson thank you. Honestly at first time it was more like a guess but based on this OP's comment: *"yes I got { Microsoft [ODBC Microsoft Access Drver] Data type Mismatch criteria Expression"* I think the problem is clearly in SQL statement. – dic19 Feb 03 '14 at 11:37