QN049. Write a program to display a record of database table (bases on specific field).
1. Objective
To display a record of database table based on specific field using java program.
2. Theory
In this program, we create statement and execute query and ResultSet object to get all data of database table.
3. Source Code
      import java.sql.*; 
      
      class JDBC49{
        public static void main(String[] args){
          try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/bca_third_2020?serverTimezone=UTC";
            String un = "root";
            String pw = "";
            Connection cn = DriverManager.getConnection(url,un,pw);
            display(cn);
          }catch(Exception e){
            System.out.println(e);
          }
        }
      private static void display(Connection cn) throws SQLException {
          Statement stat = cn.createStatement(); // statement create
          
          String query = "select name from student";
          System.out.println("Reading records...");
          ResultSet rs = stat.executeQuery(query); // execute query
          while(rs.next()) {
            //System.out.print("Roll NO: "+rs.getString("roll_no"));
            System.out.print(" Name: "+rs.getString("name"));
            //System.out.print(" Course: "+rs.getString("course"));
            System.out.println("\n");
          }
          
        }
      }
    
    4. Output
    Reading records... 
    Name: Sajan  
 
    Name: Gopal 
 
    Name: Srijana 
 
    Name: Hari 
    
5. Conclusion
In this way, I would like toconclude that, by using this program we can display specific field of database table.