Steps to connect with Database

Aniket GudgalAniket Gudgal
1 min read

In this blog, we can directly connect with the database and perform the query execution

Steps:

  1. Load the Driver:

    Class.forName("com.mysql.jdbc.Driver")

  2. Create a Connection:

    Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dbname", "root" , "root");

  3. Create query, Statement, PreparedStatement, CallableStatement

    e.g

    String q = "SELECT * FROM students";

    Statement stmt = con.createstatement();

    ResultSet set = stmt.executeQuery(q);

  4. Process the data:

    while(set.next())

    {

    int id = set.getInt("studentID");

    String name = set.getString("studentName");

    System.out.println(id);

    System.out.println(name);

    }

  5. Close the Connection:

    con.close();

1
Subscribe to my newsletter

Read articles from Aniket Gudgal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aniket Gudgal
Aniket Gudgal