jdbcsqlsyntaxerrorexception:table“ sportevent”未找到; SQL语句,但是为什么呢?

发布于 2025-02-13 07:53:38 字数 7262 浏览 0 评论 0原文

我正在为我的应用程序使用H2数据库,而我的任务是创建和读取查询作为实践。连接正常,但是无论我尝试做什么,都找不到表。我的课程如下:

DB连接:

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabaseConnectionManager {

private final String url;
private final Properties properties;

public DatabaseConnectionManager(String host, String databaseName, String username,String password){
    this.url = "jdbc:h2:mem://"+host+"/"+databaseName;
    this.properties = new Properties();
    this.properties.setProperty("username",username);
    this.properties.setProperty("password",password);
}
public Connection getConnection() throws SQLException{
    return DriverManager.getConnection(this.url,this.properties);
}
}

JDBCEXECUTOR:

package database;



import dataaccess.SporteventDAO;
import domain.FootballSportEvent;

import java.sql.*;
import java.time.LocalDateTime;

public class JDBCExecutor {
    public static void main(String[] args) {

      DatabaseConnectionManager databaseConnectionManager= new DatabaseConnectionManager("localhost","jdbc:h2:mem:sports_betting","sa","");
        try{
            Connection connection = databaseConnectionManager.getConnection();
            SporteventDAO sporteventDAO = new SporteventDAO(connection);
            FootballSportEvent footballSportEvent = new FootballSportEvent();
            footballSportEvent.setType("FootballSportEvent");
            footballSportEvent.setId(2);
            Date start = new Date(2022-07-06);
            Date end = new Date(2022-07-07);
            footballSportEvent.setStart(start);
            footballSportEvent.setEnd(end);
            footballSportEvent.setResultId(2);

            sporteventDAO.create(footballSportEvent);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

SportEventDao:

    package dataaccess;

import domain.FootballSportEvent;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class SporteventDAO extends DataAccessObject<FootballSportEvent> {
    private static final String INSERT = "INSERT INTO SPORTEVENT (DTYPE, ID, START, END, TITLE, RESULT_ID) VALUES (?,?,?,?,?,?)";

    public SporteventDAO(Connection connection){
        super(connection);
    }
    @Override
    public FootballSportEvent findById(long id) {
        return null;
    }

    @Override
    public List<FootballSportEvent> findAll() {
        return null;
    }

    @Override
    public FootballSportEvent update(FootballSportEvent dto) {
        return null;
    }

    @Override
    public FootballSportEvent create(FootballSportEvent dto) throws SQLException {
        try(PreparedStatement statement = this.connection.prepareStatement(INSERT);){
            statement.setString(1,dto.getType());
            statement.setInt(2,dto.getId());
            statement.setDate(3, (Date) dto.getStart());
            statement.setDate(4, (Date) dto.getEnd());
            statement.setInt(5, dto.getResultId());
            statement.execute();
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public void delete(long id) {

    }
}

DatatRansferObject:

package dataaccess;

public interface DataTransferObject {
    int getId();
}

dataAccessObject:

    package dataaccess;

import org.h2.jdbc.JdbcConnectionBackwardsCompat;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public abstract class DataAccessObject <T extends DataTransferObject>{
    protected final Connection connection;
    protected final static String LAST_VAL = "SELECT last_value FROM";
    protected final static String CUSTOMER_SEQUENCE = "hp_customer_seq";

    public DataAccessObject(Connection connection){
        super();
        this.connection =connection;
    }
    public abstract T findById(long id);
    public abstract List<T> findAll();
    public abstract T update(T dto);
    public abstract T create(T dto) throws SQLException;
    public abstract void delete(long id);

    protected int getLastVal(String sequence){
        int key = 0;
        String sql = LAST_VAL + sequence;
        try(Statement statement = connection.createStatement()) {
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                key = rs.getInt(1);
            }
            return key;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

footballsporteventClass:

  package domain;


import dataaccess.DataTransferObject;

import java.util.Date;

public class FootballSportEvent implements DataTransferObject 
    {
    private String type;
    private String name;
    private int id;
    private Date end;
    private Date start;
    private String title;
    private int resultId;

    public FootballSportEvent(){

    }

    public FootballSportEvent(String type, String name, int id, Date end, Date start, String title, int resultId) {
        this.type = type;
        this.name = name;
        this.id = id;
        this.end = end;
        this.start = start;
        this.title = title;
        this.resultId = resultId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getResultId() {
        return resultId;
    }

    public void setResultId(int resultId) {
        this.resultId = resultId;
    }

    @Override
    public String toString() {
        return "FootballSportEvent{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", id=" + id +
                ", end=" + end +
                ", start=" + start +
                ", title='" + title + '\'' +
                ", resultId=" + resultId +
                '}';
    }
}

以及下面的例外例外:edit:

    org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "SPORTEVENT" not found; SQL statement:
INSERT INTO SPORTEVENT (DTYPE, ID, START, END, TITLE, RESULT_ID) VALUES (?,?,?,?,?,?) 

edit: edit: 我还尝试使用: this.url = “ jdbc:h2:mem://“+host+”/“+databasename+”; db_close_delay = -1“; 但这无效。

Im using h2 database for my application, and my task is to create and read queries as practice. The connection works fine, but the table is not found no matter what I try to do. My classes look like the following:

DB connection:

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabaseConnectionManager {

private final String url;
private final Properties properties;

public DatabaseConnectionManager(String host, String databaseName, String username,String password){
    this.url = "jdbc:h2:mem://"+host+"/"+databaseName;
    this.properties = new Properties();
    this.properties.setProperty("username",username);
    this.properties.setProperty("password",password);
}
public Connection getConnection() throws SQLException{
    return DriverManager.getConnection(this.url,this.properties);
}
}

JDBCExecutor:

package database;



import dataaccess.SporteventDAO;
import domain.FootballSportEvent;

import java.sql.*;
import java.time.LocalDateTime;

public class JDBCExecutor {
    public static void main(String[] args) {

      DatabaseConnectionManager databaseConnectionManager= new DatabaseConnectionManager("localhost","jdbc:h2:mem:sports_betting","sa","");
        try{
            Connection connection = databaseConnectionManager.getConnection();
            SporteventDAO sporteventDAO = new SporteventDAO(connection);
            FootballSportEvent footballSportEvent = new FootballSportEvent();
            footballSportEvent.setType("FootballSportEvent");
            footballSportEvent.setId(2);
            Date start = new Date(2022-07-06);
            Date end = new Date(2022-07-07);
            footballSportEvent.setStart(start);
            footballSportEvent.setEnd(end);
            footballSportEvent.setResultId(2);

            sporteventDAO.create(footballSportEvent);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

SporteventDAO:

    package dataaccess;

import domain.FootballSportEvent;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class SporteventDAO extends DataAccessObject<FootballSportEvent> {
    private static final String INSERT = "INSERT INTO SPORTEVENT (DTYPE, ID, START, END, TITLE, RESULT_ID) VALUES (?,?,?,?,?,?)";

    public SporteventDAO(Connection connection){
        super(connection);
    }
    @Override
    public FootballSportEvent findById(long id) {
        return null;
    }

    @Override
    public List<FootballSportEvent> findAll() {
        return null;
    }

    @Override
    public FootballSportEvent update(FootballSportEvent dto) {
        return null;
    }

    @Override
    public FootballSportEvent create(FootballSportEvent dto) throws SQLException {
        try(PreparedStatement statement = this.connection.prepareStatement(INSERT);){
            statement.setString(1,dto.getType());
            statement.setInt(2,dto.getId());
            statement.setDate(3, (Date) dto.getStart());
            statement.setDate(4, (Date) dto.getEnd());
            statement.setInt(5, dto.getResultId());
            statement.execute();
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public void delete(long id) {

    }
}

DataTransferObject:

package dataaccess;

public interface DataTransferObject {
    int getId();
}

DataAccessObject:

    package dataaccess;

import org.h2.jdbc.JdbcConnectionBackwardsCompat;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public abstract class DataAccessObject <T extends DataTransferObject>{
    protected final Connection connection;
    protected final static String LAST_VAL = "SELECT last_value FROM";
    protected final static String CUSTOMER_SEQUENCE = "hp_customer_seq";

    public DataAccessObject(Connection connection){
        super();
        this.connection =connection;
    }
    public abstract T findById(long id);
    public abstract List<T> findAll();
    public abstract T update(T dto);
    public abstract T create(T dto) throws SQLException;
    public abstract void delete(long id);

    protected int getLastVal(String sequence){
        int key = 0;
        String sql = LAST_VAL + sequence;
        try(Statement statement = connection.createStatement()) {
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                key = rs.getInt(1);
            }
            return key;
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

FootballSportEventClass:

  package domain;


import dataaccess.DataTransferObject;

import java.util.Date;

public class FootballSportEvent implements DataTransferObject 
    {
    private String type;
    private String name;
    private int id;
    private Date end;
    private Date start;
    private String title;
    private int resultId;

    public FootballSportEvent(){

    }

    public FootballSportEvent(String type, String name, int id, Date end, Date start, String title, int resultId) {
        this.type = type;
        this.name = name;
        this.id = id;
        this.end = end;
        this.start = start;
        this.title = title;
        this.resultId = resultId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getResultId() {
        return resultId;
    }

    public void setResultId(int resultId) {
        this.resultId = resultId;
    }

    @Override
    public String toString() {
        return "FootballSportEvent{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", id=" + id +
                ", end=" + end +
                ", start=" + start +
                ", title='" + title + '\'' +
                ", resultId=" + resultId +
                '}';
    }
}

And the following exception it thrown:

    org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "SPORTEVENT" not found; SQL statement:
INSERT INTO SPORTEVENT (DTYPE, ID, START, END, TITLE, RESULT_ID) VALUES (?,?,?,?,?,?) 

Edit:
I also tried using:
this.url = "jdbc:h2:mem://"+host+"/"+databaseName+";DB_CLOSE_DELAY=-1";
but it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文