【问题标题】:store the results of a treatment in table将处理结果存储在表中
【发布时间】:2015-06-19 20:58:11
【问题描述】:

我执行了一段代码,该代码用于显示 WampServer 上“update_action”列中的字段内容中的指定信息、表“事件”和数据库“base_rapport_tt”。在我的表格中,我添加了另一列,我将在其中存储显示的信息。我的目标是显示整个列的结果并将其存储在新的列顺序中。

try
    {   
        String Sql="Select Update_Action from   incident    where id_incident ='"+jTextField3.getText()+"' and Status like 'Closed'";
        con = getConnection("jdbc:mysql://localhost:3306/base_rapport_tt","root","");
        stmt=con.createStatement();
        rs=stmt.executeQuery(Sql);

        while(rs.next()) {
            str=rs.getString("update_Action");

            while(!"".equals(str)){
              int debut=str.indexOf('(')+1;
              int fin=str.indexOf(')',debut);
              nom += " "+str.substring(debut,fin);
              str=str.substring(fin+1,str.length()); 
              nom+=", "; 
            } 
         }
    } 
    catch (Exception e) {
       JOptionPane.showMessageDialog(this,e);
   }

我尝试使用此代码:

try
{ 
   String Sql="Select Update_Action,id_incident from   incident
where Status like 'Closed'";
  con = getConnection("jdbc:mysql://localhost:3306/base_rapport_tt","root","");
 stmt=con.createStatement();
 rs=stmt.executeQuery(Sql);

 while(rs.next()) {
     str=rs.getString("update_Action");
     nom="";
     while(!"".equals(str)){
        int debut=str.indexOf('(')+1;
        int fin=str.indexOf(')',debut);
        nom += " "+str.substring(debut,fin);
        str=str.substring(fin+1,str.length()); 
        nom+=", ";  

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/base_rapport_tt", "root", "");
        String query = "update incident set intervenants = ? where id_incident like '%'";
        java.sql.PreparedStatement preparedStmt = conn.prepareStatement(query);
        preparedStmt.setString(1,nom);
        preparedStmt.executeUpdate();
        conn.close();    
      }
   }
}
catch (Exception e) 
{
           //JOptionPane.showMessageDialog(this,e);
}

收件人字段第一列的结果很好。但问题是其他字段是前者的结果。

谢谢。

【问题讨论】:

  • 一个小提示,考虑看看PreparedStatement。最好从 sql 注入中拯救自己

标签: java mysql jdbc phpmyadmin sql-update


【解决方案1】:

问题出在

 String query = "update incident set intervenants = ? where id_incident like '%'";

此查询更新所有行id_incident like '%')!您需要使用过滤器主键(我认为它 id_incident )!

你必须执行这个查询

String query = "update incident set intervenants = ? where id_incident = ? ;
preparedStmt.setString(1,nom);
preparedStmt.setInt(2,rs.getInt("id_incident")); 

附: 1) 而且单次更新不好用。最好的方法是使用 bulk update 例如 java - Multipile update statements in MySql
2)为什么选择和更新使用不同的连接?都是一样的!

【讨论】:

  • 谢谢。我试过你的答案,但没有。同样的问题
  • 我执行了一个小代码,它允许我从列的字段中获取一些信息并将其存储在同一个表中另一列的另一个字段中。倾倒第一个字段就完成了。但是,对于目标列的其他字段,它存储第一个收件人字段的结果。
  • catch 输出异常 (Exception e) { //JOptionPane.showMessageDialog(this,e); }
  • 出现此消息:字符串索引超出范围:-1
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2020-07-05
  • 2021-09-11
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
相关资源
最近更新 更多