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

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()+"%' ");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.