2014-03-24 105 views
-2

我是用一个Java servlet这样引号的字符串没有正确结束异常

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

在Oracle DATABSE更新密码,但它给值java.sql.SQLException:[甲骨文] [ODBC] [奥拉] ORA- 01756:引用字符串没有正确结束

难道我做错了什么在里面请帮助

+1

做一你的价值观中有单引号字符吗? – rgettman

+0

@rgettman你在谈论什么价值? – user3445854

+2

Thi当程序员没有正确地逃避查询参数时会发生什么。你应该真的使用准备好的语句 – BackSlash

回答

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

1

你的一个变量(可能是密码)的有报价或它分号?由于您通过字符串连接构建查询,因此您很容易受到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

+1

在此PreparedStatement stmt = con.prepareStatement(“UPDATE tbGroup SET GPassword =?where GName =?and OEmail =?”)from where为第二和第三个问号赋予价值? – user3445854

+1

是的?是您随后用setString(index,value)设置的变量的占位符。由于某些愚蠢的原因,编号从1开始而不是从0开始。 – dkatzel

相关问题