0% found this document useful (0 votes)
38 views15 pages

JDBC Programs

The document contains Java code for programs to connect to a database and retrieve or display data from tables. It includes programs to display all data, data for a specific student based on roll number or name provided by the user, and a menu program to choose these display options. The code makes use of JDBC and provides the necessary imports, driver registration, connection string and SQL queries to retrieve data and display it on the command line.

Uploaded by

Isha Thakre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views15 pages

JDBC Programs

The document contains Java code for programs to connect to a database and retrieve or display data from tables. It includes programs to display all data, data for a specific student based on roll number or name provided by the user, and a menu program to choose these display options. The code makes use of JDBC and provides the necessary imports, driver registration, connection string and SQL queries to retrieve data and display it on the command line.

Uploaded by

Isha Thakre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Advance Java Programs

Chapter Name: - JDBC


Name: - Angad Aware
Sec: - B
Roll No: - 09

/* 36.Write a java program to display all data from the database on the command
prompt.*/
import java.io.*;
import java.lang.*;
import java.sql.*;
class xyz
{
Connection con;
Statement stmt;
ResultSet rs;
String sql;

public xyz()
{
try
{
Class.forName("sun.Jdbc.Odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("Jdbc:Odbc:tydsn");

}
catch(Exception e)
{
System.out.println(e);
}

1
}

public void display()


{
try
{
sql="select * from tytable";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())
{
System.out.println("\n RollNO \t NAME \t PERCENTAGE \t
COURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));

}while(rs.next());
}
else
{
System.out.println("Database Not Found");
}
}
catch(Exception e)
{
System.out.println(e);
}

2
}
}
class Ex36
{
public static void main(String args[])
{
xyz x=new xyz();
x.display();
}
}

/* 37.Write a java program to display all data from the database on the command
prompt.*/
import java.io.*;
import java.lang.*;
import java.sql.*;
class abc
{
Connection con;
Statement stmt;
ResultSet rs;
String sql;

public abc()
{
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://tydb.accdb");

3
}
catch(Exception e)
{
System.out.println(e);
}
}

public void display()


{
try
{
sql="select * from tytable";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())
{
System.out.println("\n\tRollNO\tNAME\tPER\tCOURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));
System.out.println("\n\n");

}while(rs.next());

}
else

4
{
System.out.println("No Data Found");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Ex37
{
public static void main(String args[])
{
abc x=new abc();
x.display();
}
}

/* 38.Write a java program to accept the roll Number from user and display all data of
that particular student from the database on the command prompt.*/
import java.io.*;
import java.lang.*;
import java.sql.*;
class abc
{
Connection con;
Statement stmt;
ResultSet rs;
String sql;
int r=0;

5
public abc()
{
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://tydb.accdb");

}
catch(Exception e)
{
System.out.println(e);
}
}

public void display()


{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the Roll No :: ");
r=Integer.parseInt(br.readLine());

sql="select * from tytable where rollno = "+r;


stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())
{

6
System.out.println("\n\tRollNO\tNAME\tPER\tCOURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));
System.out.println("\n\n");

}while(rs.next());

}
else
{
System.out.println("No Data Found");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Ex38
{
public static void main(String args[])
{
abc x=new abc();
x.display();
}
}

7
/* 39.Write a java program to accept the student name from user and display all data of
that particular student from the database on the command prompt.*/
import java.io.*;
import java.lang.*;
import java.sql.*;
class abc
{
Connection con;
Statement stmt;
ResultSet rs;
String sql;
String nm;

public abc()
{
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://tydb.accdb");

}
catch(Exception e)
{
System.out.println(e);
}
}

public void display()


{

8
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the Student Name :: ");
nm=br.readLine();

sql="select * from tytable where name='"+nm+"'";


stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())
{
System.out.println("\n\tRollNO\tNAME\tPER\tCOURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));
System.out.println("\n\n");

}while(rs.next());

}
else
{
System.out.println("No Data Found");
}
}
catch(Exception e)

9
{
System.out.println(e);
}
}
}
class Ex39
{
public static void main(String args[])
{
abc x=new abc();
x.display();
}
}

/* 40.Write a java program to display below options on the command prompt.


1. Display All Records
2. Display Records by Roll No
3. Display Records By Name
*/
import java.io.*;
import java.lang.*;
import java.sql.*;
class abc
{
Connection con;
Statement stmt;
ResultSet rs;
String sql;
String nm;
int ch=0;
int r=0;

10
public abc()
{
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://tydb.accdb");

}
catch(Exception e)
{
System.out.println(e);
}
}

public void display()


{
try
{
System.out.println("1. Display All Records");
System.out.println("2. Display Records by Roll No");
System.out.println("3. Display Records By Name\n");

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

System.out.print("Enter Your Choice :: ");


ch=Integer.parseInt(br.readLine());

switch(ch)

11
{
case 1:

sql="select * from tytable ";


stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())
{
System.out.println("\n\tRollNO\tNAME\tPER\
tCOURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));
System.out.println("\n\n");

}while(rs.next());

}
else
{
System.out.println("No Data Found");
}
break;

case 2:
System.out.print("\nEnter the Roll No :: ");
r=Integer.parseInt(br.readLine());

sql="select * from tytable where rollno = "+r;

12
stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())
{
System.out.println("\n\tRollNO\tNAME\tPER\
tCOURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));
System.out.println("\n\n");

}while(rs.next());

}
else
{
System.out.println("No Data Found");
}
break;

case 3:
System.out.print("\nEnter the Student Name :: ");
nm=br.readLine();

sql="select * from tytable where name ='"+nm+"'";


stmt=con.createStatement();
rs=stmt.executeQuery(sql);

if(rs.next())

13
{
System.out.println("\n\tRollNO\tNAME\tPER\
tCOURSE");
do
{
System.out.print("\t"+rs.getInt("rollno")+"\
t"+rs.getString("name")+"\t"+rs.getInt("percentage")+"\t"+rs.getString("course"));
System.out.println("\n\n");

}while(rs.next());

}
else
{
System.out.println("No Data Found");
}
break;
default:
System.out.println("Invalid Entry");
}

}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Ex40
{
public static void main(String args[])

14
{
abc x=new abc();
x.display();
}
}

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy