Search code examples
javaderby

passing and parsing object input dialog value to database table


I want to know how to get integer value from input dailog box

and query it inside the select statement looks my error the input box type as per my below is Object and i have no idea how to parse object to integer so as to be accepted by select statement please advise how to handle object type to get inputbox variables and inject same into my select

private void InputVal(){

       // here is the input box to retrieve user entry
      Object journal_no = JOptionPane.showInputDialog(
              null,    "Please enter Journal No.?:\n",  "Search", JOptionPane.PLAIN_MESSAGE,
              null,null,"");
      if (journal_no.equals("")){
          JOptionPane.showMessageDialog(null, "Please enter correct No.");
          return;


      }
       // HERE I WILL CALL SQL STATEMENT TO LOAD Table rely on //inputbox 
      try {
  String host1=  "jdbc:derby://localhost:1527//accountsdb";
  String uName1="accounts";
  String uPass1="accounts";
   con1=DriverManager.getConnection(host1,uName1 ,uPass1);
    //String sql ="select * from log where password= jTuser.getText()  " ;

  String sql1="select * from JOUNRAL Where journal_no=" +   journal_no + "  ";
   stmt1=con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
   ResultSet.CONCUR_UPDATABLE);
       rs=stmt1.executeQuery(sql1);

            while (rs.next()){ 
                //Load values into
             Date Jdate  =rs.getDate("journal_date"); 
            txt_date.setDate(Jdate);
            int JNo;
      JNo = rs.getInt("journal_no");
            jLjournal_no.setText(Integer.toString(JNo));
            }
        } 
        catch (SQLException ex) {
              System.out.println(ex.getMessage())   ;     
}
       }

Solution

  • If that would definitely be an int use int journalNo = Integer(ObjectValue);

    Else convert it to string and then to int. You will be able to catch NumberFormatException in that case.

    Object to String:

    String journalNumber = objectValue.toString();

    String to int:

    int journalNumberAsInt = Integer.parseInt(journalNumber);