0% found this document useful (0 votes)
12 views

Practical 11 Java

This document outlines a Java program that develops a database application using JDBC and connects to a MySQL database. It imports necessary Java classes, defines connection credentials, connects to the database, executes queries to retrieve data and counts rows, prints the results, and closes resources.

Uploaded by

shivang patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Practical 11 Java

This document outlines a Java program that develops a database application using JDBC and connects to a MySQL database. It imports necessary Java classes, defines connection credentials, connects to the database, executes queries to retrieve data and counts rows, prints the results, and closes resources.

Uploaded by

shivang patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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