N. Achyut java Java program using JDBC to extract name of those students from Table

Java program using JDBC to extract name of those students from Table

Java program using JDBC to extract name of those students

Write a Java program using JDBC to extract name of those students who live in Kathmandu district,assuming that the student table has four attributes (ID, name, district, and age).

You need to add library called : mariadb-java-client-2.6.0 in your package, just download from mariadb java client side .

Figure : Source file lookup

Before Writing the code please run the Xamp server and create a database name onlinelab and add a table called student with ( id, name, district and age) column name.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Achyut1324
 */
public class Question1 {
    public static void main(String[] args) throws SQLException {
        String url = "jdbc:mariadb://localhost:3306/onlinelab";
        String username = "root";
        String password = "";
        Connection connection = DriverManager.getConnection(
                url, username, password);
        Statement statement = connection.createStatement();
        String sql = "select * from student where district = 'katmandu' ";
        ResultSet rs = statement.executeQuery(sql);
        while(rs.next()) {
            System.out.printf("%d %s %s %d n",
                rs.getInt("id"),
                rs.getString("name"),
                rs.getString("district"),
                rs.getInt("age")
            );
        }
        statement.close();
        connection.close();
    }
}

Output Of the Program

run:
1 ram katmandu 22
BUILD SUCCESSFUL (total time: 0 seconds)

Related Post