gpt4 book ai didi

java - 如何解决 SQLServerException : Invalid object name?

转载 作者:行者123 更新时间:2023-12-01 14:10:59 26 4
gpt4 key购买 nike

我正在使用 MS SQL 服务器创建一个 Spring Boot 应用程序。我有一个要求,我需要使用放置在/src/main/resources/文件夹中的 data.sql 文件初始化 user_database 数据库中的 USERS 表,其余的表应该在 @Table 注释的帮助下在 springboot_db 数据库中自动创建。下面是代码片段。

applicaiton.properties 文件

spring.datasource.platform=mssql
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=springboot_db
spring.datasource.username=sa
spring.datasource.password=einfochips@123
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.initialize=true

数据.sql
USE master
IF EXISTS(select name from sys.databases where name='user_database')
DROP DATABASE user_database;
CREATE DATABASE user_database;
USE user_database;
CREATE TABLE tbl_users (
id INTEGER NOT NULL IDENTITY(1,1),
name VARCHAR(25),
email VARCHAR(50),
username VARCHAR(25),
password VARCHAR(225),
gender VARCHAR(1),
contact_number VARCHAR(12),
address VARCHAR(150),
country VARCHAR(20),
newsletter BIT,
framework VARCHAR(500),
skill VARCHAR(500),
role_id INTEGER,
PRIMARY KEY (id)
);
INSERT INTO tbl_users
(name, email, username, password, gender, contact_number, address, country, newsletter, framework, skill, role_id)
VALUES
('Admin User1', 'admin@gmail.com', 'admin', '$2a$10$WOf9uuaNfUgqpfXrfK1QiO.scUjxJMA.wENEu4c8GJMbPhFwbxMwu', 'f', 919979294062, 'Ahmedabad', 'India', 0, 'Spring MVC', 'Spring', 1);

AppConfiguration.java(实体文件之一)
@Entity
@Table(name = "tbl_configuration_details")
public class AppConfiguration implements Serializable {

/**
*
*/
private static final long serialVersionUID = 6657691404940352980L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "config_id")
private Integer id;

@Column(name = "schedule_time")
private Integer scheduleTimePeriod;
// other variables and respective getter setters
}

如果我正在运行我的应用程序,那么将两种方法分开保存而不是正常工作。当我将两者合并到一个应用程序中时,它显示以下错误。
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'tbl_configuration_details'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:317) ~[mssql-jdbc-6.1.0.jre7.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_144]
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) ~[tomcat-jdbc-8.5.23.jar:na]
at com.sun.proxy.$Proxy92.executeQuery(Unknown Source) ~[na:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
... 94 common frames omitted

最佳答案

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'tbl_configuration_details'.



您已将表名定义为
CREATE TABLE tbl_users

但是,在您的代码中
@Table(name = "tbl_configuration_details")

由于不存在任何同名的对象,您将得到 Invalid object name异常(exception)。

关于java - 如何解决 SQLServerException : Invalid object name?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48596790/

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