运行我的代码后出现一堆错误

时间:2011-11-30 04:43:09

标签: java sql database spring jdbc

我尝试使用带有Spring框架的Java(Standalone)在我的数据库中添加一个用户,但我在此代码中遇到了一些问题

package test;
import dao.FinanceDao;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class TestDrive {

    public static void main(String[] args){

        FinanceDao finance = new FinanceDao();
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/payroll");
        dataSource.setUsername("root");
        dataSource.setPassword("123192");

        finance.setDataSource(dataSource);

        finance.Add("2010-01015", "51010");

    }
}

在我运行之后我遇到了这些错误

Nov 30, 2011 12:40:14 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Nov 30, 2011 12:40:15 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
Nov 30, 2011 12:40:15 PM org.springframework.jdbc.support.SQLErrorCodesFactory <init>
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO PERSON (empID,password) VALUES(?,?)]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'empID' in 'field list'
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:230)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:553)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:738)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:796)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:804)
    at dao.FinanceDao.Add(FinanceDao.java:24)
    at test.TestDrive.main(TestDrive.java:17)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'empID' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:744)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:537)
    ... 5 more

如果这对我的FinanceDao代码有帮助

package dao;

import javax.sql.DataSource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import dao.mapper.UserRowMapper;
import domainmodel.User;

public class FinanceDao implements Manage {

    private DataSource ds;

    @Override
    public void setDataSource(DataSource ds) {
        this.ds = ds;

    }

    @Override
    public void Add(String empID, String password) {
        JdbcTemplate Add = new JdbcTemplate(ds);
        Add.update("INSERT INTO PERSON (empID,password) VALUES(?,?)",
        new Object[] { empID, password });
    }

    @Override
    public void Delete(String empID , String password) {
        JdbcTemplate Delete = new JdbcTemplate(ds);
        Delete.update("Delete from User where emp_id = '?'",new Object[]{empID});
    }

}

4 个答案:

答案 0 :(得分:2)

我看到一个例外 引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'empID',你确定该列被称为empID而不是empId或类似的东西吗?

答案 1 :(得分:1)

您在删除时使用emp_id,但在添加中使用empID。错误是说:

  

线程“main”中的异常org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback;错误的SQL语法[INSERT INTO PERSON(empID,password)VALUES(?,?)];嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'empID'

这意味着它无法在您的数据库中找到empID。您可能已将其定义为emp_id

答案 2 :(得分:1)

假设:

MySQLSyntaxErrorException: Unknown column 'empID' in 'field list'

我想知道empID中是否存在名为User的列。根据删除查询,您的意思似乎是emp_id

@Override
public void Add(String empID, String password) {
    JdbcTemplate Add = new JdbcTemplate(ds);
    Add.update("INSERT INTO PERSON (emp_id,password) VALUES(?,?)",
    new Object[] { empID, password });
}

答案 3 :(得分:0)

你可以在id列中调用两个不同的东西,一个语句中为emp_id,另一个语句中为empID。选一个。

根据错误消息,emp_id可能更正确。 DAO添加方法是否正确?