gpt4 book ai didi

java - 访问 Java DB 或向 Java DB 添加条目时收到两条错误消息

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:15 25 4
gpt4 key购买 nike

    package deadb;

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

public class DeaDb {
public static void main(String[] args) {
try{
int controller;
String host = "jdbc:derby://localhost:1527/DeaDornas";
String uName = "tmybr11";
String uPass = "1324132448";
Connection con = DriverManager.getConnection( host, uName, uPass );
Statement stmt = con.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY );
String SQL = "SELECT * FROM DeaDornas";
ResultSet rs = stmt.executeQuery( SQL );
Scanner input = new Scanner( System.in );
int beta = 0;
while( beta == 0 ){
System.out.printf( "\nCOMPANY'S DATA BASE. WHAT DO YOU WANT TO DO?\n" );
System.out.println( "1 - SEE THE PRODUCTS LIST" );
System.out.println( "2 - ADD A PRODUCT" );
System.out.println( "----------" );
controller = input.nextInt();
if ( controller == 1 ){
while( rs.next() ){
String product_name = rs.getString("PRODUCT_NAME");
String outtern = "Product: "+ product_name;
System.out.println(outtern);
}
} else if ( controller == 2 ){
int rowCount = 0;
while ( rs.next() ){
rowCount++;
}
int newRow = rowCount + 1;
System.out.println( "INFORM THE NAME OF THE PRODUCT:" );
String prod = input.next();
int c = stmt.executeUpdate( "INSERT INTO DeaDornas"
+ "(ID_PRODUCT, PRODUCT_NAME)"
+ "VALUES ("+ newRow +", '"+ prod +"')");
}
}
}
catch( SQLException err ){
System.out.println( err.getMessage() );
}
}
}

上面的代码是我的主类。我使用 NetBeans 创建了一个数据库,并使用上述应用程序访问它。如果我不使用 while 循环,它工作正常。也就是说,当用户只选择选项 1 或选项 2 时。但后来我添加了一个 while 循环,这样用户可以添加一个条目并在之后查看新条目列表,或者查看条目列表并添加一个条目(无穷无尽循环。我会在后面添加一条出路)。

这是我添加条目然后查看条目列表时的输出:

COMPANY'S DATA BASE. O QUE DESEJA FAZER?
1 - SEE THE PRODUCTS LIST
2 - ADD A PRODUCT
----------
2
INFORM THE NAME OF THE PRODUCT:
TEST

COMPANY'S DATA BASE. O QUE DESEJA FAZER?
1 - SEE THE PRODUCTS LIST
2 - ADD A PRODUCT
----------
1
O ResultSet não está aberto. A operação 'next' não é permitida. Verifique se a auto-efetivação está desabilitada.

所以,我没有找到上面消息的确切翻译(我是巴西人),但它类似于“结果集未打开。‘下一步’操作是不允许的。检查是否自动执行已禁用。”

我不知道为什么会这样,我该如何解决这个错误。

这是我看到条目列表然后添加新条目时的输出:

COMPANY'S DATA BASE. WHAT DO YOU WANT TO DO?
1 - SEE THE PRODUCTS LIST
2 - ADD A PRODUCT
----------
1
Product: RABIT
Product: LYON
Product: CAT
Product: DOG
Product: BIRD
Product: CABELO
Product: TV
Product: FRUIT
Product: FRUIT
Product: ONION
Product: TEST

COMPANY'S DATA BASE. WHAT DO YOU WANT TO DO?
1 - SEE THE PRODUCTS LIST
2 - ADD A PRODUCT
----------
2
INFORM THE NAME OF THE PRODUCT:
CARROT
A instrução foi interrompida, porque iria gerar um valor duplicado da chave em uma restrição de chave primária ou de unicidade identificada por 'SQL150619130501560' definida em 'DEADORNAS'.

翻译为“该语句被中止,因为它会导致唯一或主键约束或唯一索引中的重复键值,该索引由‘DEADORNAS’上定义的‘SQL150619130501560’标识。”

所以,这些是我收到的消息。有什么想法吗?

最佳答案

在选项 1 的 block 内部,您需要重新执行此查询

ResultSet rs = stmt.executeQuery( SQL );

ResultSet 维护状态,它知道它在表中的位置。当它到达末尾时,需要将其重新指向开头,或者您可以创建一个新的结果集。结果集就像游标。

对于第二个错误,和第一个问题有点关系。

                    int rowCount = 0;
while ( rs.next() ){
rowCount++;
}

由于您没有正确使用结果集,rowCount 初始化为 0 值,然后将 1 添加到它。然后插入发生,它尝试将 1 插入到已经有一个主键的索引中。

关于java - 访问 Java DB 或向 Java DB 添加条目时收到两条错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30947300/

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