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

Java 2 Assign

Uploaded by

dryoplayz
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)
5 views

Java 2 Assign

Uploaded by

dryoplayz
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/ 6

TYCOA061 Assignment 2

Program Code :
package jdbc;

import java.sql.*;
import java.util.*;
public class jdbcfetch {
public static void main(String args[]) {
try {
//fetch query
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/carproject", "root",
"");

Statement stmt = con.createStatement();


ResultSet rs = stmt.executeQuery("select*from cars");

//delete query
String sql = "DELETE FROM cars WHERE car_id =20";
PreparedStatement statement = con.prepareStatement(sql);
statement.executeUpdate();
System.out.println("Record deleted.");

//insert query
String sql1 = "insert into cars
values(4,'BMW','PETROL',8,800000,'buggati.jgp','Y')";
PreparedStatement statement1 = con.prepareStatement(sql1);
statement1.executeUpdate();
System.out.println("select*from cars");
//update query
String sql2 = "UPDATE cars SET car_name='Buggati' WHERE car_id =
4";
PreparedStatement statement2 = con.prepareStatement(sql2);
statement2.execute();
System.out.println("Record updated.");

while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2) + "
" + rs.getString(3));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

Output :

Database details :

SQL> select * from cars;

CAR_ID CAR_NAME FUEL_TYPE CAPACITY PRICE CAR_IMG AVAILABLE

--- ------------- ---------- ----------- ---------- ----- ------- -------------------------

1 FERRARI PETRO 5 5000 ferrari Y

2 LAMBORGINI DEISEL 6 7000 lamborgini Y

3 PORSCHE GAS 4 3000 porsche Y

20 SWIFT PETROL 8 8008 swift Y


After firing Delete query :

SQL> select * from cars;

CAR_ID CAR_NAME FUEL_TYPE CAPACITY PRICE CAR_IMG AVAILABLE

--- ------------- ---------- ----------- ---------- ----- ------- -------------------------

1 FERRARI PETRO 5 5000 ferrari Y

2 LAMBORGINI DEISEL 6 7000 lamborgini Y

3 PORSCHE GAS 4 3000 porsche Y


After firing Insert query :

SQL> select * from cars;

CAR_ID CAR_NAME FUEL_TYPE CAPACITY PRICE CAR_IMG AVAILABLE

--- ------------- ---------- ----------- ---------- ----- ------- -------------------------

1 FERRARI PETRO 5 5000 ferrari Y

2 LAMBORGINI DEISEL 6 7000 lamborgini Y

3 PORSCHE GAS 4 3000 porsche Y

4 BMW PETROL 8 800000 buggati Y


After firing Update query :

SQL> select * from cars;

CAR_ID CAR_NAME FUEL_TYPE CAPACITY PRICE CAR_IMG AVAILABLE

--- ------------- ---------- ----------- ---------- ----- ------- -------------------------

1 FERRARI PETRO 5 5000 ferrari Y

2 LAMBORGINI DEISEL 6 7000 lamborgini Y

3 PORSCHE GAS 4 3000 porsche Y

4 Buggati PETROL 8 800000 buggati Y


After firing fetch query :

You might also like