gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:02:49 25 4
gpt4 key购买 nike

我试图使用 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});
}

}

最佳答案

我看到一个异常(exception)由以下原因引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“empID”,您确定该列称为 empID 而不是 empId 或类似名称吗?

关于java - 运行我的代码后出现一堆错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8321264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com