Choose Ride:
View Ride Details:
Update Ride Details:
Cancel Ride :
Exit :
Code:
package carRent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Rent {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
createRide_Table();
while (true) {
System.out.println("WELCOME TO Desti Car Rental System..!");
System.out.println("Enter your choice");
System.out.println("1.Choose a Ride");
System.out.println("2.View Ride Details");
System.out.println("3.Update Ride Details");
System.out.println("4.Cancel Ride.");
System.out.println("5.Exit");
choice = sc.nextInt();
switch (choice) {
case 1:
chooseRide();
break;
case 2:
viewRideDetails();
break;
case 3:
updateRideDetails();
break;
case 4:
cancelRide();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Please enter a valid choice..!");
}
}
public static void createRide_Table() {
Connection conn = null;
Statement stm = null;
try {
conn = DriverManager.getConnection("jdbc:derby:rentdb;create=true");
stm = conn.createStatement();
stm.execute(
"CREATE TABLE RIDE (r_id int, trip_type varchar(20), country
varchar(20), city varchar(20),pickup_location varchar(20),drop_location varchar(20))");
stm.close();
conn.close();
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
public static void chooseRide() {
Scanner sc = new Scanner(System.in);
int r_id;
String trip_type;
String country;
String city;
String pickup_location;
String drop_location;
System.out.println("Enter Ride ID : ");
r_id = sc.nextInt();
sc.nextLine();
System.out.println("Enter trip type Round Or Single : ");
trip_type = sc.nextLine();
System.out.println("Enter Country : ");
country = sc.nextLine();
System.out.println("Enter city : ");
city = sc.nextLine();
System.out.println("Enter Pickup_location : ");
pickup_location = sc.nextLine();
System.out.println("Enter drop_location : ");
drop_location = sc.nextLine();
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:derby:rentdb;create=true");
pst = conn.prepareStatement("INSERT INTO RIDE VALUES(?,?,?,?,?,?)");
pst.setInt(1, r_id);
pst.setString(2, trip_type);
pst.setString(3, country);
pst.setString(4, city);
pst.setString(5, pickup_location);
pst.setString(6, drop_location);
int status = pst.executeUpdate();
if (status == 1) {
System.out.println("Record added successfully!");
} else
System.out.println("Something went wrong!");
pst.close();
conn.close();
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
public static void viewRideDetails() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:derby:rentdb;create=true");
st = conn.createStatement();
rs = st.executeQuery("SELECT * FROM RIDE");
if (rs == null) {
System.out.println("No records found");
while (rs.next()) {
int r_id = rs.getInt("r_id");
String trip_type = rs.getString("trip_type");
String country = rs.getString("country");
String city = rs.getString("city");
String pickup_location = rs.getString("pickup_location");
String drop_location = rs.getString("drop_location");
System.out.println(r_id + "\t" + trip_type + "\t" + country + "\t" +
city + "\t" + pickup_location
+ "\t\t" + drop_location + "\t");
st.close();
conn.close();
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
public static void updateRideDetails() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Ride ID to update : ");
int id = sc.nextInt();
sc.nextLine();
System.out.println("Enter Ride convert to. Round or Single ");
String s = sc.nextLine();
Connection conn;
PreparedStatement pst = null;
try {
conn = DriverManager.getConnection("jdbc:derby:rentdb;create=true");
pst = conn.prepareStatement("UPDATE RIDE SET trip_type = ? WHERE r_id =
?");
pst.setString(1, s);
pst.setInt(2, id);
int status = pst.executeUpdate();
if (status == 1)
System.out.println("One record updated successfully..!");
else
System.out.println("Something went wrong..!");
pst.close();
conn.close();
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
public static void cancelRide() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Ride ID to Cancel Ride : ");
int id = sc.nextInt();
Connection conn = null;
PreparedStatement pst = null;
try {
conn = DriverManager.getConnection("jdbc:derby:rentdb;create=true");
pst = conn.prepareStatement("DELETE FROM RIDE WHERE r_id = ?");
pst.setInt(1, id);
int status = pst.executeUpdate();
if (status == 1) {
System.out.println("Ride Cancelled!");
} else
System.out.println("Something went wrong..!");
pst.close();
conn.close();
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());