【问题标题】:Quoted string not properly terminated Exception引用的字符串未正确终止异常
【发布时间】:2014-03-24 17:43:18
【问题描述】:

我正在使用这样的 java servlet 更新 oracle 数据库中的密码

Connection con;
PreparedStatement ps,ps1;
ResultSet rs;
    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    }
    catch (ClassNotFoundException e)
            {
                System.out.println("Drivers Not Found");
            }
try{
    con=DriverManager.getConnection("jdbc:odbc:SharedCryptography", "fyp", "fyp");


}catch(SQLException e1)
{
}
String query="UPDATE tbGroup SET GPassword='"+mypassword+"' where GName='"+GroupNamee+"' and OEmail='"+OwnerId+"'";
java.sql.Statement stmt = con.createStatement();
stmt.executeUpdate(query);

但它给出了 java.sql.SQLException: [Oracle][ODBC][Ora]ORA-01756: 引用的字符串未正确终止

我做错了什么吗?请帮忙

【问题讨论】:

  • 您的某个值中是否包含单引号字符?
  • @rgettman 你说的是哪个值?
  • 当程序员没有正确转义查询参数时会发生这种情况。你真的应该使用准备好的语句
  • @BackSlash 什么意思?请帮忙

标签: java servlets jdbc


【解决方案1】:

您应该绝对避免在 SQL 语句中进行字符串连接。你会遇到各种安全和稳定性问题。您的问题只需使用准备好的语句即可解决:

String sql="UPDATE tbGroup SET GPassword=? where GName=? and OEmail=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, myPassword);
ps.setString(2, groupName);
ps.setString(3, ownerId);
ps.executeUpdate();

如果你这样做,你的参数中没有“'”或“%”或“_”或“会导致任何问题。或者你可以尝试转义你的字符,但是为什么要麻烦 - PS 方法不仅更多健壮且更易于阅读,通常也更高效。

有关安全问题的一般描述,请参阅:https://www.owasp.org/index.php/SQL_injection

【讨论】:

    【解决方案2】:

    您的一个变量(可能是密码)中有一个引号或分号。由于您通过字符串连接构建查询,因此您很容易受到 SQL 注入攻击。看起来你不小心通过注射攻击了自己。如果你有一个正确格式化的变量,你可能会对你的数据库造成相当大的损害。

    请使用参数化查询

    PreparedStatement stmt = con.prepareStatement("UPDATE tbGroup SET GPassword= ? where GName= ? and OEmail=?" )
    
    stmt.setString(1, mypassword);
    ...
    stmt.executeUpdate();
    

    更多详情请看这里

    https://www.owasp.org/index.php/Preventing_SQL_Injection_in_Java

    【讨论】:

    • 在这个 PreparedStatement 中 stmt = con.prepareStatement("UPDATE tbGroup SET GPassword= ? where GName= ? and OEmail=?" ) 从哪里给第二个和第三个问号赋值?
    • 是的?是您稍后使用setString( index, value) 设置的变量的占位符,出于某种愚蠢的原因,编号从 1 而不是 0 开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2015-08-09
    相关资源
    最近更新 更多