Coders Packet

Display Student details using JDBC

By Sayali Dipak Pokharkar

  • Student_details_JDBC/
  • In this project, We are going to display all students details like ID, name, address, marks, grade, etc. from MySQL table using JDBC.

    import java.sql.*;
    import java.lang.*;
    public class Student_Details {
      public static void main(String[] args) {
        Connection con;
        Statement st;
        ResultSet rs;
        try
        {
          Class.forName("com.mysql.cj.jdbc.Driver");
          con= DriverManager.getConnection("jdbc:mysql://localhost:3306/practice","root","root");
          st=con.createStatement();
          rs=st.executeQuery("select * from student");
          while(rs.next())
          {
            //System.out.println(rs.getInt(1));
            System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getInt(4)+" "+rs.getString(5));
          }
          rs.close();
          st.close();
          con.close();
        }
        catch(Exception e)
        {
          //System.out.println(e);
      
          e.printStackTrace();
        }
        
      }
    
    }
    

    Here, We are establishing a connection between MySQL and JAVA JDBC(Java Data Base Connectivity) using Connection class by creating it's class object named as 'con'.

    Then, we prepare a MySql query statement for display student data from the 'student' table, for executes this query, we are creating object 'st' of Statement class that used to execute query by using executeQuery() method. A

    fter that, We create the object 'rs' of ResultSet class for fetched the data from the database.

    Required installations: MySql, MySql connector, eclipse

    Note: Attached code along with the required instructions and file

    Download Complete Code

    Comments

    Comments for the packet "Display Student details using JDBC";
    No comments yet