Practical No 20

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Practical No 20:

1.Write a program to delete a record from a table


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement; // Import Statement

import java.util.Scanner;

public class Emp {

public static void main(String[] args) {

String databaseURL = "jdbc:ucanaccess://C:/Users/admin/OneDrive/Documents/MSBTE.accdb";

try {

Connection con = DriverManager.getConnection(databaseURL);

Statement s = con.createStatement();

s.execute("CREATE TABLE SData ([STUD JD] int, STUD_Name varchar(255), BRANCH varchar(255))");

s.execute("DELETE FROM SData WHERE [STUD JD] = 101");

ResultSet rs = s.executeQuery("SELECT * FROM SData"); // Use executeQuery for SELECT

if (rs != null) {

while (rs.next()) {

System.out.println("ID of the Student: " + rs.getString(1));

System.out.println("Name of Student: " + rs.getString(2));

System.out.println("Branch of Student: " + rs.getString(3));

System.out.println("*******************************************");

rs.close();

s.close();

con.close();

} catch (Exception err) {

System.out.println("ERROR: " + err);

}
XIII. Exercise
1. Develop a program to update name of a student from Jack to John.
import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement; // Import Statement

import java.util.Scanner;

public class Emp2 {

public static void main(String[] args) {

try {

Connection con =
DriverManager.getConnection("jdbc:ucanaccess://C:/Users/admin/OneDrive/Documents/MSBTE.accdb");

Statement s = con.createStatement();

// s.execute("Create Table std (STUD_ID int, STUD_Name varchar(255), BRANCH varchar(255))");

s.executeUpdate("Update std set STUD_NAME = 'john' where STUD_NAME = 'Jack'");

// s.execute("select * from std");

ResultSet rs = s.getResultSet();

if (rs != null) {

while (rs.next()) {

System.out.println("Id of the Student: " + rs.getString(1));

System.out.println("Name of Student: " + rs.getString(2));

System.out.println("Branch of Student: " + rs.getString(3));

System.out.println("********************************************");

s.close();

con.close();

} catch (Exception err) {

System.out.println(" ERROR: " + err);

}
2.Develop a program to delete all record for a product whose "price" id" is greater than 500 and Id is “1234"

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class Pro3 {

public static void main(String[] args) {

try {

Connection con =
DriverManager.getConnection("jdbc:ucanaccess://C:/Users/admin/OneDrive/Documents/MSBTE.accdb");

Statement s = con.createStatement();

int flag = s.executeUpdate("DELETE FROM List WHERE P_id = '1234' AND price > 500");

System.out.println("Rows Deleted: " + flag);

s.execute("SELECT * FROM List");

ResultSet rs = s.getResultSet();

if (rs != null) {

while (rs.next()) {

System.out.println("Id of the Product: " + rs.getString(1));

System.out.println("Price of the Product: " + rs.getString(2));

System.out.println(" ");

s.close();

con.close();

} catch (Exception err) {

System.out.println("ERROR: " + err);

You might also like