我一直在尝试将我的数据库的信息更改为不同的东西,例如在数据库代码中有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
如果这令人困惑,请告诉我,我会尝试更好地解释它!
答案 0 :(得分:0)
你是什么意思&#34;限制在50到30&#34;,如果你只想要返回30个州,请在这里更改查询
ResultSet rs = stmt.executeQuery("SELECT stateId, stateName FROM STATES LIMIT 30");
同时发布您提到的错误