简体   繁体   English

org.postgresql.util.PSQLException:错误:关系“员工”不存在 Position:13

[英]org.postgresql.util.PSQLException: ERROR: relation “employee” does not exist Position: 13

I am trying to change the database from MySQL to PostgreSQL from the tutorial given on https://javainspires.blogspot.com/2020/07/spring-boot-2-spring-data-jdbc-jdbc.html . I am trying to change the database from MySQL to PostgreSQL from the tutorial given on https://javainspires.blogspot.com/2020/07/spring-boot-2-spring-data-jdbc-jdbc.html . I am constantly having the error shown in the title.我经常遇到标题中显示的错误。

Employee.java员工.java

package com. example.demo;

import javax.persistence.Entity;
import javax.persistence.Table;

public class Employee {

    private String username;
    private String email;
    private String password;

    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Employee(String username, String email, String password) {
        super();
        this.username = username;
        this.email = email;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

EmployeeController员工控制器

package com. example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class EmployeeController {

    @Autowired
    JdbcTemplate jdbcTemplate;
    
    @PostMapping(path = "addUser")
    public String addUser(Employee user) {
        
        String insert_query = "INSERT INTO employee (username,email,password) VALUES(?,?,?)"; 
        
        int rows = jdbcTemplate.update(insert_query,
                user.getUsername(), 
                user.getEmail(),
                user.getPassword());
        
        if(rows == 1) {
            return "success"; 
        } else {
            return "error"; 
        }
                
    
    }
    
}

Html Files: index.html Html 文件:index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>

 <form th:action="@{/addUser}" th:object="${user}" method="post" >
 
  <p>User Name :<input type="text" name="username" /></p>
  <p>Email :<input type="email" name="email" /></p>
  <p>Password :<input type="password" name="password" /></p>
  <p><input type="submit" value="Sign Up" /></p>
 </form>
</body>
</html>

Application.properties应用程序属性

spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver

schema.sql架构.sql

CREATE TABLE employee
(
    username varchar(100) NOT NULL, 
    email varchar(100) NOT NULL,
    password varchar(100) NOT NULL
); 

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>JDBC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JDBC</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I deleted the schema.sql file and created it manually in PgAdmin4.我删除了 schema.sql 文件并在 PgAdmin4 中手动创建。 The above code worked after that.上面的代码在那之后工作了。

You can try adding the schema name to your query.您可以尝试将架构名称添加到您的查询中。

INSERT INTO employee (username,email,password) VALUES(?,?,?)

from this to从此到

INSERT INTO SCHEMA_NAME.employee (username,email,password) VALUES(?,?,?)

If this does not work can you annotate the employee class with @Entity and @Table(name = "TABLE_NAME" may be the table name is user in your case.如果这不起作用,您可以使用@Entity and @Table(name = "TABLE_NAME"注释员工 class 可能是您的情况下的表名是用户。

And add @RequestBody annotation to the method parameter Employee public String addUser(@RequestBody Employee user)并将@RequestBody注解添加到方法参数Employee public String addUser(@RequestBody Employee user)

暂无
暂无

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

相关问题 如何解决“Caused by: org.postgresql.util.PSQLException: ERROR: relation “employee” does not exist Position: 13”错误? - How to resolve "Caused by: org.postgresql.util.PSQLException: ERROR: relation "employee" does not exist Position: 13" error? org.postgresql.util.PSQLException:错误:关系“ employee1”不存在 - org.postgresql.util.PSQLException: ERROR: relation “employee1” does not exist org.postgresql.util.PSQLException:错误:关系“序列”不存在 - org.postgresql.util.PSQLException: ERROR: relation “sequence” does not exist org.postgresql.util.PSQLException:错误:关系“产品”不存在 - org.postgresql.util.PSQLException: ERROR: relation "products" does not exist org.postgresql.util.PSQLException:错误:关系“用户”不存在 - SpringBoot,Hibernate,Postgresql - org.postgresql.util.PSQLException: ERROR: relation "users" does not exist - SpringBoot, Hibernate, Postgresql org.postgresql.util.PSQLException:错误:关系“clienti_id_seq”不存在 - org.postgresql.util.PSQLException: ERROR: the relation“clienti_id_seq” does not exist org.postgresql.util.PSQLException:错误:关系不存在PreparedStatement.executeQuery() - org.postgresql.util.PSQLException : ERROR : relation does NOT exist PreparedStatement.executeQuery () org.postgresql.util.PSQLException:错误:关系“app_user”不存在 - org.postgresql.util.PSQLException: ERROR: relation "app_user" does not exist 嵌套异常是 org.postgresql.util.PSQLException:错误:关系“equ_config”不存在 - nested exception is org.postgresql.util.PSQLException: ERROR: relation "equ_config" does not exist org.postgresql.util.PSQLException:错误:关系“table_name”不存在 - org.postgresql.util.PSQLException:ERROR: relation "table_name" does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM