错误的一堆后,我跑我的代码

我试图使用Java与Spring框架(单机)我的数据库添加一个用户,但我在此代码专门遇到的问题

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}); } }

--------------解决方案-------------

我看到导致了异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列'EMPID“在”字段列表“,你肯定列被称为EMPID而不是EMPID或类似的东西?

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

异常线程“main”org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback; 糟糕的SQL语法[INSERT INTO PERSON(EMPID,密码)VALUES(?,?); 嵌套的例外是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列'EMPID“在”字段列表“

这意味着它无法找到empID在数据库中。 你可能把它定义为emp_id

鉴于:

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

如果确实有一个名为专栏中,我会怀疑empIDUser 。 基于删除查询,它看起来像你的意思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 });
}

你叫id列两回事,EMP_ID在一条语句,并在EMPID另一个。 选一个。

根据错误信息,EMP_ID可能是更正确的。 是DAO Add方法正确吗?

分类:java的 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.670 (s). 10 q(s)