简体   繁体   English

如何在Java中的表列中设置文本字段的值

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

how to set value of method in database using java code. 如何使用Java代码在数据库中设置方法的值。

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 //用于获取outputid的值并将其设置到数据库中

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 //用于获取outputid的值并将其设置到数据库端

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 这里我收到从方法MethodOutputid(INT NUM1)NUM1值如上所述,并且我设置该值转换成文本字段命名txtoutputid1MethodOutputid(INT NUM1)方法中

now i want to insert the value of txtoutputid1 (a textfield) into table INPUT_CONFIGURATION in the OUTPUT_ID column. 现在我想将txtoutputid1(文本字段)的值插入到OUTPUT_ID列中的表INPUT_CONFIGURATION中。

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. 我该怎么做才能将该文本字段的值(如MethodOutputid(int num1)中提到的txtoutputid1)插入INPUT_CONFIGURATION中的OUTPUT_ID列。

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 //您的查询中没有9列

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语句的结果,该参数可能没有值:

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. 这应该在没有此异常的情况下执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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