Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 爪哇及;SQL-Can';t在主键和外键中创建重复值_Java_Sql_Netbeans_Derby - Fatal编程技术网

Java 爪哇及;SQL-Can';t在主键和外键中创建重复值

Java 爪哇及;SQL-Can';t在主键和外键中创建重复值,java,sql,netbeans,derby,Java,Sql,Netbeans,Derby,放松点,中学老师在上CS课。我有一个Java程序,它要求输入用户名、身高、体重,进行一些计算,并向用户提供结果。我现在需要将这些数据存储在数据库中。在开始使用主键和外键之前,我可以获取要存储的数据 以下是我无法理解的错误: 错误:java.sql.SQLIntegrityConstraintViolationException:该语句被中止,因为它会导致在“USERPROFILE”上定义的“SQL180429151131780”标识的唯一或主键约束或唯一索引中出现重复的键值 这是我的桌子: dr

放松点,中学老师在上CS课。我有一个Java程序,它要求输入用户名、身高、体重,进行一些计算,并向用户提供结果。我现在需要将这些数据存储在数据库中。在开始使用主键和外键之前,我可以获取要存储的数据

以下是我无法理解的错误: 错误:java.sql.SQLIntegrityConstraintViolationException:该语句被中止,因为它会导致在“USERPROFILE”上定义的“SQL180429151131780”标识的唯一或主键约束或唯一索引中出现重复的键值

这是我的桌子:

drop table stayfitapp.userdata;
drop table stayfitapp.userprofile;
drop schema stayfitapp restrict;

create schema stayfitapp;

create table stayfitapp.userprofile
(
    profileName varchar(255) not null primary key, 
    profileGender varchar(255) not null
);

create table stayfitapp.userdata
( 
    profileAge double not null,
    profileWeight double not null,
    profileHeight double not null,
    profileWaistCircumference double not null,
    profileHipCircumference double not null,
    profileName varchar(255),
    foreign key (profileName) references stayfitapp.userprofile(profileName)
);
下面是“应用程序”中写入表的部分

public void save(){
    try {
        String query = "insert into stayfitapp.userprofile" + "(profileName, profileGender)" + "values" + "(?,?)"; 

        String query2 = "insert into stayfitapp.userdata" + "(profileAge, profileWeight, profileHeight, profileWaistCircumference, profileHipCircumference)" + "values" + "(?,?,?,?,?)";


    Connection myConnection = DriverManager.getConnection("jdbc:derby://localhost:1527/stayfitDB2", "username", "password");

    Statement myStatement = myConnection.createStatement();
    //Statement myStatement2 = myConnection.createStatement();

    PreparedStatement prepared = myConnection.prepareStatement(query);
    prepared.setString(1, profileName);
    prepared.setString(2, profileGender);

    PreparedStatement prepared2 = myConnection.prepareStatement(query2);
    prepared2.setDouble(1, profileAge);
    prepared2.setDouble(2, profileWeight);
    prepared2.setDouble(3, profileHeight);
    prepared2.setDouble(4, profileWaistCircumference);
    prepared2.setDouble(5, profileHipCircumference);

    int rowsAffected = prepared.executeUpdate(); 
    int rowsAffected2 = prepared2.executeUpdate();

    if(rowsAffected==0)
    {
        System.out.println("Warning: User data did not save!");
    }
    else
    {
        System.out.println("User info saved!");
    }
}
catch(SQLException e)
{
    System.out.println("Error: "+e.toString());
}
save()方法将尝试将用户添加到stayfitapp.userprofile表中。此表有一个名为profileName的字段。profileName是“主键”,因此不允许重复值。 您遇到的错误是无法将记录添加(插入)到表中,因为该表已经有一个同名的记录。 如果每次使用不同的名称,您的程序工作正常吗

您需要向程序中添加一些逻辑来处理profileName已经存在于表中的场景。这可能涉及删除或更新现有记录。

这就是问题所在

insert into stayfitapp.userprofile" 
+ "(profileName, profileGender)" + "values" , etc
您无需检查记录是否已存在。像这样的东西会更好用

insert into stayfitapp.userprofile
profileName, profileGender
select distinct ?, ?
from someSmallTable
where not exists (
select 1
from stayfitapp.userprofile
where profileName = ?
)

someSmallTable位取决于您的数据库引擎,您没有指定它。

我最后编写了一个方法来检查用户名是否已经在配置文件表中。如果用户名是重复的,我只会写入数据表。如果用户名是新的,我会写入两个表


谢谢你的帮助!我确信有一种更有效的方法(从形象上和字面上来说),但我正在进行我的最后一个项目,几乎在一个实际的CS类中幸存下来。

userprofile
,列:
profileName varchar(255)not null主键,
-主键的意思是,此列中的值必须是唯一的,此列中不能有两个或多个值相同的记录。您正在尝试插入例如
John
,但另一个
John
已在表中,数据库对此表示不满。谢谢。其想法是用户返回使用该程序,并存储他们的新数据(相同用户名)。对于gendercode,varchar(255)?用户键入他们的性别。是的,不同的配置文件名称可以很好地更新表。我需要为每个用户创建多条记录,因为程序稍后会对当前用户的权重、BMI等进行平均。如果您希望为每个用户创建多条记录,则可能不希望将profileName作为主键。如果该用户过去使用过该程序,我是否可以跳过更新主键表?还是那糟糕的形式?了解更多关于课堂作业的知识会有所帮助。我的猜测是,您将努力创建一个UserProfile(id,name)表和一个UserDetail表(id,weight,height等)。这两个表将用主键和外键链接。你基本上猜到了。。。1) 我们创建了一个Java应用程序,它收集了用户名、年龄、身高、体重、臀围和腰围。2) 我们对数据进行了计算3)我们将数据存储在ArrayList中,并计算平均值、最大值和最小值4)我们创建了表(如上所示),并通过SQL将数据存储在这些表中5)我们现在正在集成Java应用程序和表。这些表将存储数据,而不是ArrayList。我们将对表中的数据执行平均、最小和最大计算。我不确定“数据库引擎”的确切含义。也许jdbc:derbydatabaseengine是
软件
关系数据库管理系统(RDBMS)
的另一个词。示例包括Sql Server、Oracle、Sybase和MySQL。