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

    Default Database help... 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?

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

    Default

    Where are the commas separating these values?
    Java Code:
    +"'"+ list_1 +"'"+ DayOff +""+"'"
    You should really use PreparedStatement and not cobble together statements like this. That will prevent a lot of these sorts of errors, as well as automatically correctly formatting values, and help to prevent SQL Injection attacks.

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

    Default

    hey, in ur query u havnt properly closed the string literal........
    executeQuery("Insert...........()"); tht's y it is givin u error.

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

    Default

    by the way..u shudnt use executeQuery() for an insert stmt. so chnge it to executeUpdate() & chk again!

  5. #5
    venkatallu is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    hey,u should not use executeQuery() for insert.U have to use executeUpdate().
    And u didnt close brackets properly in Insert query.

    The correct one is ,

    executeUpdate("INSERT INTO Table1 (list_1,DayOff,MonthOff,YearOff,DayBack,MonthBack, YearBack) VALUES("+"'"+ list_1 +"'"+ DayOff +""+"'"+ MonthOff +""+"'"+ YearOff +""+"'"+ DayBack+""+"'"+ MonthBack +""+"'"+ YearBack +"));

    Try the above one.

Similar Threads

  1. Database String... whats wrong?
    By neosnokia in forum New To Java
    Replies: 10
    Last Post: 06-05-2009, 12:40 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
  •