?
Solved

prepareStatement

Posted on 2005-03-31
3
Medium Priority
?
433 Views
Last Modified: 2010-03-31
Hi,
Here is my GUI and database diagram: http://people.clarkson.edu/~havenssm/gui7.doc
I am trying to insert all the course on ExamPanel into Exam table in the database:
I can't found out why i am still having the error message below.

802         <<<<===== i was able to print cID
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
        at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3150)
        at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:214)
        at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPreparedStatement.java:136)
        at sss.StudentSupportServices$AddExam.actionPerformed(StudentSupportServices.java:439)  
     
       public void actionPerformed(ActionEvent e)
       {
            Statement stm = null;
            ResultSet rs = null;
            int cID = Integer.parseInt( ""+courseTable.getModel().getValueAt(
                                            courseTable.getSelectedRow(), 0 ) );
    try{
           // Prepare a statement to insert a record
         String addExamSql = "INSERT INTO Exam(" +
                    "CourseID, Date, Time, [Time In], [Time Out], [Delivred By]," +
                    " [Total Time], [Special Instruction] )" +
                    " VALUES(?, ?, ?, ?, ?, ?, ?, ? )";
         
        PreparedStatement pstmt = accessCon.prepareStatement( addExamSql );
        String date = "";
        // Insert all the rows on CourseTable filled with data.
        for (int rowIndex=0; rowIndex < examsTable.getRowCount(); rowIndex++)
        {
            date = (String)examsTable.getValueAt(rowIndex, 0);
            if( date != null && date != "" ) // just check the first val in JTable.
            {// Set the values
                //the JTable rows col start from 0.
                pstmt.setInt(1, cID );
                pstmt.setString(2, date );
                pstmt.setString(3, (String)examsTable.getValueAt(rowIndex, 1) );
                pstmt.setString(4, (String)examsTable.getValueAt(rowIndex, 2) );
                pstmt.setString(5, (String)examsTable.getValueAt(rowIndex, 3) );
                pstmt.setString(6, (String)examsTable.getValueAt(rowIndex, 4) );
                pstmt.setString(7, (String)examsTable.getValueAt(rowIndex, 5) );
                pstmt.setString(8, (String)examsTable.getValueAt(rowIndex, 6) );
           
                // Insert this row
                int result = pstmt.executeUpdate(); <<<<<<<<<<<<<<<<<<<<<<<<<<===== Line 439
            }
        }
      // Commit updates
        accessCon.commit();
        JOptionPane.showMessageDialog(StudentSupportServices.this,
        "Exams Added", "Adding Exams", JOptionPane.INFORMATION_MESSAGE );        
            }
       
         catch (SQLException eqlex)
        {
              JOptionPane.showMessageDialog(StudentSupportServices.this,
        "Failed to add the Exams", "Adding Exams", JOptionPane.INFORMATION_MESSAGE );        
     
             try {
                    // Rollback update
                     accessCon.rollback();
                }
                catch (SQLException se)
                {
                    se.printStackTrace();
                }
              eqlex.printStackTrace();
       
        }
 
       }
     
   }////////////// END OF INNER CLASS AddExam /////////////////////////////////
   
0
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 13677076
try:

         String addExamSql = "INSERT INTO Exam (" +
                    "CourseID, [Date], [Time], [Time In], [Time Out], [Delivred By]," +
                    " [Total Time], [Special Instruction] )" +
                    " VALUES (?, ?, ?, ?, ?, ?, ?, ? )";
0
 

Author Comment

by:komlaaa
ID: 13677136
I owe you a beer man :)
Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 13677171
mmmmm, beer :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question