Prepared Statement 主键存储种子 1 和增量

Prepared Statement Primary Key store seed 1 and increment

好的,所以我刚开始 JDBC 使用 derby 客户端,我对它有点陌生。 我将列 ID 设置为主键,并将 int 作为其数据类型。但是,我不确定是否应该包含 myStatement.setString(1, ?);,因为我认为它应该自动递增,但看起来它没有这样做。

这是我的 Grab 文件详细信息:

create table "ADMIN1".STUDENTPERSONALDETAILS
(
ID INTEGER not null primary key,
STUDENTID VARCHAR(10) not null,
LASTNAME VARCHAR(50) not null,
FIRSTNAME VARCHAR(50) not null,
MIDDLENAME VARCHAR(50) not null,
PLACEOFBIRTH VARCHAR(200) not null,
DOB VARCHAR(50) not null,
GENDER VARCHAR(4) not null,
CIVILSTATUS VARCHAR(7) not null,
RELIGION VARCHAR(15) not null,
NATIONALITY VARCHAR(20) not null
)

如何更正我的 PreparedStatement 或我的 Table 以便自动为列 ID 添加值,以便我可以开始 setString(2, studentID) 并避免出现错误关于与提供的内容不匹配的列数?

这是我的代码:

addButton.addActionListener(new ActionListener () {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                String myDbUrl = "jdbc:derby://localhost:1527/Enrollment"; //stores url to string
                String userName = "admin1";
                String Password = "admin1";
                    Connection myDBConnection = DriverManager.getConnection(myDbUrl, userName, Password);
                String myQuery = "INSERT INTO STUDENTPERSONALDETAILS"
                        + "(STUDENTID,LASTNAME,FIRSTNAME,MIDDLENAME,PLACEOFBIRTH,DOB,GENDER,CIVILSTATUS,RELIGION,NATIONALITY) "
                        + "VALUES(?,?,?,?,?,?,?,?,?,?) ";

                String adminissionNo ;
                String studentID = tfStudentId.getText().toString();
                String lastName = tfLastName.getText().toString();
                String firstName = tfFirstName.getText().toString();
                String middleName = tfMiddleName.getText().toString();
                String placeOfBirth = tfPob.getText().toString();
                String dateOfBirth = listDOB.getSelectedItem().toString();
                String gender = listGender.getSelectedItem().toString();
                String civilStatus = listCivilStatus.getSelectedItem().toString();
                String religion = listReligion.getSelectedItem().toString();
                String nationality = listNationality.getSelectedItem().toString();

                PreparedStatement myStatement = myDBConnection.prepareStatement(myQuery);

                    myStatement.setString(2, lastName);
                    myStatement.setString(3, firstName);
                    myStatement.setString(4, middleName);
                    myStatement.setString(5, placeOfBirth);
                    myStatement.setString(6, dateOfBirth);
                    myStatement.setString(7, gender);
                    myStatement.setString(8, civilStatus);
                    myStatement.setString(9, religion);
                    myStatement.setString(10, nationality);

                boolean insertResult = myStatement.execute();
                if(insertResult == true)
                    JOptionPane.showMessageDialog(null, "Successfully Added Information");
                else
                    JOptionPane.showMessageDialog(null, "Encountered an error while inserting data");
            }
            catch(SQLException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            }
        }
  });  

是否需要包含 myStatement.setString(1, integervaluehere) 作为主键?它不是应该自动递增吗?

我很感激任何解释,因为我最近才开始学习 PreparedStatements 的基础知识。

我尝试计算列数并尝试了 myStatement.setString() 的 10 行和 11 行,但由于不匹配,仍然无法插入数据。

提前致谢。

您需要明确提及 'auto increment'。 或者您可以编写自己的 java 代码来跟踪每个 table 的 ID,每当您要求该方法提供 ID 时,它都会 return lastID + 1.

但是,我认为现在您可以使用 auto_increment 选项。

如果你想让它自动递增,你需要在列定义中这样说,但你没有。

我不知道你标题中的 'default 1' 是什么意思,因为你没有在你的问题中提到它,但你不能有默认值 and 自动递增。没有意义。

我也不知道 'store seed 1' 在您的编辑中是什么意思。

当您有一个具有您想要依赖的默认值或自动增量的列时,您根本不会在 INSERT 语句中提及它,因此没有要设置的位置参数。

首先,将主标识符列设置为自动增量。由于您的查询已经排除了主键,因此您只需更改 PreparedStatement indexes to match the number of parameters in your query starting from one.

由于除了主 ID 列之外还有 10 列,因此您的 PreparedStatement 可能如下所示:

PreparedStatement myStatement = myDBConnection.prepareStatement(myQuery);
myStatement.setString(1, studentId);
myStatement.setString(2, lastName);
myStatement.setString(3, firstName);
myStatement.setString(4, middleName);
myStatement.setString(5, placeOfBirth);
myStatement.setString(6, dateOfBirth);
myStatement.setString(7, gender);
myStatement.setString(8, civilStatus);
myStatement.setString(9, religion);
myStatement.setString(10, nationality);

请注意,一旦您将 table 中的主键更改为 auto-increment,就不需要指令 myStatement.setInt(1, primaryId);。但是,如果您选择将主键保留为 non-autoincrementing,则必须明确指定主键值并在查询中提供参数以插入该数据。

如果您正在使用 MySQL Workbench,如果您没有使用,我强烈推荐,因为它很管用。您必须选择 Auto-Increment 作为该列的特征。如果你想让你的列自动递增,在你的数据库中创建列时,勾选选项Auto-Increment,有时写成AI。