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

如何在另一个函数中使用带有参数的函数,并在代码 java 中的条件 sql 中使用它,如下所示:


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

下面是我的 Java 代码。我有两个功能;如何在函数getid中使用函数updateDatefin?


// get id of line date

public 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 insertion

public 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){


    }           

}


潇潇雨雨
浏览 107回答 3
3回答

斯蒂芬大帝

您只需将要使用的值发送到函数:query = "update date set date_fin = '" + dates + "' where id ='" + getid(date_fin) + "'";如果这不是您想要的值,只需date_fin在此行中替换为您需要的参数即可。

神不在的星期二

对于这种情况,您可以通过两种方式调用函数:只需创建一个新变量并调用函数并将该变量传递给查询。字符串 getID=getid(date_fin);字符串查询;query = "更新日期集 date_fin = '"+dates+"' where id ='"+getID+"'";在查询中直接调用函数字符串查询;query = "更新日期集 date_fin = '"+dates+"' where id ='"+getid(date_fin)+"'";注意:您必须在 getid 函数中传递一个参数。

jeck猫

你可以试试这个:  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();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java