JDBC 和数据库集成错误

Posted

技术标签:

【中文标题】JDBC 和数据库集成错误【英文标题】:JDBC and Database Integration Error 【发布时间】:2015-07-16 23:18:54 【问题描述】:

我一直在尝试将我的数据库信息更改为不同的东西,例如在数据库代码中,有 50 位信息进入 STATES 值,当我使用它运行我的 JDBC 代码以返回多少行时有信息,它可以工作并返回 50 行。 但是我想将这个数字从 50 降低到 30,但是会发生错误,我不知道如何在没有错误发生的情况下执行此操作。它可能是非常基本的东西,但我不知道如何去做,而不会发生错误。 任何帮助将不胜感激,如果很难准确理解我的要求,我很抱歉,但这是我可以解释它的最佳方式

我不得不削减很多其余的代码,因为它看起来非常混乱。 这是我遇到问题的一些数据库代码:

SET DATABASE SQL TYPES FALSE
SET DATABASE SQL TDC DELETE TRUE
SET DATABASE SQL TDC UPDATE TRUE
SET DATABASE SQL TRANSLATE TTI TYPES TRUE
SET DATABASE SQL CONCAT NULLS TRUE
SET DATABASE SQL NULLS FIRST TRUE
SET DATABASE SQL UNIQUE NULLS TRUE
SET DATABASE SQL CONVERT TRUNCATE TRUE
SET DATABASE SQL AVG SCALE 0
SET DATABASE SQL DOUBLE NAN TRUE
SET DATABASE SQL LONGVAR IS LOB FALSE
SET DATABASE TRANSACTION CONTROL LOCKS
SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED
SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE
SET DATABASE TEXT TABLE DEFAULTS ''
SET FILES WRITE DELAY 20
SET FILES BACKUP INCREMENT FALSE
SET FILES CACHE SIZE 10000
SET FILES CACHE ROWS 50000
SET FILES SCALE 1
SET FILES LOB SCALE 32
SET FILES DEFRAG 0
SET FILES NIO TRUE
SET FILES NIO SIZE 256
SET FILES LOG TRUE
SET FILES LOG SIZE 200
CREATE USER SA PASSWORD ""
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
SET SCHEMA PUBLIC

CREATE MEMORY TABLE PUBLIC.EXPLORERS(EXPLORERID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,FIRSTNAME VARCHAR(50),LASTNAME VARCHAR(50),DOB DATE,EMAIL VARCHAR(100),ADDRESS VARCHAR(100),CITY VARCHAR(50),STATE VARCHAR(2),ZIPCODE VARCHAR(10),USERNAME VARCHAR(15),PASSWORD VARCHAR(10),TOURS VARCHAR(29),BIO VARCHAR(4096))
ALTER TABLE PUBLIC.EXPLORERS ALTER COLUMN EXPLORERID RESTART WITH 4

CREATE MEMORY TABLE PUBLIC.STATES(STATEID VARCHAR(2) NOT NULL PRIMARY KEY,STATENAME VARCHAR(29)

ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC
GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
GRANT DBA TO SA
SET SCHEMA PUBLIC

INSERT INTO STATES VALUES('AK','Alaska')
INSERT INTO STATES VALUES('AL','Alabama')
INSERT INTO STATES VALUES('AR','Arkansas')
INSERT INTO STATES VALUES('AZ','Arizona')
INSERT INTO STATES VALUES('CA','California')
INSERT INTO STATES VALUES('CO','Colorado')
INSERT INTO STATES VALUES('CT','Connecticut')
INSERT INTO STATES VALUES('DC','District of Columbia')
INSERT INTO STATES VALUES('DE','Delaware')
INSERT INTO STATES VALUES('FL','Florida')
INSERT INTO STATES VALUES('GA','Georgia')
INSERT INTO STATES VALUES('HI','Hawaii')
INSERT INTO STATES VALUES('IA','Iowa')
INSERT INTO STATES VALUES('ID','Idaho')
INSERT INTO STATES VALUES('IL','Illinois')
INSERT INTO STATES VALUES('IN','Indiana')
INSERT INTO STATES VALUES('KA','Kansas')
INSERT INTO STATES VALUES('KY','Kentucky')
INSERT INTO STATES VALUES('LA','Louisiana')
INSERT INTO STATES VALUES('MA','Massachusetts')
INSERT INTO STATES VALUES('MD','Maryland')
INSERT INTO STATES VALUES('ME','Maine')
INSERT INTO STATES VALUES('MI','Michigan')
INSERT INTO STATES VALUES('MN','Minnesota')
INSERT INTO STATES VALUES('MO','Missouri')
INSERT INTO STATES VALUES('MT','Montana')
INSERT INTO STATES VALUES('NC','North Carolina')
INSERT INTO STATES VALUES('ND','North Dakota')
INSERT INTO STATES VALUES('NE','Nebraska')
INSERT INTO STATES VALUES('NH','New Hampshire')
INSERT INTO STATES VALUES('NJ','New Jersey')
INSERT INTO STATES VALUES('NM','New Mexico')
INSERT INTO STATES VALUES('NV','Nevada')
INSERT INTO STATES VALUES('NY','New York')
INSERT INTO STATES VALUES('OH','Ohio')
INSERT INTO STATES VALUES('OK','Oklahoma')
INSERT INTO STATES VALUES('OR','Oregon')
INSERT INTO STATES VALUES('PA','Pennsylvania')
INSERT INTO STATES VALUES('RI','Rhode Island')
INSERT INTO STATES VALUES('SC','South Carolina')
INSERT INTO STATES VALUES('SD','South Dakota')
INSERT INTO STATES VALUES('TN','Tennessee')
INSERT INTO STATES VALUES('TX','Texas')
INSERT INTO STATES VALUES('UT','Utah')
INSERT INTO STATES VALUES('VA','Virginia')
INSERT INTO STATES VALUES('VT','Vermont')
INSERT INTO STATES VALUES('WA','Washington')
INSERT INTO STATES VALUES('WI','Wisconsin')
INSERT INTO STATES VALUES('WV','West Virginia')
INSERT INTO STATES VALUES('WY','Wyoming')

那么这里是Java集成代码:

package Java.JDBC.Beginners;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main 

    public static void main(String[] args) throws SQLException 

     try (
         Connection conn = DBUtil.getConnection(DBType.HSQLDB);
         Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         ResultSet rs = stmt.executeQuery("SELECT stateId, stateName FROM STATES");

     )  

         rs.last();
         System.out.print("Number of rows: " + rs.getRow());

        // System.out.println("Connected!");
      catch (SQLException e) 
         DBUtil.processException(e);
     



    





package Java.JDBC.Beginners;

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


public class DBUtil 

      private static final String USERNAME = "dbuser";
      private static final String PASSWORD = "mypassword";
      private static final String H_CONN_STRING = 
              "jdbc:hsqldb:data/explorecalifornia";
      private static final String M_CONN_STRING = 
              "jdbc:mysql://localhost/explorecalifornia";

   public static Connection getConnection(DBType dbType) throws SQLException 

    switch (dbType) 
    case MYSQL:
        return DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
    case HSQLDB:
        return DriverManager.getConnection(H_CONN_STRING, USERNAME, PASSWORD);

    default:
        return null;
     

   
   public static void processException(SQLException e) 
       System.err.println("Error message: " + e.getMessage());
       System.err.println("Error code: " + e.getErrorCode() );
       System.err.println("SQL state: " + e.getSQLState());

   


它给我的错误是:

2015-07-17T00:29:59.004+0100  SEVERE  readExistingData failed
org.hsqldb.HsqlException: error in script file: 
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.scriptio.ScriptReaderText.readExistingData(Unknown Source)
    at org.hsqldb.scriptio.ScriptReaderBase.readAll(Unknown Source)
    at org.hsqldb.persist.Log.processScript(Unknown Source)
    at org.hsqldb.persist.Log.open(Unknown Source)
    at org.hsqldb.persist.Logger.openPersistence(Unknown Source)
    at org.hsqldb.Database.reopen(Unknown Source)
    at org.hsqldb.Database.open(Unknown Source)
    at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
    at org.hsqldb.DatabaseManager.newSession(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Java.JDBC.Beginners.DBUtil.getConnection(DBUtil.java:23)
    at Java.JDBC.Beginners.Main.main(Main.java:14)

2015-07-17T00:29:59.011+0100  WARNING  Script processing failure
org.hsqldb.HsqlException: error in script file line: 113 org.hsqldb.HsqlException: error in script file: 
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.scriptio.ScriptReaderText.readExistingData(Unknown Source)
    at org.hsqldb.scriptio.ScriptReaderBase.readAll(Unknown Source)
    at org.hsqldb.persist.Log.processScript(Unknown Source)
    at org.hsqldb.persist.Log.open(Unknown Source)
    at org.hsqldb.persist.Logger.openPersistence(Unknown Source)
    at org.hsqldb.Database.reopen(Unknown Source)
    at org.hsqldb.Database.open(Unknown Source)
    at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
    at org.hsqldb.DatabaseManager.newSession(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Java.JDBC.Beginners.DBUtil.getConnection(DBUtil.java:23)
    at Java.JDBC.Beginners.Main.main(Main.java:14)
Caused by: org.hsqldb.HsqlException: error in script file: 
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 17 more

2015-07-17T00:29:59.126+0100  SEVERE  could not reopen database
org.hsqldb.HsqlException: error in script file line: 113 org.hsqldb.HsqlException: error in script file: 
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.scriptio.ScriptReaderText.readExistingData(Unknown Source)
    at org.hsqldb.scriptio.ScriptReaderBase.readAll(Unknown Source)
    at org.hsqldb.persist.Log.processScript(Unknown Source)
    at org.hsqldb.persist.Log.open(Unknown Source)
    at org.hsqldb.persist.Logger.openPersistence(Unknown Source)
    at org.hsqldb.Database.reopen(Unknown Source)
    at org.hsqldb.Database.open(Unknown Source)
    at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
    at org.hsqldb.DatabaseManager.newSession(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Java.JDBC.Beginners.DBUtil.getConnection(DBUtil.java:23)
    at Java.JDBC.Beginners.Main.main(Main.java:14)
Caused by: org.hsqldb.HsqlException: error in script file: 
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 17 more

Error message: error in script file line: 113 org.hsqldb.HsqlException: error in script file: 
Error code: -25
SQL state: S1000

如果这令人困惑,请告诉我,我会尝试更好地解释它!

【问题讨论】:

如果你想改变数据库中的数据,你需要使用 SQL DELETE(用于删除行)或UPDATE(用于更改值) .我没有看到您在发布的代码中进行任何数据操作;你到底想做什么? HSQLDB 抛出此错误。请检查您复制的 .script 文件并复制导致问题的第 113 行。请注意,您不应该自己编辑和更改 .script 文件,因为数据库会这样做。 您不应该直接编辑脚本文件。如果您想从表中删除行,请使用您最喜欢的 SQL 工具使用 DELETE 语句 【参考方案1】:

“限制从 50 到 30”是什么意思,如果只希望返回 30 个状态,请在此处更改查询

ResultSet rs = stmt.executeQuery("SELECT stateId, stateName FROM STATES LIMIT 30");

同时发布您提到的错误

【讨论】:

我不想这样限制,但我只想知道为什么当我从中取出 5 个或更多状态时,我无法更改状态值中的数据库信息。我只是想知道如何改变它以及为什么它给我这个错误。通常我可以去它并编辑信息而不会出错并弄乱它,但由于某种原因我不能这样做。我将现在发生的错误放在问题中。非常感谢您的回复,真的很感激!

以上是关于JDBC 和数据库集成错误的主要内容,如果未能解决你的问题,请参考以下文章

HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成

Spring集成JDBC

使用集成测试触发器在内存数据库中初始化 JDBC 兼容?

OWB:将外部数据库与 JDBC 集成时出现问题

如何将 Spring 与 JDBC 集成?

springboot集成jdbc