"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数据的变量???
但是他会报错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数据的变量???
你直接执行看看。不行的话加上日期转换的函数。
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语句的
可能我没说清楚
test是我要从另外一张表中获得的日期值
然后再将test的值付给 b_outbooks这张表里的obotime我做过了几次尝试
比如再用 update给表中某字段名赋值时 需要加上转义符 \"
我知道 sql变量用的是'而不是",但是java中没有\'转义符
我也在程序中试验过了,用变量给字段赋值时可以用\"我单独把这段程序拿出来试了一下
"update b_outbooks set obid = 5 where obotime = '"+ test +"'"
是可以执行的~而且不用在where前加 "+" 还是谢谢大家的帮助了。
//开始还书操作
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");
谢谢大家帮忙差错~
能改正的一定追加分数 谢谢~
修改之前的
'obotime = '2010-06-11"
修改之后的自己好好看看!