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

VR Ajp 18 To 20

The document contains code snippets for connecting to a database and performing CRUD (create, read, update, delete) operations using JDBC. The code examples demonstrate how to retrieve data from a database table where the percentage is greater than 70, update a record by changing the age field, update a record by changing the name field, and delete records where the price is greater than 500.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
801 views

VR Ajp 18 To 20

The document contains code snippets for connecting to a database and performing CRUD (create, read, update, delete) operations using JDBC. The code examples demonstrate how to retrieve data from a database table where the percentage is greater than 70, update a record by changing the age field, update a record by changing the name field, and delete records where the price is greater than 500.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical No.

18: Write a Program to insert and retrieve data from


database using JDBC.
2. Develop a program to display the name and roll_no of students from
“Student table” having percentage>70.
Program:-

import java.sql.*;

public class sqldemo18{

public static void main(String args[]){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("connecting to database");

Connection con=DriverManager.getConnection("jdbc:odbc:nespdsn");

System.out.println("Connected to database");

String s="Select * from Table1 where Percentage>70";

Statement s1=con.createStatement();

ResultSet rs=s1.executeQuery(s);

while(rs.next()){

System.out.println(rs.getInt("ID"));

System.out.println(rs.getString("Name"));

System.out.println(rs.getInt("Percentage"));

con.close();

catch(Exception e){

System.out.println(e);

}
}

Output:-
Practical No. 19: Write a Program to demonstarte the use of
PreparedStatement and ResultSet interface.

2.Develop a program to update a record in database table.

Program:-

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

public class sqldemo19{

public static void main(String args[]){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("Connecting to Database");

Connection con=DriverManager.getConnection("jdbc:odbc:nespdsn");

System.out.println("Connected to Database");

String s="update Table1 set Age=17 where Rollno=3";

PreparedStatement st=con.prepareStatement(s);

st.executeUpdate();

con.close();

catch(Exception e){

System.out.println("Connection error");

}
Output:-
Practical No. 20: Write a program to update and delete a record
from a database table.
1.Develop a program to update name of student from Jack to John.
Program:-

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

public class sqldemo20{

public static void main(String args[]){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("Connecting to Database");

Connection con=DriverManager.getConnection("jdbc:odbc:nespdsn");

System.out.println("Connected to Database");

String s="update Table1 set Name='Jack' where Name='John'";

PreparedStatement st=con.prepareStatement(s);

st.executeUpdate();

con.close();

catch(Exception e){

System.out.println("Connection error");

}
Output:-
2.Develop a program to delete all record for product whose “price is greater

than 500” and ID is “P1234”.

Program:-
import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

public class sqldemo20demo{

public static void main(String args[]){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("Connecting to Database");

Connection con=DriverManager.getConnection("jdbc:odbc:nespdsn");

System.out.println("Connected to Database");

String s="delete from Table1 where Price>500";

PreparedStatement st=con.prepareStatement(s);

st.executeUpdate();

con.close();

catch(Exception e){

System.out.println(e);

}
Output:-

You might also like