简体   繁体   English

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

[英]Bunch of Errors after I run my Code

I was trying to add a User in my database using Java with Spring framework(Standalone) but I have encountered problems specifically in this code 我试图使用带有Spring框架的Java(Standalone)在我的数据库中添加一个User,但我在这段代码中遇到了一些问题

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

    }
}

and after I ran it I have encountered these errors 在我运行之后,我遇到了这些错误

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

if it would help this is my FinanceDao code 如果它有帮助这是我的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:'field list'中的未知列'empID',你确定该列被称为empID而不是empId或类似的东西吗?

You are using emp_id in your delete but empID in your add. 您在删除中使用emp_id ,但在添加中使用empID The error is saying: 错误是说:

Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; 线程“main”中的异常org.springframework.jdbc.BadSqlGrammarException:PreparedStatementCallback; bad SQL grammar [INSERT INTO PERSON (empID,password) VALUES(?,?)]; 错误的SQL语法[INSERT INTO PERSON(empID,password)VALUES(?,?)]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'empID' in 'field list' 嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'empID'

It means it cannot find empID in your database. 这意味着它无法在您的数据库中找到empID You have probably defined it as emp_id 您可能已将其定义为emp_id

Given: 鉴于:

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

I would wonder if there is actually a column named empID in User . 我想知道在User是否存在名为empID的列。 Based on the delete query, it looks like you meant emp_id instead: 基于删除查询,它看起来像你的意思是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 });
}

You call the id column two different things, emp_id in one statement, and empID in another. 你可以在id列中调用两个不同的东西,emp_id在一个语句中,empID在另一个语句中。 Pick one. 选一个。

According to the error message, emp_id is likely more-correct. 根据错误消息,emp_id可能更正确。 Is the DAO Add method correct? DAO添加方法是否正确?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码无法运行,但没有错误 - My code won't run, but has no errors 我的代码说它没有错误但不会运行 - My code says it has no errors yet will not run 我的Android应用在运行时关闭,代码无错误 - My Android app closes on run, no errors in code 为什么我的代码永远运行? 它只是一堆应该递减的整数的for循环? - Why does my code run forever? its just a bunch of for loops with ints that are supposed to decrement? 代码完成后输出一堆零 - Output of a bunch of zeros after code is finished Eclipse不会运行我的代码,编辑器根本不会显示任何代码错误 - Eclipse will not run my code and the editor shows no code errors at all 如何在我的程序中为具有多个参数的方法指定简单的逻辑而不需要一堆代码? - How can I specify simple logic in my program for methods with many arguments without having a bunch of code? 当该方法单独运行时,我的代码会尝试工作,但是当我在测试程序时调用该方法时…出现捕获错误 - try of my code works when the method is run on its own, but when I call the method when testing my program… catch errors appears 启动一个线程后,我们怎么能保持run函数运行? 我有很多想法,但我不确定哪个更专业? - After starting a thread how could we keep the run function running? I have bunch of ideas but I'm not sure which is more professional? BlueJ:我的代码编译没有错误,但是程序无法运行 - BlueJ: My code compiles with no errors but the program won't run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM