Results 1 to 11 of 11
  1. #1
    neosnokia is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Database String... whats wrong?

    Hi all,

    I am trying to make an SQL statement which inserts values in to a database. So far, i have typed this in but it doesnt work and Netbeans gives a red error line underneath the line which reads ResultSet theResult=theStatement.executeQuery("INSERT INTO etc etc

    Java Code:
        public void inserttoDb ()
        {
            try
            {
              //Directory may need to be changed for differnt computer
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ= F:/JDB/sicksys.mdb";
              theConnection = DriverManager.getConnection(myDB);
              Statement theStatement=theConnection.createStatement();
              ResultSet theResult=theStatement.executeQuery("INSERT INTO Table1 (list_1,DayOff,MonthOff,YearOff,DayBack,MonthBack,YearBack VALUES("+"'"+ list_1 +"'"+ DayOff +""+"'"+ MonthOff +""+"'"+ YearOff +""+"'"+ DayBack+""+"'"+ MonthBack +""+"'"+ YearBack +")));
    
            }
    
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
    Can anyone see anything majorly wrong with this?
    Last edited by neosnokia; 06-04-2009 at 07:15 PM.

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    I'm sure Netbeans also TELLS you WHY it's put a red line under it, but my guesses would be either you've got your quotes muddled or you haven't imported a class.

  3. #3
    neosnokia is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Yes Netbeans does say "unclosed string literal" which means nothing to me as this is the first time i have done something with Java.

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Aha! OK, what it's telling you (albeit in a fundamentally stupid way) is that you've got your quotes muddled.

    So you need to go very carefully through your string. Each time you want a particular piece of text added verbatim to your query, surround that text in exactly one quote either side. When/if you want an actual quote character in your string, you write either a double quote, or \". (In many variants of SQL, you actually usually use single quotes, so maybe this doesn't apply.)

    It might help you to write your code more like this:

    Java Code:
    String query = "SELECT Blah FROM Bluu WHERE Bling = ";
    query += "'" + theValueToCheck + "'";
    query += " AND Status = 1";
    rather than as one big long confusing line.

  5. #5
    neosnokia is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    i dont get what you mean :-( I am trying to insert values form a few variables :
    -list_1
    -DayOff
    -MonthOff
    -YearOff
    -DayBack
    -MonthBack
    -YearBack

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    ResultSet theResult = theStatement.executeQuery("INSERT INTO Table1 (list_1,DayOff,MonthOff,YearOff,DayBack,MonthBack,YearBack [COLOR="Red"])[/COLOR]VALUES(" + "'" + list_1 + "'" + DayOff + "[COLOR="Red"]'[/COLOR]" + "'"+ MonthOff +""+"'"+ YearOff +""+"'"+ DayBack+""+"'"+ MonthBack +""+"'"+ YearBack +")));
    you missed some ")", ",", "'" in the sql
    please check it
    Last edited by mtyoung; 06-05-2009 at 05:18 AM.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See your thread in the database forum.

    Please don't cross-post like this.

  8. #8
    vrk's Avatar
    vrk
    vrk is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    hey,
    u r inserting values into db,
    so...u need to put st.executeUpdate("INSERT......."); instead of executeQuery();.....this is for SELECT stmt's ok.
    now u can chk for values getting inserted r nt.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That is also a problem, yes, but the syntax is what caused the current problem. This would have been the next. ;-)

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by neilcoffey View Post
    Java Code:
    String query = "SELECT Blah FROM Bluu WHERE Bling = ";
    query += "'" + theValueToCheck + "'";
    query += " AND Status = 1";
    To extend this, since we may as well get you on the correct path from the start, you should be using a PreparedStatement, which allows you to declare placeholders for the values you're inserting/selecting on...so the above becomes:

    Java Code:
    String query = "SELECT Blah FROM Bluu WHERE Bling = ? AND Status = 1";
    The question mark is the placeholder.
    To use this you would do:

    Java Code:
    PreparedStatement ps = theConnection.prepareStatement();
    ps.setString(1,theValueToCheck); // This sets the first '?' in your query to the value of theValueToCheck.
    // Then get the results.
    ResultSet theResult = ps.executeQuery();

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

    Default

    Hi,
    Use PreparedStatement as suggested by Toll.If you just build query with quotes and n number of values,surely u will end with error.That too preparedStatement are good in performance.

    Gothru the attached piece of code and execute and also gothru article related to PreparedStatement.

    Java Code:
     public void inserttoDb ()
        {
            try
            {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ= F:/JDB/sicksys.mdb";
              theConnection = DriverManager.getConnection(myDB);
    		  StringBuffer insertQueryBuffer = new StringBuffer();
    		  insertQueryBuffer.append("INSERT INTO TABLE1(LIST_1,DAYOFF,MONTHOFF,YEAROFF,DAYBACK, "); 
    		  insertQueryBuffer.append("MONTHBACK,YEARBACK VALUES(?,?,?,?,?,?,?)");	
              //Directory may need to be changed for differnt computer
              PreparedStatement theStatement=theConnection.prepareStatememt(insertQueryBuffer.toString());
    		  theStatement(1,list_1);
    	  	  theStatement(2,DayOff);
    		  theStatement(3,MonthOff);
    		  theStatement(4,YearOff);
    		  theStatement(5,DayBack);
    		  theStatement(6,MonthBack);
    		  theStatement(7,YearBack);
              int insertCount=theStatement.executeUpdate();
    		  System.out.println("InsertCount  "+ insertCount);
             }
    
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
    Ramya:cool:

Similar Threads

  1. Whats Wrong? Errors when compiling
    By software_dude_2009 in forum New To Java
    Replies: 5
    Last Post: 05-12-2009, 12:19 PM
  2. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 03:51 AM
  3. Whats wrong with my maths???
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 11-03-2008, 05:52 PM
  4. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:57 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 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
  •