简体   繁体   中英

How to set value of textfield into the table column in java

how to set value of method in database using java code.

mycode

// for getting value code start here

 public void MethodOutputid(int num1)

          String sql="Select MAX(OUTPUT_ID) from OUTPUT_CONFIGURATION";
          PreparedStatement psmt=conn.prepareStatement(sql);
          rs=psmt.executeQuery();
          while(rs.next())
          {
              num1=rs.getInt(1);
          }
                num1=num1+1;               
                txtoutputid1.setText(""+num1);
             }      

//// for getting value end here

// for fetching value of outputid and set it into database start here

private boolean AddButton(ActionEvent event) throws IOException {

                String sql = "insert into INPUT_CONFIGURATION (FILE_NAME,OUTPUT_ID)value(?,?)";

                String filename = txtfilename.getText();                    
                int outputnum=0;
                MethodOutputid(outputnum); 
                PreparedStatement ps = conn.prepareStatement(sql);  
                ps.setString(1, filename);                     

                if(Integer.parseInt(txtoutputid1.getText())==outputnum)
                {
                   ps.setInt(2, outputnum); 
                }

                ps.executeUpdate();
               }

// for fetching value of outputid and set it into database end here

here i am getting the num1 value from the method MethodOutputid(int num1) as mentioned above and i am setting that value into textfield named txtoutputid1 inside the MethodOutputid(int num1) method

now i want to insert the value of txtoutputid1 (a textfield) into table INPUT_CONFIGURATION in the OUTPUT_ID column.

i tried as

if(Integer.parseInt(txtoutputid1.getText())==outputnum)
{
      ps.setInt(2, outputnum); 
}

i tried this, but showing error as No value specified for parameter

wht should i do to insert value of that textfield (as txtoutputid1 mentioned in MethodOutputid(int num1)) in to the OUTPUT_ID column which is in INPUT_CONFIGURATION.

Your way of coding is not Recommended.

And About Error: You have specified only two question marks in the query.So

You have to re write the line

ps.setInt(9, outputnum); // You are not having 9 columns in your query

to

ps.setInt(2, outputnum);

You should set guaranteed value to second argument. Otherwise, prepared statement cannot be compiled. This argument may be left without a value depending of result of if statement in your code:

if(Integer.parseInt(txtoutputid1.getText())==outputnum)
{
    ps.setInt(2, outputnum); 
}

This's your mistake. Setting the value should be outside the condition. Leave default value or throw an exception in the case of necessary statement with comparison fail:

if(Integer.parseInt(txtoutputid1.getText()) == outputnum)
{
    throw new RuntimeException("error message");
}

ps.setInt(2, outputnum);

This should be executed without this exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM