Suppose I have a table:


and with the help of a Java program, I want to select the SID of the known port with the help of a SQL query:

Select SID from SERVICE where PORT=port

But when I write the code snippet:

String sql="Select SID from SERVICE where PORT=port;";

where port is an integer variable but on execution I get the following error:

No such column port".How to get rid of this error?
  • you need to pass that parameter(value) id the values of is is 'port' put it between the '' – Up_One Apr 1 '14 at 13:38
  • Your query is literally written as in the given example? And "port" variable is declared before? Because if it's so, "port" is not a literal (as it's not enclosed), neither is a integer (because it's not numeric). Please provide an actual code snippet on how you execute the query. – ddaniel Apr 1 '14 at 13:41

It is generally a terrible idea to construct SQL queries the way you currently do, as it opens the door to all sorts of SQL injection attacks. To do this properly, you'll have to use Prepared Statements instead. This will also resolve all sorts of escaping issues that you're evidently having at the moment.

String[] port = new String[] {"3306"};
    PreparedStatement statement = connection.prepareStatement("String sql="Select SID from SERVICE where PORT= ?");

    for (String name: names) {
        statement.setString(1, port);    
        ResultSet resultSet = statement.executeQuery();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.