Results 1 to 4 of 4
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy cant find the error in the following SQL statemen but it just wont work!!

    I have the following codein my project.My IDE(netbeans) points out an error in the
    Java Code:
    executeUpdate
    statement which I cant spot.Anyone willing to proof read and point out the error?
    Java Code:
    //All unseen variablesdeclaration and constructors were declared above^^^
    public void registerNewStudent()
        {
            String fname=newStudent.getFirstName();
            String lname = newStudent.getLastName();
            String regNumber = newStudent.getKURegNumber();
            String campus = newStudent.getCampus();
            String nationalID = newStudent.getNational_ID();
            String courseCode,
                    studentID,
                    yearOfEntry;
            String str = regNumber;
            String split [];
            split = str.split("/");
          
            courseCode = split[0];
            studentID = split[1];
            yearOfEntry = split[2];
    
            Connection con = null;
            try {
                Class.forName(DRIVER).newInstance();
                Properties p = new Properties();
                p.put("user", "root");
                p.put("password", "mysecret");
                con = DriverManager.getConnection(DATABASE_URL,p);
                Statement stmt = con.createStatement();
                stmt.executeUpdate("INSERT into kustudents (StudentID, FirstName, 
             // ^^^^^^^everything in the project is fine until here ^^^^:confused:
    LastName, CourseCode, YearOfEntry, RegNumber, National_ID, Campus) " +
                                                 "VALUES ("+studentID+",'"+fname+"','"+lname+"','"+courseCode+"','"+yearOfEntry+"','" +regNumber+"','" +nationalID+"','"+campus+"')");
      //I have a feeling I messed up with the above.
                stmt.close();
                System.out.print("Thank you for registering..");
            }
    //all the rest proceeds:p

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't create queries by concatenating strings like that.
    Use a PreparedStatement instead.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Like what ro35198x said ,u have to use PreparedStatement for this things.

    Another suggestion,u should not directly give big query statement for execution.debugging will be a problem.Use StringBuffer for framing query and then use toString() method for stringbuffer object and use it in executeupdate().Before passing just put s.o.p for seeing ur query and u can easily correct if something goes wrong.
    Ramya:cool:

  4. #4
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

Similar Threads

  1. error cannot find symbol
    By jcoon3 in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 10:56 PM
  2. 'Cannot find symbol' error
    By minihazard10 in forum New To Java
    Replies: 6
    Last Post: 10-10-2008, 04:05 AM
  3. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 AM
  4. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM
  5. Error: cannot find symbol
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •