Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从结果集中删除字符_Java_Mysql_Sql_Jdbc - Fatal编程技术网

在Java中从结果集中删除字符

在Java中从结果集中删除字符,java,mysql,sql,jdbc,Java,Mysql,Sql,Jdbc,我在Java中的SqlResultSet中的值有问题。 此值有一个单引号('),该值无效。正确的值将是逗号(,)。 我想将此单引号(')更改为逗号(,) 错误值示例:7'8 预期结果示例:7,8 我的查询和结果集示例: String query = "SELECT width,height FROM SizeTable;"; rs = st.executeQuery(query); while(rs.next()){ String width = rs.getString(1);

我在Java中的Sql
ResultSet
中的值有问题。 此值有一个单引号('),该值无效。正确的值将是逗号(,)。 我想将此单引号(')更改为逗号(,)

错误值示例:
7'8

预期结果示例:
7,8

我的查询和结果集示例:

 String query = "SELECT width,height FROM SizeTable;";
 rs = st.executeQuery(query);

 while(rs.next()){
    String width = rs.getString(1);
    String height = rs.getString(2); <--- Here is on I get the error
 }

 String insert = "INSERT INTO SizeTable2 (Value1,Value2) VALUES('"+width+"','"+height+"')";

 st.executeUpdate(insert);
String query=“从大小表中选择宽度、高度;”;
rs=st.executeQuery(查询);
while(rs.next()){
字符串宽度=rs.getString(1);

字符串高度=rs.getString(2);当您获得输入时,可以使用
String::replace

String height = rs.getString(2).replace("'", ",");

您必须在循环外声明变量,以便以后使用:

String width = "";
String height = "";
while(rs.next()){
    width = rs.getString(1);
    height = rs.getString(2).replace("'", ",");
}

现在您的sql可能会出现sql注入或语法错误,为了避免这种情况,我建议改用PreparedStatement:

String insert = "INSERT INTO SizeTable2 (Value1,Value2) VALUES(?, ?)";

try (PreparedStatement insert = connection.prepareStatement(insert)) {
    insert.setString(1, width);
    insert.setString(2, height);
    insert.executeUpdate();
}

注意您可以插入空值,因此我建议在插入前检查值:

if(!width.isEmpty() && !height.isEmpty()){
  //prepared statement
}

第二件事,我假设我们使用实数表示宽度和高度,所以为什么要使用字符串,我建议您更改数据类型以避免所有这些问题。

也许您应该告诉我们您遇到了什么错误?我仍然不明白错误是在SQL语句中还是在其结果中请在尝试之前读取我想问更多的问题。用这些代码玩SQL注入攻击吧!尽快学习使用
PreparedStatement
s。我收到一个与此错误字符(')相关的错误,我想将其更改为逗号(,)对于Java,你仍然有相同的答案。Java解决方案在不久之前被你删除了。@PradeepSingh请阅读第二个注释,你能澄清一下你的意思吗?我真的不明白你的意思,除了7'8之外,我还有更多的值。这将更正我字段中的所有无效字符或仅此而已?还感谢SQL注入info!!!;)@JohnStonecutter它只会更正最后一个值,如果您想更改所有内容,则必须将插入部分移动到while循环中,以便替换和插入所有新值,但要小心