Java 将处理结果存储在表中
我执行了一个代码,该代码用于显示WampServer上“更新操作”列、表、事件、数据库、基本关系”字段内容中的指定信息。在我的表中,我添加了另一列,我将在其中存储显示的信息。我的目标是显示整个列的结果,并将其存储在新的列顺序中Java 将处理结果存储在表中,java,mysql,jdbc,phpmyadmin,sql-update,Java,Mysql,Jdbc,Phpmyadmin,Sql Update,我执行了一个代码,该代码用于显示WampServer上“更新操作”列、表、事件、数据库、基本关系”字段内容中的指定信息。在我的表中,我添加了另一列,我将在其中存储显示的信息。我的目标是显示整个列的结果,并将其存储在新的列顺序中 try { String Sql="Select Update_Action from incident where id_incident ='"+jTextField3.getText()+"' and Status like
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\u事件,如“%”)!您需要使用筛选器主键(我认为它是id\u事件)
您必须执行此查询
String query = "update incident set intervenants = ? where id_incident = ? ;
preparedStmt.setString(1,nom);
preparedStmt.setInt(2,rs.getInt("id_incident"));
附言。
1) 使用单一更新是不好的。例如,最好的方法是使用批量更新
2) 为什么选择和更新时使用不同的连接?都一样一个小TIP,考虑看一看。最好是避免sql注入,谢谢。我试过你的答案,但没有。同样的问题我执行了一个小代码,它允许我从一列的一个字段中获取一些信息,并将其存储在同一表中另一列的另一个字段中。但是,对于目标列的其他字段,它存储第一个收件人字段的结果。catch的输出异常(异常e){//JOptionPane.showMessageDialog(this,e);}此消息显示:字符串索引超出范围:-1