当前位置:编程学习 > JAVA >>

MyEclipse连接MySQL出错???

HTTP Status 500 - 

--------------------------------------------------------------------------------
type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

javax.servlet.ServletException: org.hibernate.exception.SQLGrammarException: could not execute query
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.qdu.sun.BO.FormFilter.doFilter(FormFilter.java:75)


root cause 

org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.loader.Loader.doList(Loader.java:2235)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
org.hibernate.loader.Loader.list(Loader.java:2124)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
com.qdu.sun.hibernate.UserDAO.findByExample(UserDAO.java:67)
com.qdu.sun.BO.LoginBO.validate(LoginBO.java:20)
com.qdu.sun.struts.action.LoginAction.execute(LoginAction.java:43)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.qdu.sun.BO.FormFilter.doFilter(FormFilter.java:75)


root cause 

java.sql.SQLException: Unknown column 'this_.id' in 'field list'
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2901)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1584)
com.mysql.jdbc.Connection.serverPrepare(Connection.java:4932)
com.mysql.jdbc.Connection.prepareStatement(Connection.java:1312)
com.mysql.jdbc.Connection.prepareStatement(Connection.java:1284)
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
org.hibernate.loader.Loader.doQuery(Loader.java:696)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
org.hibernate.loader.Loader.doList(Loader.java:2232)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
org.hibernate.loader.Loader.list(Loader.java:2124)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
com.qdu.sun.hibernate.UserDAO.findByExample(UserDAO.java:67)
com.qdu.sun.BO.LoginBO.validate(LoginBO.java:20)
com.qdu.sun.struts.action.LoginAction.execute(LoginAction.java:43)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.qdu.sun.BO.FormFilter.doFilter(FormFilter.java:75)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.





hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/User</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">MyEclispe MySQL</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="show_sql">true</property>
<mapping resource="com/qdu/sun/hibernate/User.hbm.xml" />

</session-factory>

</hibernate-configuration>





User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.qdu.sun.hibernate.User" table="user" catalog="login">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="200" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="20" not-null="true" />
        </property>
    </class>
</hibernate-mapping>





HibernateSessionFactory.java
package com.qdu.sun;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

static {
     try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
    }
    private HibernateSessionFactory() {
    }

/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

        return session;
    }

/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
return configuration;
}

}
MySQL MyEclipse 异常 Hibernate --------------------编程问答-------------------- 通过工具生成的实体配置文件问题很多的,<generator class="increment" />改为<generator class="native" />看看表里的数据库字段名是否和你的实体属性相同。 --------------------编程问答-------------------- id配置的和db里的不一致吧。。 --------------------编程问答-------------------- <generator class="increment" />改为<generator class="native" />还是不行呐,运行还是一样的错误。数据库字段名跟实体属性是一样的。 --------------------编程问答-------------------- 截图看看,数据库字段名是否有空格啥的
org.gjt.mm.mysql.Driver改为com.mysql.jdbc.Driver --------------------编程问答-------------------- 改了也不行啊
数据库截图如下: --------------------编程问答-------------------- 看不到你的id属性在哪里 --------------------编程问答-------------------- 你的意思是因为我的user表里没有id这个属性才会导致这样的错误码?但我加入了id,并设为主键。运行还是出错。root cause   java.sql.SQLException: Table 'login.user' doesn't exist
--------------------编程问答-------------------- 这个问题解决了,谢啦
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,