VR Ajp 18

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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