Results 1 to 11 of 11
Thread: Database String... whats wrong?
- 06-04-2009, 07:12 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
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
Can anyone see anything majorly wrong with this?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()); }Last edited by neosnokia; 06-04-2009 at 07:15 PM.
- 06-04-2009, 08:30 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.
Neil Coffey
Javamex - Java tutorials and performance info
- 06-04-2009, 08:34 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
Yes Netbeans does say "unclosed string literal" which means nothing to me as this is the first time i have done something with Java.
- 06-04-2009, 08:39 PM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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:
rather than as one big long confusing line.Java Code:String query = "SELECT Blah FROM Bluu WHERE Bling = "; query += "'" + theValueToCheck + "'"; query += " AND Status = 1";
Neil Coffey
Javamex - Java tutorials and performance info
- 06-04-2009, 09:27 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 27
- Rep Power
- 0
i dont get what you mean :-( I am trying to insert values form a few variables :
-list_1
-DayOff
-MonthOff
-YearOff
-DayBack
-MonthBack
-YearBack
- 06-05-2009, 05:14 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
you missed some ")", ",", "'" in the sqlJava 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 +")));
please check itLast edited by mtyoung; 06-05-2009 at 05:18 AM.
- 06-05-2009, 05:57 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
See your thread in the database forum.
Please don't cross-post like this.
- 06-05-2009, 07:11 AM #8
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.
- 06-05-2009, 08:13 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
That is also a problem, yes, but the syntax is what caused the current problem. This would have been the next. ;-)
- 06-05-2009, 12:16 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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:
The question mark is the placeholder.Java Code:String query = "SELECT Blah FROM Bluu WHERE Bling = ? AND Status = 1";
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();
- 06-05-2009, 12:40 PM #11
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
-
Whats Wrong? Errors when compiling
By software_dude_2009 in forum New To JavaReplies: 5Last Post: 05-12-2009, 12:19 PM -
Whats wrong with this code?
By bbtgirl in forum New To JavaReplies: 2Last Post: 02-25-2009, 03:51 AM -
Whats wrong with my maths???
By soc86 in forum New To JavaReplies: 4Last Post: 11-03-2008, 05:52 PM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
Whats wrong with my code???
By Soda in forum New To JavaReplies: 2Last Post: 12-06-2007, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks