java - 如何在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){
}
}
解决方案
你可以试试这个:
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();
推荐阅读
- php - 如何使正则表达式中需要的字符?
- dsc - 如何使文件与所需的状态配置同步
- python - 如何将 Pandas 中的函数应用于同一行中的不同单元格满足条件的每一行中的单元格?
- ios - iOS 13 中未调用应用程序委托方法
- python - Urllib2 突然不工作了。请求时间
- laravel - 如何将变量从控制器传递到视图而不刷新 Laravel 中的网页?
- javascript - 为什么 eval() 不适用于导入的函数?
- c - 我在使用 strcat 时遇到分段错误。请告诉我出了什么问题
- python - 将 Anaconda 安装从一个用户帐户转移到另一个用户帐户
- laravel-nova - Laravel Nova 在 BelongsToMany 子表中显示计算字段