为了账号安全,请及时绑定邮箱和手机立即绑定

如何在java中的另一个函数中使用带有参数的函数

如何在java中的另一个函数中使用带有参数的函数

潇潇雨雨 2022-11-02 16:55:18
如何在另一个函数中使用带有参数的函数,并在代码 java 中的条件 sql 中使用它,如下所示:query = "update date set date_fin = '"+dates+"' where id ='"+getid()+"'";下面是我的 Java 代码。我有两个功能;如何在函数getid中使用函数updateDatefin?// get id of line datepublic int getid(String date){    String datedb;    datedb = date;    int h = 0;    try{       Connection con=ConnectDB();       Statement stmt=con.createStatement();       ResultSet rs=stmt.executeQuery("select id  from date where date_debut = '"+datedb+"' ");       while(rs.next()) {           h =rs.getInt("id");       }    } catch (SQLException | IOException ex) {        Logger.getLogger(Recognizer.class.getName()).log(Level.SEVERE, null, ex);    }    return h;}//date time insertionpublic void updateDatefin(String date_fin){    String dates = null;    dates = date_fin;    try{          Connection conn=ConnectDB();        String query;        query = "update date set date_fin = '"+dates+"' where id ='"+getid()+"'";        // create the mysql insert preparedstatement        PreparedStatement preparedStmt = conn.prepareStatement(query);        // execute the preparedstatement        preparedStmt.execute();        //JOptionPane.showMessageDialog(null, "Data added");    }catch(SQLException | HeadlessException e){        System.out.println("errrr");    }catch(IOException iox){    }           }
查看完整描述

3 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

您只需将要使用的值发送到函数:

query = "update date set date_fin = '" + dates + "' where id ='" + getid(date_fin) + "'";

如果这不是您想要的值,只需date_fin在此行中替换为您需要的参数即可。



查看完整回答
反对 回复 2022-11-02
?
神不在的星期二

TA贡献1963条经验 获得超6个赞

对于这种情况,您可以通过两种方式调用函数:

  1. 只需创建一个新变量并调用函数并将该变量传递给查询。

字符串 getID=getid(date_fin);

字符串查询;

query = "更新日期集 date_fin = '"+dates+"' where id ='"+getID+"'";

  1. 在查询中直接调用函数

字符串查询;

query = "更新日期集 date_fin = '"+dates+"' where id ='"+getid(date_fin)+"'";

注意:您必须在 getid 函数中传递一个参数。


查看完整回答
反对 回复 2022-11-02
?
jeck猫

TA贡献1909条经验 获得超7个赞

你可以试试这个:


  query = "update date set date_fin = ? where id = ?";

  // create the mysql insert preparedstatement

  PreparedStatement preparedStmt = conn.prepareStatement(query);

  preparedStmt.setString(1, dates);

  preparedStmt.setString(2, getid(date_fin));

  // execute the preparedstatement

  preparedStmt.execute();


查看完整回答
反对 回复 2022-11-02
  • 3 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信