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

标签 java swing jdbc prepared-statement

好的,所以我刚开始使用 derby 客户端的 JDBC,我对它有点陌生。 我将列 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 或 My 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 的基础知识。

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

enter image description here

提前致谢。

最佳答案

您需要明确提及“自动递增”。 或者您可以编写自己的 Java 代码来跟踪每个表的 ID,并且每当您要求该方法提供 ID 时,它都会返回 lastID + 1。

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

关于java - Prepared Statement 主键存储种子 1 和增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025673/

相关文章:

java - 使用适用于桌面应用程序的 Java 开发工具包进行 AWS Cognito 登录

java - "isProbablePrime"真的需要花费大部分时间来读取文件吗?

java - miglayout关于列的问题

java - JApplet多页面应用

java.sql.SQLException : Before start of result set. 结果集循环内的查询如何?

oracle - oracle DATE 和 TIMESTAMP 的区别

java - spring SQL Injection中的@Query注解安全吗?

java - 我无法获得所选文件的完整物理路径

java - 如何在单击输入键盘输出时选择文本框

java - 如何防止JSP中的SQL注入(inject)?