1

我正在学习 Spring 教程,但在 JDBC 部分遇到了问题。代码是直接从教程本身复制而来的,在失败之前,对数据库的几次调用都正确运行。谁能帮我?这是我的错误跟踪。

Jul 31, 2012 9:40:03 PM org.springframework.jdbc.core.metadata.GenericCallMetaDataProvider processProcedureColumns
WARNING: Error while retrieving metadata for procedure columns: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: FUNCTION getrecord does not exist
Jul 31, 2012 9:40:03 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]
Jul 31, 2012 9:40:03 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: CallableStatementCallback; bad SQL grammar [{call getrecord()}]; nested exception is com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: FUNCTION getrecord does not exist
        at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:969)
        at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1003)
        at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:391)
        at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:354)
        at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:181)
        at com.tutorialspoint.StudentJDBCTemplate.getStudent(StudentJDBCTemplate.java:32)
        at com.tutorialspoint.MainApp.main(MainApp.java:29)
    Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: FUNCTION getrecord does not exist
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
        at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
        at com.mysql.jdbc.DatabaseMetaData.getCallStmtParameterTypes(DatabaseMetaData.java:1607)
        at com.mysql.jdbc.DatabaseMetaData.getProcedureColumns(DatabaseMetaData.java:4034)
        at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStatement.java:709)
        at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:513)
        at com.mysql.jdbc.Connection.parseCallableStatement(Connection.java:4583)
        at com.mysql.jdbc.Connection.prepareCall(Connection.java:4657)
        at com.mysql.jdbc.Connection.prepareCall(Connection.java:4631)
        at org.springframework.jdbc.core.CallableStatementCreatorFactory$CallableStatementCreatorImpl.createCallableStatement(CallableStatementCreatorFactory.java:167)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:947)
        ... 6 more

这是我的主要应用程序中出现问题的部分,我已经标记了有问题的行。

    System.out.println("----Listing Record with ID = 2 -----");
        // Error occurs in next line
    Student student = studentJDBCTemplate.getStudent(2);
    System.out.print("ID : " + student.getId());
    System.out.print(", Name : " + student.getName());
    System.out.println(", Age : " + student.getAge());

StudentJDBCTemplate 有这两个变量。

private DataSource dataSource;
private SimpleJdbcCall jdbcCall;

StudentJDCBTemplate 像这样设置数据源,这可能是问题的根源,因为我无法弄清楚 getRecord 指的是什么。

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcCall = new SimpleJdbcCall(dataSource)
            .withProcedureName("getRecord");
}

StudentJDBCTemplate这个方法出现问题。

public Student getStudent(Integer id) {
    SqlParameterSource in = new MapSqlParameterSource().addValue("in_id",
            id);
    Map<String, Object> out = jdbcCall.execute(in);
    Student student = new Student();
    student.setId(id);
    student.setName((String) out.get("out_name"));
    student.setAge((Integer) out.get("out_age"));
    return student;
}

这是我的 Beans.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-3.0.xsd "> <!-- Initialization for data source -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/TEST" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean> <!-- Definition for studentJDBCTemplate bean -->
    <bean id="studentJDBCTemplate" class="com.tutorialspoint.StudentJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

对于它的价值,这是我的学生课。

package com.tutorialspoint;

public class Student {
    private Integer age;
    private String name;
    private Integer id;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }
}
4

4 回答 4

1

按照您的教程,您错过了getRecord存储过程的创建:

DELIMITER $$

DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$
CREATE PROCEDURE `TEST`.`getRecord` (
IN in_id INTEGER,
OUT out_name VARCHAR(20),
OUT out_age  INTEGER)
BEGIN
   SELECT name, age
   INTO out_name, out_age
   FROM Student where id = in_id;
END $$

DELIMITER ;
于 2012-08-01T07:05:42.493 回答
0

您正在尝试执行存储过程“getrecord”。哪个在数据库中不存在或未正确编译。尝试先通过数据库端的 SQL 客户端执行。

程序应与签名匹配为 2 个输出参数和 1 个输入参数。

于 2012-08-01T04:55:47.040 回答
0

Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: FUNCTION getrecord does not exist..所以检查一次..创建getrecord函数并尝试一次..

于 2012-08-01T07:01:40.473 回答
0

以防万一有人在 2017 年遇到这个问题,如果您使用 MySQL 连接器版本 6.x 而不是 5.x,这里有必要更改那个旧教程

在 Beans.xml 中,这一行应该是:

<property name="url" value="jdbc:mysql://localhost:3306/test?nullNamePatternMatchesAll=true" />

代替: <property name="url" value="jdbc:mysql://localhost:3306/test />

声称未定义“out_name”参数的 SQL 错误非常具有误导性。

这是由于 MySQL 连接器从 5.x 版更改为 6.x 版,如此处评论 原始 SO 答案这里

于 2017-01-21T20:50:17.817 回答