Show Menu
Cheatography

Java Programming Cheat Sheet by

Java Programming Cheat Sheet

Inheri­tance

class Animal {
void eat() {
System.ou­t.p­rin­tln­("ea­t");
}
}

class Dog extends Animal {
void bark() {
System.ou­t.p­rin­tln­("ba­rk");
}
}

Interface

interface Animal {
void sound();
}

class Dog implements Animal {
public void sound() {
System.ou­t.p­rin­tln­("ba­rk");
}
}

Abstract

abstract class Animal {
abstract void sound();

void eat() {
System.ou­t.p­rin­tln­("ea­t");
}
}

class Dog extends Animal {
void sound() {
System.ou­t.p­rin­tln­("ba­rk");
}
}

Exception

public class UsedCa­rEx­ception extends Exception {
private static final long serial­Ver­sionUID = 1L;

public UsedCa­rEx­cep­tio­n(S­tring message) {
super(­mes­sage);
}
}
 

Working with Files

public class UsedCarApp {

public static void writeT­oFi­le(­Arr­ayL­ist­<Us­edC­ar> cars, String fileName) {
try {
PrintW­riter writer = new PrintW­rit­er(new FileWr­ite­r(f­ile­Name));

for (UsedCar car : cars) {
writer.pr­int­ln(­car.to­Str­ing());
}

writer.cl­ose();
System.ou­t.p­rin­tln­("\n­Suc­ces­sfully saved valid cars to file.");
} catch (IOExc­eption e) {
System.ou­t.p­rin­tln­("Error writing to file: " + e.getM­ess­age());
}
}

public static void readFr­omF­ile­(String fileName) {
try {
Scanner fileInput = new Scanne­r(new File(f­ile­Name));

System.ou­t.p­rin­tln­("\nVIN and Price from file:");
while (fileI­npu­t.h­asN­ext­Line()) {
String line = fileIn­put.ne­xtL­ine();
String[] parts = line.s­pli­t(",­");

if (parts.length == 4) {
String vin = parts[0];
String price = parts[3];

System.ou­t.p­rin­tln­("VIN: " + vin + " | Price: " + price);
}
}

fileIn­put.cl­ose();
} catch (FileN­otF­oun­dEx­ception e) {
System.ou­t.p­rin­tln­("Error reading file: " + e.getM­ess­age());
}
}

public static void main(S­tring[] args) {
Scanner input = new Scanne­r(S­yst­em.in);
ArrayL­ist­<Us­edC­ar> validCars = new ArrayL­ist­<>();

final int NUMBER­_OF­_CARS = 3;

for (int i = 0; i < NUMBER­_OF­_CARS; i++) {
try {
System.ou­t.p­rin­tln­("\n­Enter inform­ation for car #" + (i + 1));

System.ou­t.p­rin­t("Enter VIN (4 digits): ");
String vin = input.n­ex­tLi­ne();

System.ou­t.p­rin­t("Enter make: ");
String make = input.n­ex­tLi­ne();

System.ou­t.p­rin­t("Enter year: ");
int year = Intege­r.p­ars­eIn­t(i­npu­t.n­ext­Lin­e());

System.ou­t.p­rin­t("Enter price: ");
double price = Double.pa­rse­Dou­ble­(in­put.ne­xtL­ine());

UsedCar car = new UsedCa­r(vin, make, year, price);
validC­ars.ad­d(car);

System.ou­t.p­rin­tln­("Car created succes­sfu­lly.");

} catch (UsedC­arE­xce­ption e) {
System.ou­t.p­rin­tln­("Us­edC­arE­xce­ption: " + e.getM­ess­age());
} catch (Exception e) {
System.ou­t.p­rin­tln­("Ex­cep­tion: Invalid input. " + e.getM­ess­age());
}
}

String fileName = "­Use­dCa­rs.t­xt­";

writeT­oFi­le(­val­idCars, fileName);
readFr­omF­ile­(fi­leN­ame);

input.c­lo­se();
}
}

JDBC

module­-in­fo.java >

module FinalT­erm­Pra­ctice {
requires java.sql;
}
 

JDBC

import java.s­ql.*;

public class Employ­eeDBApp {

private static final String DB_FILE = "­Emp­loy­eeD­B.a­ccd­b";
private static final String DB_URL = "­jdb­c:u­can­acc­ess­://­" + DB_FILE;

public static Connection getCon­nec­tion() throws SQLExc­eption, ClassN­otF­oun­dEx­ception {
Class.f­or­Nam­e("n­et.u­ca­nac­ces­s.j­dbc.Uc­ana­cce­ssD­riv­er");
return Driver­Man­age­r.g­etC­onn­ect­ion­(DB­_URL);
}

public static void insert­Emp­loy­ee(­Scanner input) {
Connection conn = null;
Statement statement = null;

try {
conn = getCon­nec­tion();
statement = conn.c­rea­teS­tat­eme­nt();

System.ou­t.p­rin­t("Enter employee name: ");
String name = input.n­ex­tLi­ne();

System.ou­t.p­rin­t("Enter employee salary: ");
double salary = Double.pa­rse­Dou­ble­(in­put.ne­xtL­ine());

String insertQ = "­INSERT INTO Employee (Name, Salary) VALUES ('"
+ name + "', " + salary + "­)";
statem­ent.ex­ecu­teU­pda­te(­ins­ertQ);

System.ou­t.p­rin­tln­("Em­ployee inserted succes­sfu­lly.");

} catch (Class­Not­Fou­ndE­xce­ption ex) {
System.ou­t.p­rin­tln­("Driver error: " + ex.get­Mes­sag­e());
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­("Da­tabase error: " + ex.get­Mes­sag­e());
} catch (Exception ex) {
System.ou­t.p­rin­tln­("Input error: " + ex.get­Mes­sag­e());
} finally {
try {
if (statement != null)
statem­ent.cl­ose();
if (conn != null)
conn.c­lose();
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­(ex.ge­tMe­ssa­ge());
}
}
}

public static void update­Emp­loy­eeS­ala­ry(­Scanner input) {
Connection conn = null;
Statement statement = null;

try {
conn = getCon­nec­tion();
statement = conn.c­rea­teS­tat­eme­nt();

System.ou­t.p­rin­t("Enter employee name to update: ");
String name = input.n­ex­tLi­ne();

System.ou­t.p­rin­t("Enter new salary: ");
double salary = Double.pa­rse­Dou­ble­(in­put.ne­xtL­ine());

String updateQ = "­UPDATE Employee SET Salary = " + salary
+ " WHERE Name = '" + name + "­'";
int rows = statem­ent.ex­ecu­teU­pda­te(­upd­ateQ);

if (rows > 0) {
System.ou­t.p­rin­tln­("Salary updated succes­sfu­lly.");
} else {
System.ou­t.p­rin­tln­("Em­ployee not found."­);
}

} catch (Class­Not­Fou­ndE­xce­ption ex) {
System.ou­t.p­rin­tln­("Driver error: " + ex.get­Mes­sag­e());
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­("Da­tabase error: " + ex.get­Mes­sag­e());
} catch (Exception ex) {
System.ou­t.p­rin­tln­("Input error: " + ex.get­Mes­sag­e());
} finally {
try {
if (statement != null)
statem­ent.cl­ose();
if (conn != null)
conn.c­lose();
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­(ex.ge­tMe­ssa­ge());
}
}
}

public static void delete­Emp­loy­ee(­Scanner input) {
Connection conn = null;
Statement statement = null;

try {
conn = getCon­nec­tion();
statement = conn.c­rea­teS­tat­eme­nt();

System.ou­t.p­rin­t("Enter employee name to delete: ");
String name = input.n­ex­tLi­ne();

String deleteQ = "­DELETE FROM Employee WHERE Name = '" + name + "­'";
int rows = statem­ent.ex­ecu­teU­pda­te(­del­eteQ);

if (rows > 0) {
System.ou­t.p­rin­tln­("Em­ployee deleted succes­sfu­lly.");
} else {
System.ou­t.p­rin­tln­("Em­ployee not found."­);
}

} catch (Class­Not­Fou­ndE­xce­ption ex) {
System.ou­t.p­rin­tln­("Driver error: " + ex.get­Mes­sag­e());
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­("Da­tabase error: " + ex.get­Mes­sag­e());
} catch (Exception ex) {
System.ou­t.p­rin­tln­("Input error: " + ex.get­Mes­sag­e());
} finally {
try {
if (statement != null)
statem­ent.cl­ose();
if (conn != null)
conn.c­lose();
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­(ex.ge­tMe­ssa­ge());
}
}
}

public static void displa­yAl­lEm­plo­yees() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;

ArrayL­ist­<Em­plo­yee> employees = new ArrayL­ist­<Em­plo­yee­>();

try {
conn = getCon­nec­tion();
statement = conn.c­rea­teS­tat­eme­nt();

String query = "­SELECT * FROM Employ­ee";
rs = statem­ent.ex­ecu­teQ­uer­y(q­uery);

while (rs.ne­xt()) {
int id = rs.get­Int­("ID­");
String name = rs.get­Str­ing­("Na­me");
double salary = rs.get­Dou­ble­("Sa­lar­y");

Employee emp = new Employ­ee(id, name, salary);
employ­ees.ad­d(emp);
}

if (emplo­yee­s.s­ize() == 0) {
System.ou­t.p­rin­tln­("No employee records found."­);
} else {
System.ou­t.p­rin­tln­("\n--- All Employees ---");
for (Employee emp : employees) {
System.ou­t.p­rin­tln­("ID: " + emp.ge­tId()
+ ", Name: " + emp.ge­tName()
+ ", Salary: " + emp.ge­tSa­lar­y());
}
}

} catch (Class­Not­Fou­ndE­xce­ption ex) {
System.ou­t.p­rin­tln­("Driver error: " + ex.get­Mes­sag­e());
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­("Da­tabase error: " + ex.get­Mes­sag­e());
} finally {
try {
if (rs != null)
rs.clo­se();
if (statement != null)
statem­ent.cl­ose();
if (conn != null)
conn.c­lose();
} catch (SQLEx­ception ex) {
System.ou­t.p­rin­tln­(ex.ge­tMe­ssa­ge());
}
}
}

public static void showMenu() {
System.ou­t.p­rin­tln­("\n­===== Employee Database Menu =====");
System.ou­t.p­rin­tln­("1. Insert Employ­ee");
System.ou­t.p­rin­tln­("2. Update Employee Salary­");
System.ou­t.p­rin­tln­("3. Delete Employ­ee");
System.ou­t.p­rin­tln­("4. Display All Employ­ees­");
System.ou­t.p­rin­tln­("5. Exit");
System.ou­t.p­rin­t("Enter your choice: ");
}

public static void main(S­tring[] args) {
Scanner input = new Scanne­r(S­yst­em.in);
int choice = 0;

do {
showMe­nu();

try {
choice = Intege­r.p­ars­eIn­t(i­npu­t.n­ext­Lin­e());

switch (choice) {
case 1:
insert­Emp­loy­ee(­input);
break;
case 2:
update­Emp­loy­eeS­ala­ry(­input);
break;
case 3:
delete­Emp­loy­ee(­input);
break;
case 4:
displa­yAl­lEm­plo­yees();
break;
case 5:
System.ou­t.p­rin­tln­("Pr­ogram termin­ate­d.");
break;
default:
System.ou­t.p­rin­tln­("In­valid choice. Please enter a number from 1 to 5.");
}

} catch (Exception ex) {
System.ou­t.p­rin­tln­("In­valid input. Please enter a valid number.");
}

} while (choice != 5);

input.c­lo­se();
}
}
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by sally sung

          Web Development Cheat Sheet
          Web Development Midterm Cheat Sheet
          Web Development Quiz2 Cheat Sheet