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).


package JDBC;

import java.sql.*;

public class Q3_2072 {

static final String db_url="jdbc:mysql://localhost:3306/students";

public static void main(String[] args) {

Connection conn=null;

Statement statement=null;

ResultSet resultSet=null;

try{

//connection to database

conn=DriverManager.getConnection(db_url,"root","");

//sql query

String sql="Select *from student where address='dhading'";

//executing sql statement

resultSet=statement.executeQuery(sql);

ResultSetMetaData metaData=resultSet.getMetaData();

int numOfCols=metaData.getColumnCount();

System.out.println("Students------------");

//getting column names

for(int i=1;i<=numOfCols;i++){

System.out.printf("%-8s\t", metaData.getColumnName(i));

System.out.println();

}

//executing query

while(resultSet.next()){

for(int i=1;i<=numOfCols;i++){

System.out.printf("%-8s\t", resultSet.getObject(i));

System.out.println();

}

}

}catch(SQLException e){

e.printStackTrace();

}finally{

try

{

if(statement!=null){

statement.close();

resultSet.close();

conn.close();

}

}

catch(SQLException ex){

ex.printStackTrace();

}

}

}

}

Comments

Popular posts from this blog

What is the cloud cube model? Explain in context to the Jericho cloud cube model along with its various dimensions.

Explain cloud computing reference model .

Discuss classification or taxonomy of virtualization at different levels.