text: Q:
  
  Displaying data from database using keyboard in jdbc
  
  I am working on a JDBC project for the first time. I have a table in a MySQL database as shown in image:
  
  This is my code which displays complete table
  //load Driver class
  Class.forName("com.mysql.jdbc.Driver");
  
  //get Connection
  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/School", "root", "1947");
  
   //get statement
  Statement stmt=con.createStatement(); 
  
   //load ResultSet
  ResultSet rs1=stmt.executeQuery("select  *from Student");
  
  I want to display data according to rollno i.e System first ask for a rollno. When the user inputs the rollno, it should display all the details related to this rollno. Is it possible?    
  
  A:
  
  Use Scanner to ask for rollno and PreparedStatement to set parameters in your query
      java.util.Scanner sc = new java.util.Scanner(System.in);
      int rollNo = sc.nextInt();
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
          ps = conn.prepareStatement("select * from Student where rollno=?");
          ps.setInt(1, rollNo);
          rs = ps.executeQuery();
          while (rs.next()) {
              Integer rollno = rs.getInt("rollno");
              String name = rs.getString("name");
              //other columns
          }
      } catch (Exception e) {
          e.printStackTrace();
      }finally{
          try {
              rs.close();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
          try {
              ps.close();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
          try {
              conn.close();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
  