• Post Reply Bookmark Topic Watch Topic
  • New Topic

Errors ";" and "may not have been initialized" on my project that I can't solve?  RSS feed

 
M Sparks
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am getting errors in my code (lines 26, 29 34) for ";" expected on String sql "INSERT INTO Customer VALUES ('"+ customer.getCustomerID() +"', '"+ customer.getFirstName() +"'," and "variable sql may not have been initialized on line [i]executeModifyQuery(sql); I have been adding and subtracting ";" everywhere but I just don't get it? I also don't get the other error as it is written 2 other times and is not returning an error? Any help for a new guy would be great. Thanks!

-M

[/i]
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Sparks wrote:

You have missed the assignment operator (=).
String sql = "INSET INTO....
String sql = "UPDATE...
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Few more things to remember.

1. It is better to use java.sql.PreparedStatement instead of java.sql.Statement.
  • It helps avoiding SQL Injection.
  • It makes your code more readable.
  • It is pre-compiled, so faster in case of executing same query multiple times with different parameters.

  • 2. It is better to release SQL objects in finally block instead of try block.
    Imagine a case when you got a Connection object and exception occurs before releasing it.
    Then after some time you will get "Maximum connections exceeded" or "Maximum open cursors exceeded" errors.
     
    M Sparks
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Awesome, can't believe I missed that! Also, I have already changed to PreparedStatement, that is way better. Thanks for your help!

    -M
     
    M Sparks
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a MySQL database I am connected to in NetBeans called "IT351". Any chance anyone could point me to a "how to" or explain briefly how to take all the data typed in the GUI and store in said MySQL database (IT351)? Then I how I would check to see the data in the database on NetBeans. Thanks.
     
    Tapas Chand
    Ranch Hand
    Posts: 614
    9
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    M Sparks wrote:I have a MySQL database I am connected to in NetBeans called "IT351". Any chance anyone could point me to a "how to" or explain briefly how to take all the data typed in the GUI and store in said MySQL database (IT351)? Then I how I would check to see the data in the database on NetBeans. Thanks.

    I have never used Netbeans, but as far as database is concerned, I always prefer database tools to manage them instead of Java IDE, MySQL Workbench for MySQL as an example.
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another reason why you couldn't find the compiler error may be that you are writing too much code at once. Write not more than five lines of code, compile and run it and check for errors. Then write the next five lines.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!