?
Solved

sql

Posted on 2005-03-28
8
Medium Priority
?
145 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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