Results 1 to 16 of 16
- 02-08-2012, 02:25 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
unknown column 'abc' in where clause in mysql
i am doing a java project using jdbc and swing frames. The following code works
String strupdate = "delete from booktable where Book_ISBN = 88664488";
jcb.stat.executeUpdate(strupdate);
but when i want to make it a little advanced and general by writing
String strdeleteitem = JOptionPane.showInputDialog("Enter the Book_ISBN of the book");
String strupdate = "delete from booktable where Book_ISBN = strdeleteitem";
jcb.stat.executeUpdate(strupdate);
it doesnot work ..
shows the error
unknown column 'strdeleteitem' in where clause in mysql .........
please help me....
- 02-08-2012, 02:56 PM #2
Re: unknown column 'abc' in where clause in mysql
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-08-2012, 03:07 PM #3
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Re: unknown column 'abc' in where clause in mysql
You has try changed code from:
String strupdate = "delete from booktable where Book_ISBN = strdeleteitem";
to:
String strupdate = "delete from booktable where Book_ISBN = " + strdeleteitem;
Good luck!
- 02-08-2012, 03:09 PM #4
- 02-08-2012, 04:45 PM #5
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Re: unknown column 'abc' in where clause in mysql
I think this problem is bug, and how to fix it. and I will waiting answer of "sandeshyadav" .^-^.
Thank your sharing.
- 02-08-2012, 05:56 PM #6
- 02-08-2012, 06:07 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
- 02-08-2012, 06:16 PM #8
Re: unknown column 'abc' in where clause in mysql
I'm happy you found the tutorial useful. Now to address this:
You would need to include single quotes as part of the String literal so as to wrap the String variable value passed to the Statement.These quirks, which facilitate SQL injection (Google it) and also cause problems with String variable values that themselves contain single or double quote characters, are the reason you should use PreparedStatement -- which takes care of any and all necessary quoting without you having to bother about it.Java Code:String strupdate = "delete from booktable where Book_ISBN = '" + strdeleteitem + "'";
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-08-2012, 06:21 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
Re: unknown column 'abc' in where clause in mysql
exactly ...... that is very true sir ........ i broke my head all day trying different quote tricks \'', \' + & and so on .... but none worked ... finally prepared statement gave me the solution ....
- 02-08-2012, 08:15 PM #10
Re: unknown column 'abc' in where clause in mysql
Why you should never use Statement in conjunction with user input:

(From xkcd: Exploits of a Mom)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-09-2012, 05:52 PM #11
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Re: unknown column 'abc' in where clause in mysql
This code for your use "Statement" and you have chosen solution "PreparedStatement", it is the smart choice.
I think I has learn more from "DarrylBurke" when offer a solution, thank you.
- 02-12-2012, 05:08 AM #12
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
Re: unknown column 'abc' in where clause in mysql
Can i use prepared statement for "(SELECT * FROM ?") ?
This code is showing SQL exception ...... Please help..............
pstat = con.prepareStatement("SELECT * FROM ?");
pstat.setString(1,strtable);
rs = pstat.executeQuery();
rsmd = rs.getMetaData();
int colcount =rsmd.getColumnCount();
for(int i = 1; i <= colcount; i++){
columns.addElement(rsmd.getColumnName(i));
}
while (rs.next()){
Vector<Object> row = new Vector<Object>();
for(int i = 1; i <= colcount; i++){
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
pstat.close();
btable = new JTable(data, columns);
bscrollpane = new JScrollPane(btable);
- 02-12-2012, 07:25 AM #13
Re: unknown column 'abc' in where clause in mysql
You could find this for yourself -- learn to search the net.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-12-2012, 09:35 AM #14
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
Re: unknown column 'abc' in where clause in mysql
thanx but i searched a lot .... when i could not find for a long time i asked . anyways thanx ........
- 02-13-2012, 11:06 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: unknown column 'abc' in where clause in mysql
You can only bind values, not things like column names or table names.
- 02-13-2012, 09:31 PM #16
Member
- Join Date
- Feb 2012
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
hibernate unknown column
By arpho in forum New To JavaReplies: 0Last Post: 10-15-2011, 11:27 AM -
import Excel file into Mysql DB via Java unknown problem
By black300 in forum JDBCReplies: 0Last Post: 04-02-2011, 10:22 AM -
Unknown column 'id' in 'field list'
By peace76 in forum Java ServletReplies: 5Last Post: 12-21-2010, 01:54 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
Mysql LIKE clause problem
By stalkerism in forum JDBCReplies: 1Last Post: 07-31-2009, 10:01 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks