2014-12-24 90 views
-3

代码:在参数中准备语句?

 java.sql.PreparedStatement pstmt=null; 
     String sql = "UPDATE Person SET address = ? WHERE name = ? "; 
     pstmt=thecon.prepareStatement(sql); 
     String addVar= JOptionPane.showInputDialog("Enter the new address"); 
     String nameVar=JOptionPane.showInputDialog("Enter the Name"); 
     pstmt.setString(1 , addVar); 
     pstmt.setString(2, nameVar); 


     // Tell affected no of rows in table 
     int num = pstmt.executeUpdate(sql); 
     //Step 7: Process the results of the query 
     System.out.println(num + " records updated"); 
     thecon.close(); 
    } catch (SQLException e) { 
     System.out.println("sql problem: "+e); 
    } 

例外:

SQL问题:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在''附近使用正确的语法'? WHERE name =?'在第1行

+2

有问题吗? – philshem

+0

什么是SQL语法的问题? –

+1

您正在使用错误的'executeUpdate'方法,您应该使用没有字符串参数的方法。 –

回答

-1

你可以试试这个: String sql =“UPDATE Person SET address =?WHERE name =?;”;

+0

我尝试但仍然是相同的错误(您有一个SQL语法错误;检查手册,对应于您的MySQL服务器版本的正确的语法使用附近'?WHERE名称=?'在第1行) –

+0

SQL查询是所有好,那里我的SQL语法是不区分大小写的,所以我没有看到任何问题。你能否尝试从人身上找到选择的地址,姓名并看看你得到了什么? – krmanish007

+0

分号不需要(甚至不鼓励),因为您不需要JDBC中的语句分隔符(按规范 - 只执行单个语句)。 –