User.javapackage onlyfun.caterpillar;public class User {
private String id;
private String name;
private char sex;
private int age; public int getAge() {
return age;
} public String getId() {
return id;
} public String getName() {
return name;
} public char getSex() {
return sex;
} public void setAge(int i) {
age = i;
} public void setId(String string) {
id = string;
} public void setName(String string) {
name = string;
} public void setSex(char c) {
sex = c;
}
}hibernate.cfg.xml
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- 顯示實際操作資料庫時的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,這邊設定的是MySQL -->
<!--<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>-->
<!-- SQL方言,這邊設定的是SQLServer -->
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<!-- JDBC驅動程式 -->
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<!-- JDBC URL -->
<!--<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property> -->
<!-- JDBC URL -->
<property name="connection.url">jdbc:microsoft:sqlserver://192.168.1.51:1433;DatabaseName=audit</property>
<!-- 資料庫使用者 -->
<property name="connection.username">peizhiyou</property>
<!-- 資料庫密碼 -->
<property name="connection.password">admin123</property>
<property name="database.schema">dbo</property>
<property name="database.catalog">Northwind</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- 物件與資料庫表格映射文件 -->
<mapping resource="User.hbm.xml"/> </session-factory> </hibernate-configuration>User.hbm.xml<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="onlyfun.caterpillar.User" table="USER"> <id name="id" type="string" unsaved-value="null">
<column name="user_id" sql-type="char(32)" />
<generator class="uuid.hex"/>
</id> <property name="name" type="string" not-null="true">
<column name="name" length="16" not-null="true"/>
</property> <property name="sex" type="char"/> <property name="age" type="int"/> </class> </hibernate-mapping>错误信息为:
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as age from USER user0_
Hit uncaught exception net.sf.hibernate.exception.GenericJDBCException
net.sf.hibernate.exception.GenericJDBCException: Could not execute query
at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:80)
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4131)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1557)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
at HibernateTest.query(HibernateTest.java:27)
at HibernateTest.main(HibernateTest.java:9)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'USER' 附近有语法错误。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:89)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:880)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:273)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
... 4 more
苦等
private String id;
private String name;
private char sex;
private int age; public int getAge() {
return age;
} public String getId() {
return id;
} public String getName() {
return name;
} public char getSex() {
return sex;
} public void setAge(int i) {
age = i;
} public void setId(String string) {
id = string;
} public void setName(String string) {
name = string;
} public void setSex(char c) {
sex = c;
}
}hibernate.cfg.xml
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- 顯示實際操作資料庫時的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,這邊設定的是MySQL -->
<!--<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>-->
<!-- SQL方言,這邊設定的是SQLServer -->
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<!-- JDBC驅動程式 -->
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<!-- JDBC URL -->
<!--<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property> -->
<!-- JDBC URL -->
<property name="connection.url">jdbc:microsoft:sqlserver://192.168.1.51:1433;DatabaseName=audit</property>
<!-- 資料庫使用者 -->
<property name="connection.username">peizhiyou</property>
<!-- 資料庫密碼 -->
<property name="connection.password">admin123</property>
<property name="database.schema">dbo</property>
<property name="database.catalog">Northwind</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- 物件與資料庫表格映射文件 -->
<mapping resource="User.hbm.xml"/> </session-factory> </hibernate-configuration>User.hbm.xml<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="onlyfun.caterpillar.User" table="USER"> <id name="id" type="string" unsaved-value="null">
<column name="user_id" sql-type="char(32)" />
<generator class="uuid.hex"/>
</id> <property name="name" type="string" not-null="true">
<column name="name" length="16" not-null="true"/>
</property> <property name="sex" type="char"/> <property name="age" type="int"/> </class> </hibernate-mapping>错误信息为:
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as age from USER user0_
Hit uncaught exception net.sf.hibernate.exception.GenericJDBCException
net.sf.hibernate.exception.GenericJDBCException: Could not execute query
at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:80)
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4131)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1557)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
at HibernateTest.query(HibernateTest.java:27)
at HibernateTest.main(HibernateTest.java:9)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'USER' 附近有语法错误。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:89)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:880)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:273)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
... 4 more
苦等
解决方案 »
- 如何用java实现类似qq的登陆
- 问个关于dao层的问题~
- 关于绘图效率的问题
- 请各位大虾帮忙解决个难题!!!!!!在线!~~~~~~
- 我在JB里面写ejb,用的数据库是mysql,服务器是weblogic。但是我在部署ejb出了问题,请大家帮我想一下。
- 小弟刚开始接触JAVA,请教一些问题,大家随便指教,你们都会的!
- Eclipse3 M7不能正常运行问题!急!在线等!
- xml-rpc xerces httpunit axis 都是干什么用的?
- 使用Jbuilder8+Weblogic7+Oracle9i部署EJB的一个问题!
- 在Java里,有没有直接对XML文件进行操作的函数?
- 有关连接池的问题!对于连不同的数据库,Context对象内部具体是什么值?
- 砸锅卖铁高分求助:自定义标记
http://community.csdn.net/Expert/topic/4847/4847668.xml
解决办法:
1、把查询目标换成表的名字
Session session = sessionFactory.openSession();
List users = session.find("from User");
2、使用HQL
Session session = sessionFactory.openSession();
List users = session.createQuery("from onlyfun.caterpillar.User");
改表名为USERS,其他做相应修改