Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-04-2009, 08:56 PM
Member
 
Join Date: Feb 2009
Posts: 27
Rep Power: 0
neosnokia is on a distinguished road
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

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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-05-2009, 06:55 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Where are the commas separating these values?
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-06-2009, 09:40 AM
vrk's Avatar
vrk vrk is offline
Member
 
Join Date: Jan 2009
Posts: 24
Rep Power: 0
vrk is on a distinguished road
Default
hey, in ur query u havnt properly closed the string literal........
executeQuery("Insert...........()"); tht's y it is givin u error.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-06-2009, 09:43 AM
vrk's Avatar
vrk vrk is offline
Member
 
Join Date: Jan 2009
Posts: 24
Rep Power: 0
vrk is on a distinguished road
Default
by the way..u shudnt use executeQuery() for an insert stmt. so chnge it to executeUpdate() & chk again!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-10-2009, 12:17 AM
Member
 
Join Date: Sep 2008
Posts: 16
Rep Power: 0
venkatallu is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database String... whats wrong? neosnokia New To Java 10 06-05-2009 01:40 PM
Whats wrong with this code? bbtgirl New To Java 2 02-25-2009 04:51 AM
Whats wrong with my maths??? soc86 New To Java 4 11-03-2008 06:52 PM
Cannot understand whats wrong Lehane_9 New To Java 1 03-06-2008 08:57 PM
Whats wrong with my code??? Soda New To Java 2 12-06-2007 01:54 PM


All times are GMT +2. The time now is 03:00 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org