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

exp18_ajp

The document contains Java programs that demonstrate how to interact with a database using JDBC. It includes examples for creating and inserting data into student and employee tables, as well as retrieving student names and roll numbers based on a percentage condition. Each program establishes a connection to a database and executes SQL commands to manipulate data.

Uploaded by

jsamiksha051
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)
1 views

exp18_ajp

The document contains Java programs that demonstrate how to interact with a database using JDBC. It includes examples for creating and inserting data into student and employee tables, as well as retrieving student names and roll numbers based on a percentage condition. Each program establishes a connection to a database and executes SQL commands to manipulate data.

Uploaded by

jsamiksha051
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/ 5

EXPERIMENT NO 18: WRITE A PROGRAM TO INSERT AND RETRIVE DATA FROM DATABASE

USING JDBC.

OUTPUT OF THE PROGRAM GIVEN

import java.sql.*;
public class EXP18 {
public static void main(String args[])
{
try {
Connection con =
DriverManager.getConnection("jdbc:ucanaccess://EXP18_ALL.accdb
");
System.out.println("Connection Established");
Statement st = con.createStatement();
//ss.executeUpdate("CREATE TABLE EMPLOYEE
(EMP_NAME VARCHAR(30),EMP_ID INTEGER,)");
// System.out.println("TABLE CREATED");
String str= "select* from student";
ResultSet rs=st.executeQuery(str);
String text="";
System.out.println("Roll Number \t Name");
while(rs.next())
{
text= text+rs.getInt(2)+" \t
"+rs.getString(1)+"\n";
}
System.out.print(text);
st.close();
con.close();
}

catch (Exception e) {
System.out.println(e);
}
}
}
OUTPUT:
Q1)write a program to create a student table in database and insert a record in a student
table.

import java.sql.*;
public class EXP18 {
public static void main(String args[])
{
try {
Connection con =
DriverManager.getConnection("jdbc:ucanaccess://EXP18_ALL.accdb
");
System.out.println("Connection Established");
Statement ss = con.createStatement();
//ss.executeUpdate("CREATE TABLE STUDENT (NAME
VARCHAR(30),ROLLNO INTEGER,PERCENTAGE INTEGER)");
System.out.println("TABLE CREATED");
ss.executeUpdate("INSERT INTO STUDENT
VALUES('SAMIKSHA',24,94)");
ss.executeUpdate("INSERT INTO STUDENT
VALUES('KALPANA',70,80)");
ss.executeUpdate("INSERT INTO STUDENT
VALUES('RAHUL',10,70)");
System.out.println("rows inserted");
}
catch (Exception e) {
System.out.println(e);
}
}
}

OUTPUT:
Q2)Develop a program to create employee table in database having two columns
“emp_id” and “emp_name”.

import java.sql.*;
public class EXP18 {
public static void main(String args[])
{
try {
Connection con =
DriverManager.getConnection("jdbc:ucanaccess://EXP18_ALL.accdb
");
System.out.println("Connection Established");
Statement ss = con.createStatement();
ss.executeUpdate("CREATE TABLE EMPLOYEE (EMP_NAME
VARCHAR(30),EMP_ID INTEGER,)");
System.out.println("TABLE CREATED");
ss.executeUpdate("INSERT INTO EMPLOYEE
VALUES('SAMIKSHA',123)");
ss.executeUpdate("INSERT INTO EMPLOYEE
VALUES('VAISHNAVI',100)");
ss.executeUpdate("INSERT INTO EMPLOYEE
VALUES('ANUSHKA',111)");
System.out.println("rows inserted");
}
catch (Exception e) {
System.out.println(e);
}
}
}

OUTPUT:

Q3)Develop a program to display the name and roll_no of students from “student table”
having percentage>70.

import java.sql.*;
public class EXP18 {
public static void main(String args[])
{
try {
Connection con =
DriverManager.getConnection("jdbc:ucanaccess://EXP18_ALL.accdb
");
System.out.println("Connection Established");
Statement ss = con.createStatement();
//ss.executeUpdate("CREATE TABLE STUDENT (NAME
VARCHAR(30),ROLLNO INTEGER,PERCENTAGE INTEGER)");
System.out.println("TABLE CREATED");
/*ss.executeUpdate("INSERT INTO STUDENT
VALUES('SAMIKSHA',24,94)");
ss.executeUpdate("INSERT INTO STUDENT
VALUES('KALPANA',70,80)");
ss.executeUpdate("INSERT INTO STUDENT
VALUES('RAHUL',10,70)");
System.out.println("rows inserted");*/
ResultSet res =ss.executeQuery("select * from
student where percentage>70");
while(res.next())
{

System.out.println("name :"+res.getString(1)+"||Roll
no :"+res.getInt(2));
}
}
catch (Exception e) {
System.out.println(e);
}
}
}

You might also like