0

I have a mysql table named 'customer'. serialnumber (int), name(varchar), gender(varchar) are the table fields.

I have a int variable a = 2.

I want to fetch the value from the table when serialnumber = a. That is when the serialnumber = 2 it should display the particular customer's name.

String url="jdbc:mysql://localhost/shopping";
String usr ="root";
String pass = "rootpassword";

session.setAttribute( "URL", url );
session.setAttribute( "user", usr );
session.setAttribute( "passwd", pass );

String connectionURL = (String)session.getAttribute( "URL" );
String user = (String)session.getAttribute("user");
String passwd = (String)session.getAttribute("passwd");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      connection = DriverManager.getConnection(connectionURL, user, passwd);
      statement = connection.createStatement();

rs=statement.executeQuery("select * from customer where serialnumber like '%a%' ");

      while (rs.next()) {
      out.println("Customer Name: ");
      out.println(rs.getString("name")); 
     }rs.close();
    }catch(Exception ce){out.println(ce);} 


 }

This throws me nothing. But when i give the number instead a in rs.execute Query it displays the customer name. How to solve this and what is my mistake??

Thanks

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by |-|x
1

I can't see where your variable a is getting defined or set, but presumably it is a java variable available in the current scope and all you need to do is build it into the sql string.

rs=statement.executeQuery("select * from customer where serialnumber like '%"+a.ToString()+"%' ");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.