Reputation: 95
I have a successfully connected Database Manager (simple Phonebook) with some functional options like add and get all entries. Now I need to prepare a Select option, that will allow the user to search for a first, last or full name in the Table.
The menu option is working, but I´m having some problem searching the Database for that specific name. What´s the correct syntax?
I´m trying something like this without success:
resultSet = statement.executeQuery("SELECT * FROM TableName WHERE ColumnName LIKE '%" + nameThatIsBeingSearched + "%');
I´m getting a com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'TABLE_NAME' in 'where clause'.
Is it possible to just get the column name automatically? There´s only one column, and I´m having difficulties trying to find it´s name.
Upvotes: 0
Views: 888
Reputation: 468
you can use the following syntax for adding enlargements to your sql query
String insertSQL = "SELECT * FROM DatabaseName WHERE ColumnName LIKE %?"
PreparedStatement stat = conn.prepareStatement(insertSQL);
stat.setString(1,nameThatIsBeingSearche);
ResultSet rs = stat.executeQuery();
Upvotes: 1
Reputation: 2266
The query looks correct, only instead DatabaseName should be the name of the table.
Upvotes: 0
Reputation: 10717
You should replace
resultSet = statement.executeQuery("SELECT * FROM DatabaseName WHERE ColumnName LIKE '%" + nameThatIsBeingSearched + "%');
By
resultSet = statement.executeQuery("SELECT * FROM TableName WHERE ColumnName LIKE '%" + nameThatIsBeingSearched + "%');
Upvotes: 0
Reputation: 993
The statement looks okay. What is your problem? No results or wrong results?
Upvotes: 0