一個書城后臺系統練手,連接了sql server資料庫,連接時沒問題的
更新圖書操作有問題 把引數設定為靜態的時候可以更新資料庫里面的內容
用?占位符的時候就報錯了,,求大佬指點!

測驗源代碼
public void updateBook() {
bookDao.updateBook(new Book(5,"mxyisdog","dhxdhxhhh",new BigDecimal(32),123,44,"static/images"));
}
bookDao里面的方法
public int updateBook(Book book) {
String sql="update t_book set name=?,author=?,price=?,sales=?,stock=?,img_path=? where id=?";
return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
}
IDEA報錯日志
三月 20, 2020 4:58:52 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
資訊: {dataSource-1} inited
java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: ',' 附近有語法錯誤。 Query: update t_book set name=?,author=?,price=?,sales=?,stock=?,img_path=? where id=? Parameters: [mxyisdog, dhxdhxhhh, 32, 123, 44, static/images, 5]
at org.apache.commons.dbutils.QueryRunner.rethrow(QueryRunner.java:542)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:599)
at com.atguigu.dao.impl.BaseDao.update(BaseDao.java:21)
at com.atguigu.dao.impl.BookDaoImpl.updateBook(BookDaoImpl.java:25)
at com.atguigu.test.BookDaoImplTest.updateBook(BookDaoImplTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Process finished with exit code 0
uj5u.com熱心網友回復:
可能是由于imgpath的斜杠引起的,把所有的需要用字串替換的問號前后加引號uj5u.com熱心網友回復:
哥,不行啊,加了引號之后報錯 com.microsoft.sqlserver.jdbc.SQLServerException: 關鍵字 'FROM' 附近有語法錯誤。 Query: update t_book set name='?
uj5u.com熱心網友回復:
你試試將每個逗號后面添加一個空格 set name=?, author=? 類似這樣,試試uj5u.com熱心網友回復:
我試了下,底層的sql沒問題
你把static/images換成其他沒用斜杠的字串試試,如果不報這個錯了,那就是斜杠的問題。
update方法那里打斷點,單步進去看看引數替換后的sql是不是對的。例外里已經提示了錯誤的行數。你自己去看看吧。
除錯前記得先把commons.dbutils包的原始碼下載下來。https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils
注意版本
至于加空格加引號這些就別試了,沒意義。
uj5u.com熱心網友回復:
前輩 斜杠替換了,斷點頁打了,貌似還是不行。。。。

uj5u.com熱心網友回復:
前輩 不行害!
uj5u.com熱心網友回復:
你仔細檢查下你的getter。我看你資料庫里的型別好像都是字串型別的,把book類成員變數的型別都換成和資料庫的對應。把這些因素都排除一下。語法錯誤,應該就是設定值的時候出錯了,從這方面下手。uj5u.com熱心網友回復:
前輩 感覺無解了 初入Java web這編譯器就這樣欺負小白




uj5u.com熱心網友回復:
找不到問題就一個引數一個引數的試唄。控制變數法。就一個變數用通配符,其他都設固定值。
uj5u.com熱心網友回復:
前輩 貌似問題找到了 但是。。。解決不了
。。。

uj5u.com熱心網友回復:
我在我mysql上試了一下,沒問題的。。。。可以成功更新。Book book = new Book(5, "mxyisdog", "dhxdhxhhh", 32, 123, 77, "static/images");
try
{
String sql = "update t_book set name=?,author=?,price=?,sales=?,stock=?,img_path=? where id=?";
QueryRunner qRunner = new QueryRunner();
qRunner.update(conn, sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(),
book.getStock(), book.getImgPath(), book.getId());
} catch (SQLException e)
{
e.printStackTrace();
}

uj5u.com熱心網友回復:
我放棄了!不如我直接換成mysql用
uj5u.com熱心網友回復:
update陳述句傳參的時候,第一個是sql,第二個是引數,如果是多個引數,可以用陣列代替這是我自己做的時候寫的方法 沒有問題
public void register(User user) {
String sql = "insert into tb_user(username,password,name,email,telephone,birthday,sex)"
+ " values(?,?,?,?,?,?,?)";
Object[] params = {
user.getUsername(),
user.getPassword(),
user.getName(),
user.getEmail(),
user.getTelephone(),
user.getBirthday(),
user.getSex()
};
//新增資料到資料庫
jdbcTemplate.update(sql,params);
}
jdbcTemplate.update(sql,params);
uj5u.com熱心網友回復:
前輩 貌似sql server真的有毒
uj5u.com熱心網友回復:
author后面的問號都沒寫能不錯嗎。直接用spring管理mybatis不香么。。怎么還在用jdbc模板啊。。uj5u.com熱心網友回復:
還有你傳兩個引數時 資料型別都寫錯了好嗎,sales跟stock都是整形,你寫成了字串了啊,你資料庫也是整型的,你傳個字串能不錯么。。 String sql="update t_book set name=?,author='ddd',price=?,sales=?,stock=44,img_path=? where id=? ";這才是正確的寫法啊,整型是沒兩點的uj5u.com熱心網友回復:
逗號是中文鍵盤吧轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/107301.html
標籤:Java EE
上一篇:Lambda 呼叫容器中物件的方法;格式 類名::普通方法
下一篇:JAVA簡易計算器
