You are on page 1of 3

Practical 11

Aim:- Develop a database application that uses any JDBC driver.

import java.sql.*;

public class pract11

static final String jdbc_Driver="com.mysql.cj.jdbc.Driver";

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

static final String user="root";

static final String pass="Kartik";

public static void main(String []args)

Connection conn=null;

Statement stmt=null;

try{

Class.forName(jdbc_Driver);

System.out.println("Connecting Databases...");

conn= DriverManager.getConnection(db_url,user,pass);

System.out.println("Connection to Databases ESTABLISHED...");

System.out.println("Creating Statement...");

stmt=conn.createStatement();

ResultSet rs;

int count=0;

rs=stmt.executeQuery("Select Count(*)'age' from Employees");

while(rs.next()){

count=rs.getInt(1);

System.out.println(count);
rs=stmt.executeQuery("Select * from Employees");

while(rs.next())

int id=rs.getInt("id");

int age=rs.getInt("age");

String name=rs.getString("name");

System.out.print(" ID="+id);

System.out.print(" Name="+name);

System.out.println(" age="+age);

rs.close();

stmt.close();

conn.close();

}catch(SQLException se){

se.printStackTrace();

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(conn!=null)

conn.close();

}catch(SQLException se){

se.printStackTrace();

System.out.println("Good Bye..");

}
Output:-

You might also like