首页 > 解决方案 > 如何在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){

    }           
}

标签: javasql

解决方案


你可以试试这个:

  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();

推荐阅读