?
Solved

sql

Posted on 2005-03-28
8
Medium Priority
?
152 Views
Last Modified: 2010-03-31
Hi,
I am trying to insert from this GUI:http://people.clarkson.edu/~havenssm/gui5.doc
by pressing *Add new Student* button. i was able to print the last and first names of the student
 entered on GUI, why this error?


===============Error message ===============
sLast:myLast
sFirst:myFirst
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
          at sss.StudentSupportServices$AddStudent.actionPerformed(StudentSupportServices.java:450)

   
       //================ 1.) process student ====================
       
            String sLast = student.getLast();
            String sFirst = student.getFirst();
            String sNum = student.getStudentNum();
            String sPhone = student.getPhoneNum();
            String sBox = student.getBoxNum();
            String sAccom = student.getAccom();
            try {    
                 stm = accessCon.createStatement();  
                // Disable auto commit
                accessCon.setAutoCommit(false);
               
            String addStudentSql ="INSERT INTO Student" +
              " ([Last Name], [First Name], [Student Number],[Box #]," +
              " [Student Phone]" +
              ") VALUES " +
              "('" + sLast + "','" + sFirst + "','" +
              sNum + "','" + sBox + "','" + sAccom +"');";
           
            System.out.println("sLast:" +sLast);
            System.out.println("sFirst:" +sFirst);
           
            String selectStudentSql = "SELECT " +
            "StudentID, [First Name], [Last Name] FROM Student " +
            "WHERE [Last Name]= "
            + sLast + " AND [First Name] = "+ sFirst +";";
           
            rs = stm.executeQuery( selectStudentSql );  <<<<<<======Line 450
0
Comment
Question by:komlaaa
  • 5
  • 2
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13650294
I think you forgot the quotes
0
 
LVL 37

Accepted Solution

by:
zzynx earned 800 total points
ID: 13650302
...
 "WHERE [Last Name]= '" + sLast + "' AND [First Name] = '"+ sFirst +"';";
                                     ^                  ^                              ^                 ^
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13650314
Mmmm... the positioning of the ^ indicators isn't right.

I hope you know what I mean: The result should be:

  ... WHERE [Last Name]= 'blablabla' AND [First Name] = 'blabla';
where you had
  ... WHERE [Last Name]= blabla AND [First Name] = blabla;
0
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!

 
LVL 37

Expert Comment

by:zzynx
ID: 13650328
Or you can use a PreparedStatement
0
 
LVL 92

Assisted Solution

by:objects
objects earned 240 total points
ID: 13650354
String selectStudentSql = "SELECT " +
            "StudentID, [First Name], [Last Name] FROM Student " +
            "WHERE [Last Name]= ? AND [First Name] = ?";
PreparedStatement ps = accessCon.prepareStatement(selectStudentSql);
ps.setObject(1, sLast);
ps.setObject(2, sFirst);
rs = ps.executeQuery();

0
 

Author Comment

by:komlaaa
ID: 13651764
Thanks, i should realize that on my own
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13662744
Thanks for accepting
0
 
LVL 92

Expert Comment

by:objects
ID: 13667482
:)
0

Featured Post

Industry Leaders: 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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

609 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