错误如下
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
check the manual that corresponds to your MySQL server version for the right syntax to use near '-BookInfo bookinfo0_ where bookinfo0_.isbn='0102'' at line 1
错误代码
public int findBookPriceByIsbn(String isbn) {
String sql = "select b.price from BookInfo b where b.isbn = ?";
System.out.println("sql:"+sql);
Query query = getSession().createQuery(sql);
query.setString(0, isbn);
int bi = (int) query.uniqueResult();
System.out.println("hahah"+bi);
return bi;
}
错误原因
跟数据库没有连接上,
一般测试连接正不正常都是通过以下代码实现的
package cn.com.dao.com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.dao.service.BookShopService;
public class Test {
public static void main(String[] args) {
ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource da=ioc.getBean(DataSource.class);
try {
System.out.println(da.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我运行这个代码,没有报错,也返返回连接的信息,但是
如果把数据库关闭,也测试正常
这个真的是害人不浅啊,但是我不知道为什么?
只有运行这个代码,数据库能够自动生成映射文件对应的表,就说明Spring整合Hibernate的时候连接数据库是没有问题的