0

I am using Sqlite, and this is the code I have written for my database and a picture of my program running showing the dilemma.

I have to input the full text of table field to get search result. BUT I want to enter only a few letters to bring up results.

private void txt_SearchKeyReleased(java.awt.event.KeyEvent evt) {                                       
    // TODO add your handling code here:


    try {

        String sql ="select * from GameList1 where Name =?";
        pst=conn.prepareStatement(sql);
        pst.setString(1, txt_Search.getText());

        rs=pst.executeQuery();
        if(rs.next()) {

            String add1=rs.getString("GameID");
            txt_GameID.setText(add1);
            String add2=rs.getString("Name");
            txt_Name.setText(add2);
            String add3=rs.getString("Company");
            txt_Company.setText(add3);
            String add4=rs.getString("Format");
            txt_Format.setText(add4);
            String add5=rs.getString("YearMade");
            txt_Year.setText(add5);
            String add6=rs.getString("YearReleased");
            txt_Release.setText(add6);


        }
    }
}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79

1 Answers1

0

I assume that you want to retrieve data from database by typing only few letters and not searching by full row value. If this is the problem than the solution would be LIKE operator.

Example:

try {
    String sql ="select * from GameList1 where Name LIKE ?";
    String PERCENTAGE_SIGN="%";
    pst=conn.prepareStatement(sql);
    pst.setString(1, PERCENTAGE_SIGN+txt_Search.getText()+PERCENTAGE_SIGN);

    rs=pst.executeQuery();
    if(rs.next()) {

        String add1=rs.getString("GameID");
        txt_GameID.setText(add1);
        String add2=rs.getString("Name");
        txt_Name.setText(add2);
        String add3=rs.getString("Company");
        txt_Company.setText(add3);
        String add4=rs.getString("Format");
        txt_Format.setText(add4);
        String add5=rs.getString("YearMade");
        txt_Year.setText(add5);
        String add6=rs.getString("YearReleased");
        txt_Release.setText(add6);


                     }
    }
drgPP
  • 926
  • 2
  • 7
  • 22