0

I'm trying to query MySQL database

statement = conn.createStatement();
String sql = "select * from file_post where gas_key='"+commun+"'";
fp =statement.executeQuery(sql);

where commun is

String commun= (String) session.getAttribute("commun");

I'm getting exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'bro' in 'where clause'

bro is the value of commun.

I also tried PreparedStatement, but that also gave me the same exception.

But when I make a query in MySQL command line:

select * from file_post where gas_key='bro';

It's perfect there and returns the exact data.

I can't figure out why it is giving me the exception when using the same query in a Java class.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user3443275
  • 31
  • 1
  • 7

2 Answers2

1

This exception happens normally when you don't match the ", to avoid problems like this use a PreparedStatement

PreparedStatement st = con.prepareStatement("SELECT * FROM file_post WHERE gas_key=?");
st.setString(1,commun);
rs = st.executeQuery();
Jordi Castilla
  • 24,953
  • 6
  • 58
  • 97
0

Can you try to print preparedStatement after setting variable...just to check what query is building...

System.out.println(preparedStatement);

and what is fp here? ResultSet?

Mr. Noddy
  • 1,240
  • 1
  • 14
  • 40
  • yes sir fp is result set, n now the problem is solved , the problem was that the connections which i was getting from the connection pool was not working correctly, when i cleaned my project it started working – user3443275 Aug 19 '15 at 13:41
  • its good you found problem...and solved on your own...and dont call me sir please.....!! – Mr. Noddy Aug 20 '15 at 08:51