java - 将处理结果存储在表中

标签 java mysql jdbc phpmyadmin sql-update

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

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

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

谢谢。

最佳答案

问题出在

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

此查询更新所有行(id_incident 如“%”)!您需要使用过滤器主键(我认为是 id_incident )!

您必须执行此查询

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

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

关于java - 将处理结果存储在表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624134/

相关文章:

java - java 编程中构造函数未定义

java - 当我启动类(class)时,值没有被传递

mysql - SQL:没有任何选择的简单 if-then-else 语句

java - 从 Spring JdbcTemplate 的 queryForObject 方法返回泛型类型

java - 无法获取通知表单扫描仪

java - JSON 解析 : Error parsing data org. json.JSONException:输入字符 0 的结尾

mysql - 如何将两个表合并为一个带有表源的新字段?

jquery - SQL数据库设计外键

java - 使用 wasnull() 处理 java 结果集中的 null

java - POST 与 JDBC