更新JDBC中的行

时间:2014-06-05 20:55:22

标签: java jdbc derby

我目前正在尝试创建一个java文件,以允许基本用户界面连接到derby客户端服务器。我需要在学生课程表中更新一行,用户将知道学生ID(SID),可能需要输入学生课程1,2和3以及课程费用1,2,3,并将其更改为输入新数据。目前,我正在使用方法从用户那里获取数据,并将这些数据存储在Java中的变量中,然后我需要能够通过SID进行搜索,但不能更改它,然后学生课程ID SCID和SID保持不变输入剩余数据。 我一直在尝试使用准备好的语句来实现这一目标,但我遇到了问题。

---SQLException Caught---

  SQLState:   XCL14
  Severity: 20000
  Message:  The column position '10' is out of range.  The number of columns for t
        his ResultSet is '9'.
  java.sql.SQLException: The column position '10' is out of range.  The number of
  columns for this ResultSet is '9'.
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unkn
  own Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Sourc
  e)
    at org.apache.derby.client.am.ColumnMetaData.getColumnType(Unknown Sourc
  e)
    at org.apache.derby.client.am.PreparedStatement.setString(Unknown Source
  )
          at StudentPayments.main(StudentPayments.java:186)
  Caused by: org.apache.derby.client.am.SqlException: The column position '10' is
  out of range.  The number of columns for this ResultSet is '9'.
    at org.apache.derby.client.am.ColumnMetaData.checkForValidColumnIndex(Un
  known Source)
    ... 3 more
  Students database finishing. . . .

这是表格的架构

CREATE TABLE STUDENT_COURSES
(SCID INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    SID INT ,
    CID1 INT ,
    CID2 INT ,
    CID3 INT ,
    CC1 INT ,
    CC2 INT ,
    CC3 INT ,
    TOTAL INT ,
    foreign key (SID) references STUDENT(SID),
    foreign key (CID1) references COURSES(CID),
    foreign key (CID2) references COURSES(CID),
    foreign key (CID3) references COURSES(CID),
    UNIQUE (TOTAL)
    );

这就是我在Java中所拥有的但它无法正常工作

PreparedStatement scUpdate;
ResultSet StudentCourse;
String SCID, CID1, CID2, CID3, CC1, CC2, CC3;
String studentTotalCost;
String responseSC;

stuUpdate = connect.prepareStatement("UPDATE STUDENT SET FIRSTNAME = ?, LASTNAME = ?, DOB = ?, ADDRESS = ?, SUBURB = ?, PROVINCE = ?, POSTCODE = ?, PHONE = ?, EMAIL = ? WHERE SID = ?");

responseSC=getTableChoiceFromUser();
if( ( responseSC.substring(0,1).equals("3") || responseSC.substring(0,Math.min(responseSC.length(),mUpdate.length())).toUpperCase().equals(mUpdate) ))
//checks the Users selection                        
{
    SID = getStudentSIDFromUser();
    if(! SID.substring(0,Math.min(SID.length(),mEXIT.length())).toUpperCase().equals(mEXIT) )
    //checks to make sure User does not want to exit this step and return to start.
    {
        CID1 = getCIDFromUser();
        CID2 = getCIDFromUser();
        CID3 = getCIDFromUser();
        CC1 = getCCFromUser();
        CC2 = getCCFromUser();
        CC3 = getCCFromUser();
        SCID = getCIDFromUser();
        studentTotalCost = CC1 + CC2 + CC3;
        scUpdate.setString(1, SID);
        scUpdate.setString(2, CID1);
        scUpdate.setString(3, CID2);
        scUpdate.setString(4, CID3);
        scUpdate.setString(5, CC1);
        scUpdate.setString(6, CC2);
        scUpdate.setString(7, CC3);
        scUpdate.setString(8, studentTotalCost);
        scUpdate.setString(9, SCID);
        scUpdate.executeUpdate();
        System.out.println(Bar);
        System.out.println("Students Courses Successfully Updated.");
        System.out.println(Bar);
    }

1 个答案:

答案 0 :(得分:0)

异常代码看起来很奇怪,因为你没有提到任何“?”声明中没有。但是请注意,您只为语句提供了9个参数而不是10 - 这可能会导致问题。