"update b_outbooks set obid = 2" +"where  obotime = "+ "\"" + test +"\""判断条件是  字段名为obotime的值等于  变量 test
但是他会报错You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'obotime = "2010-04-01"' at line 1
test我定义的是String的。obotime是date类型的...
是不是因为类型不同才报的错,如果是的话 应该怎么定义一个存放mysql中date数据的变量???

解决方案 »

  1.   

    update b_outbooks set obid = 2" + "where  obotime ="+ "'test'"是‘ 而不是 “
      

  2.   

    "update b_outbooks set obid = 2" +"where  obotime = "'" + test +"'"首先 sql里没有 "" ;update b_outbooks set obid = 2 where  obotime = '2010-04-01';
    你直接执行看看。不行的话加上日期转换的函数。
      

  3.   

    "update b_outbooks set obid = 2 " +" where  obotime = '"+test +"'"
      

  4.   

    where 前面没有空格所以错误了 帮你改装了下"update b_outbooks set obid = 2" +" where  obotime = '"+ test +"'"
      

  5.   

    其实这种问题的解决方法就是你把构建的sql语句直接放到mysql下看看能不能运行,这里明显是你的时间类型不对应该是'不是"吧一般构建sql语句还是用PreparedStatement比较好,只是个建议,看看别的高手怎么回答
            private PreparedStatement pstat = null;
    private ResultSet rs = null;
    private Connection conn = null;        conn = new ConnDB().getConn();
    pstat = conn.prepareStatement("select * from dalilyreport where ri=?");
    pstat.setString(1, date);
    rs = pstat.executeQuery();
    我基本上是这么构建sql语句的
      

  6.   

    建议你这个好用之后,再去看一下preparedStatement
      

  7.   

    "update b_outbooks set obid = 2" +"where  obotime = "+ "'" + java.sql.Date.ValueOf("2010-04-01") +"'"
      

  8.   

    额~
    可能我没说清楚
    test是我要从另外一张表中获得的日期值
    然后再将test的值付给 b_outbooks这张表里的obotime我做过了几次尝试
    比如再用 update给表中某字段名赋值时  需要加上转义符 \"
    我知道 sql变量用的是'而不是",但是java中没有\'转义符
    我也在程序中试验过了,用变量给字段赋值时可以用\"我单独把这段程序拿出来试了一下
    "update b_outbooks set obid = 5 where  obotime = '"+ test +"'"
    是可以执行的~而且不用在where前加 "+" 还是谢谢大家的帮助了。
      

  9.   


                    //开始还书操作
                    if (dbc.executeUpdate(
                            "UPDATE b_OutBooks SET Obstate='是',Obkeep='否' WHERE Obid = " +
                            Integer.parseInt(txtBid.getText()) + " AND Ouid = " +
                            Integer.parseInt(txtUid.getText()) + "AND Obotime = '" +
                            time + "'") &&
                        dbc.
                        executeUpdate(
                                "UPDATE b_Users SET Uyue=Uyue+(SELECT Bprice FROM Books WHERE Bid = " +
                                Integer.parseInt(txtBid.getText()) +
                                ") WHERE Uid = " +
                                Integer.parseInt(txtUid.getText())) &&
                        dbc.
                        executeUpdate(
                                "UPDATE b_Books SET Bsum=Bsum+1 WHERE Bid =" +
                                Integer.parseInt(txtBid.getText()))) {
                        javax.swing.JOptionPane.showMessageDialog(this, "还书成功!");                } else {
                        javax.swing.JOptionPane.showMessageDialog(this,
                                "还书失败!请重试。");
                    }
                这个是程序中出错的代码
    他报错是you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'obotime = '2010-06-11"at line 1
    test是之前time = rs.getString("Obotime");
    谢谢大家帮忙差错~
    能改正的一定追加分数  谢谢~
      

  10.   

    'obotime = "2010-04-01"'
    修改之前的
    'obotime = '2010-06-11"
    修改之后的自己好好看看!
      

  11.   

    某互联网公司招聘my sql DBA开发工程师
      

  12.   

    这种问题放到mysql下运行下不就解决了?
      

  13.   

    "update b_outbooks set obid = 2 where  obotime = '"+ test +"'"
      

  14.   

    楼主,只要是SQL语句报错,你就先把excute里面的语句打印出来,执行一次赋值到SQL开发工具里面调试,保证比你在这边问要快得多....