在将值插入表中时出错

时间:2016-11-08 07:37:48

标签: java xml spring hibernate javamail

我正在尝试使用扫描仪将值插入表中。它需要输入,但它会显示这些错误。

Hibernate: insert into MAIL (MAIL_ID, TOADDRESS, FROM, subject, message, attachements, date, snt_status) values (default, ?, ?, ?, ?, ?, ?, ?)
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.RML.Model.MailBean]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.RML.Model.MailBean]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
    at com.RML.Dao.Impl.MailDaoImpl.save(MailDaoImpl.java:36)
    at com.RML.Main.Main.main(Main.java:97)
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [com.RML.Model.MailBean]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:64)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
    at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:697)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    ... 4 more
Caused by: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "FROM" at line 1, column 39.
    at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.client.am.ClientConnection.prepareStatement(Unknown Source)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:54)
    ... 21 more
Caused by: ERROR 42X01: Syntax error: Encountered "FROM" at line 1, column 39.
    at org.apache.derby.client.am.ClientStatement.completeSqlca(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
    at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
    at org.apache.derby.client.am.ClientStatement.readPrepareDescribeOutput(Unknown Source)
    at org.apache.derby.client.am.ClientPreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.ClientPreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
    at org.apache.derby.client.am.ClientPreparedStatement.prepare(Unknown Source)
    at org.apache.derby.client.am.ClientConnection.prepareStatementX(Unknown Source)
    ... 25 more

Main.java

ApplicationContext appContext  = new ClassPathXmlApplicationContext("spring/Hibernate.xml");



MailDaoImpl maildao = (MailDaoImpl)appContext.getBean("MailDao");

    MailBean mail = new MailBean();
    /** insert **/


    Scanner sc = new Scanner(System.in) ;
    try{

    System.out.println("Enter the from address\n");
    String fromAdd = sc.nextLine();
    mail.setFromAdd(fromAdd);


    System.out.println("Enter the To address\n");
    String toAddress = sc.nextLine();
    mail.setToAdd(toAddress);


    System.out.println("Enter the  Subject\n");
    String sub = sc.nextLine();
    mail.setSubject(sub);


    System.out.println("Enter the  Message\n");
    String msg = sc.nextLine();
    mail.setMessage(msg);



    mail.setAttachements(null);

    /*for getting current time */
    Calendar calendar = Calendar.getInstance();
    java.util.Date now = calendar.getTime();
    java.sql.Timestamp msgdate = new java.sql.Timestamp(now.getTime());
    mail.setDate(msgdate);

    mail.setSnt_status(false);
    }
    finally{

        sc.close();
    }


    maildao.save(mail);

}

Hibernate.xml

<?xml version='1.0' encoding='utf-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>properties/database.properties</value>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>


    <!-- Hibernate session factory -->


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property> 
        <property name="mappingResources">
            <list>
                <value>/hibernate/Mail.hbm.xml</value>
            </list>
        </property>
    </bean>
   <bean id="MailDao" class="com.RML.Dao.Impl.MailDaoImpl" >
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>


</beans>    

我的hbm.xml文件

mail.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">

<hibernate-mapping>
    <class name="com.RML.Model.MailBean" table="MAIL" >
        <id name="mail_id" type="int">
            <column name="MAIL_ID" />
            <generator class="identity" />
        </id>
        <property name="toAdd" type="string">
            <column name="TOADDRESS" length="50" not-null="true" unique="false" />
        </property>
        <property name="fromAdd" type="string">
            <column name="FROM" length="50" not-null="true" unique="false" />
        </property>
       <property name="subject" type="string">
            <column name="subject" length="250" not-null="false" unique="false" />
        </property>
        <property name="message" type="string">
            <column name="message" length="1000" not-null="false" unique="false" />
        </property>
        <property name="attachements" type="string">
            <column name="attachements" length="1000" not-null="false" unique="false" />
        </property>
        <property name="date" type="timestamp">
            <column name="date" length="50" not-null="true" unique="false" />
        </property>
        <property name="snt_status" type="boolean">
            <column name="snt_status" length="50" not-null="true" unique="false" />
        </property>

    </class>
</hibernate-mapping>

我第一次尝试这个。请在必要时纠正我。如果有必要,请问我需要哪个部分才能清楚地看到我的错误。

1 个答案:

答案 0 :(得分:4)

不要将列命名为FROM,它是保留的SQL关键字。

将其重命名为mail.hbm.xml中的其他内容。