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

Class Note 06 - Object Oriented Programming and JDBC

The document discusses object reference casting in Java. It provides examples of casting between classes and interfaces, and discusses when casting is legal or will result in errors. It also covers enumerations in Java, demonstrating how to define and use enum types.

Uploaded by

MUSIC & TECH
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)
18 views

Class Note 06 - Object Oriented Programming and JDBC

The document discusses object reference casting in Java. It provides examples of casting between classes and interfaces, and discusses when casting is legal or will result in errors. It also covers enumerations in Java, demonstrating how to define and use enum types.

Uploaded by

MUSIC & TECH
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/ 33

CMJD – Diploma in Comprehensive Master Java Developer

Object Reference Casting


01.

class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();
a1.b=200; //Illegal
a1.mB(); //Illegal
}
}

02.
Exercise

class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}

Page 1 of 33
CMJD – Diploma in Comprehensive Master Java Developer

class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();

//---------------------------
//call mB()of B
}
}

03 From 02.

class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();

//---------------------------
//call mB()of B
B b1=a1; //Illegal
b1.b=200;
b1.mB();
}
}

Page 2 of 33
CMJD – Diploma in Comprehensive Master Java Developer

04.

class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();

//---------------------------
//call mB()of B
//B b1=a1; //Illegal
B b1=(B)a1; //Object Reference Casting
b1.b=200;
b1.mB();
}
}

05. Case I

class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");

Page 3 of 33
CMJD – Diploma in Comprehensive Master Java Developer

}
}
class Demo{
public static void main(String args[]){
A a1=new A();
a1.a=100;
a1.mA();

B b1=(B)a1; //Legal, Runtime Error


b1.b=200;
b1.mB();
}
}

06. Case II

class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new A();
a1.a=100;
a1.mA();

if( a1 instanceof B){


B b1=(B)a1;
b1.b=200;
b1.mB();
}
System.out.println("a1 instanceof B :"+(a1 instanceof B));
}
}

Page 4 of 33
CMJD – Diploma in Comprehensive Master Java Developer

07. Exercise

class A{}
class B extends A{}
class C extends B{}
class Demo{
public static void main(String args[]){
A a1=new A();
A a2=new B();
A a3=new C();
B b1=new B();
B b2=new C();
C c1=new C();

System.out.println("a1 instanceof A :"+(a1 instanceof A));


System.out.println("a1 instanceof B :"+(a1 instanceof B));
System.out.println("a1 instanceof C :"+(a1 instanceof C));

System.out.println("a2 instanceof A :"+(a2 instanceof A));


System.out.println("a2 instanceof B :"+(a2 instanceof B));
System.out.println("a2 instanceof C :"+(a2 instanceof C));

System.out.println("a3 instanceof A :"+(a3 instanceof A));


System.out.println("a3 instanceof B :"+(a3 instanceof B));
System.out.println("a3 instanceof C :"+(a3 instanceof C));

System.out.println("b1 instanceof A :"+(b1 instanceof A));


System.out.println("b1 instanceof B :"+(b1 instanceof B));
System.out.println("b1 instanceof C :"+(b1 instanceof C));

System.out.println("c1 instanceof A :"+(c1 instanceof A));


System.out.println("c1 instanceof B :"+(c1 instanceof B));
System.out.println("c1 instanceof C :"+(c1 instanceof C));
}
}

Page 5 of 33
CMJD – Diploma in Comprehensive Master Java Developer

08. Case III

class A{}
class B{}
class Demo{
public static void main(String args[]){
A a1=new A();
B b1=new B();
a1=(A)b1; //Illegal
b1=(B)a1; //Illegal
}
}

09. Exercise

class A{}
class B{}
class Demo{
public static void main(String args[]){
A a1=new A();
B b1=new B();
System.out.println(a1 instanceof A);
System.out.println(a1 instanceof B); //Compile Error
System.out.println(b1 instanceof A); //Compile Error
System.out.println(b1 instanceof B);
}
}

10. Case IV

class A{}
interface P{}
class Demo{
public static void main(String args[]){
A a1=null;
P p1=null;

a1=(A)p1; //Legal
p1=(P)a1; //Legal
}
}

Page 6 of 33
CMJD – Diploma in Comprehensive Master Java Developer

11. Case V

final class A{}


interface P{}
class Demo{
public static void main(String args[]){
A a1=null;
P p1=null;

a1=(A)p1; //Illegal
p1=(P)a1; //Illegal
}
}

Enumerations
12.

/*class Color{
public final static String RED="RED";
public final static String BLACK="BLACK";
public final static String BLUE="BLUE";
public final static String GREEN="GREEN";
}*/
enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color c1; //Create an enum reference type "Color"
c1=Color.BLACK;
System.out.println(c1); //c1.toString()-->BLACK
}
}

Page 7 of 33
CMJD – Diploma in Comprehensive Master Java Developer

13. Case I

enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color c1=new Color(); //Error
}
}

14. Case II

enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color[] colors=Color.values();
for(Color c1:colors){
System.out.println(c1);
}
}
}

15. Case III

enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color color=Color.BLACK;

switch(color){ //Legal
case RED : //
case BLUE:
case BLACK:
}
}
}

Page 8 of 33
CMJD – Diploma in Comprehensive Master Java Developer

16. Case IV

enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color color=Color.valueOf("RED");
System.out.println(color);

}
}

17. Case V

enum Color{
RED(),BLACK(),BLUE(),GREEN;

Color(){
System.out.println("Color()");
}
}
class Demo{
public static void main(String args[]){
Color c1=Color.BLACK;
}
}

18. Case VI

enum Color{
RED(1001),BLACK(1002),BLUE(1003),GREEN(1004);
private int code;
Color(int code){
this.code=code;
}
public void setCode(int code){
this.code=code;
}
public int getCode(){
return code;
}

Page 9 of 33
CMJD – Diploma in Comprehensive Master Java Developer

}
class Demo{
public static void main(String args[]){
Color c1=Color.BLACK;
System.out.println(c1.getCode());
c1.setCode(2002);
System.out.println(c1.getCode());
}
}

Java Collection Framework

Class “ArrayList”
Generic type safe class
As a list
Indexing and fast Random accessing Insertion order

19.

import java.util.ArrayList;
class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

public String toString() {


return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {
ArrayList<String> strList = new ArrayList<>();
strList.add(new String("A"));
strList.add(new String("B"));
strList.add(new String("C"));

Page 10 of 33
CMJD – Diploma in Comprehensive Master Java Developer

strList.add(new String("D"));
strList.add(new String("E"));
System.out.println(strList); // strList.toString"(;
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Customer 1"));
custList.add(new Customer(1002, "Customer 2"));
custList.add(new Customer(1003, "Customer 3"));
custList.add(new Customer(1004, "Customer 4"));
custList.add(new Customer(1005, "Customer 5"));
System.out.println(custList);
}
}

20.

import java.util.ArrayList;
class Customer {
private int code;
private String name;
Customer(int code, String name) {
this.code = code;
this.name = name;
}
public String toString() {
return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
Customer c1 = custList.get(2); // Indexing, Random access System.out.println("Customer
of index 2 : "+c1);
custList.add(2, new Customer(1111, "Sirisena"));
System.out.println(custList);
for (int i = 0; i < custList.size(); i++) {

Page 11 of 33
CMJD – Diploma in Comprehensive Master Java Developer

Customer cust = custList.get(i);


System.out.println(cust);
}
}
}

21. for-each with collection classes

import java.util.ArrayList;
class Customer {
private int code;
private String name;
Customer(int code, String name) {
this.code = code;
this.name = name;
}
public String toString() {
return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
for (Customer cust : custList) {
System.out.println(cust);
}
}
}

Page 12 of 33
CMJD – Diploma in Comprehensive Master Java Developer

22.

import java.util.ArrayList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

public String toString() {


return code + "-" + name;
}
}

class Demo {

public static void main(String args[]) {


ArrayList<String> strList = new ArrayList<>();
strList.add(new String("A"));
strList.add(new String("B"));
strList.add(new String("C"));
strList.add(new String("D"));
strList.add(new String("E"));
System.out.println(strList);

boolean b = strList.contains(new String("C"));


System.out.println("String C contains " + b); // true
b = strList.contains(new String("X"));
System.out.println("String X contains " + b); // false
strList.remove(2);
System.out.println(strList);
strList.remove(new String("D"));
System.out.println(strList);

}
}

Page 13 of 33
CMJD – Diploma in Comprehensive Master Java Developer

23. Exercise

import java.util.ArrayList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

public String toString() {

return code + "-" + name;


}
}

class Demo {
public static void main(String args[]) {
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
boolean b = custList.contains(new Customer(1003, "Siripala"));
System.out.println("1003-Siripala : " + b); // false
custList.remove(new Customer(1003, "Siripala"));
System.out.println(custList);

}
}

Page 14 of 33
CMJD – Diploma in Comprehensive Master Java Developer

24. Method public boolean equals(Object)

import java.util.ArrayList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

/*
* public boolean equals(Object obj){
* return this==obj;
*}
*/
public String toString() {
return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {
Customer c1 = new Customer(1001, "Danapala");
Customer c2 = new Customer(1001, "Danapala");
Customer c3 = new Customer(1002, "Ganapala");
Customer c4 = c1;
System.out.println("c1 equals c2 : " + c1.equals(c2));// false
System.out.println("c1 equals c3 : " + c1.equals(c3));// false
System.out.println("c1 equals c4 : " + c1.equals(c4));// true

}
}

Page 15 of 33
CMJD – Diploma in Comprehensive Master Java Developer

25 From 24. Overriding “public boolean equals(Object)”

import java.util.ArrayList;

class Customer {

private int code;


private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

/*
* public boolean euqlas(Object obj){
* return this==obj;
*}
*/
public boolean equals(Object obj) {
// return this.code==obj.code;
// Customer c1=obj;
Customer c1 = (Customer) obj; // object reference casting
return this.code==c1.code;
}

public String toString() {


return code + "-" + name;
}
}
class Demo {
public static void main(String args[]) {

Customer c1 = new Customer(1001, "Danapala");


Customer c2 = new Customer(1001, "Danapala");
Customer c3 = new Customer(1002, "Ganapala");
Customer c4 = c1;
System.out.println("c1 equals c2 : " + c1.equals(c2));// true
System.out.println("c1 equals c3 : " + c1.equals(c3));// false
System.out.println("c1 equals c4 : " + c1.equals(c4));// true
}
}

Page 16 of 33
CMJD – Diploma in Comprehensive Master Java Developer

26 From 23.

import java.util.ArrayList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
// Illegal, obj.code

public boolean equals(Object obj) {


Customer c1 = (Customer) obj;
return this.code == c1.code;
}

public String toString() {


return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {

ArrayList<Customer> custList = new ArrayList<>();


custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
boolean b = custList.contains(new Customer(1003, "Siripala"));
System.out.println("1003-Siripala : " + b); // false
custList.remove(new Customer(1003, "Siripala"));
System.out.println(custList);
}

Page 17 of 33
CMJD – Diploma in Comprehensive Master Java Developer

27. Class “LinkedList” Speed, addition and deletion

import java.util.LinkedList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

public boolean equals(Object obj) {


Customer c1 = (Customer) obj;
return this.code == c1.code;
}

public String toString() {


return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {
LinkedList<Customer> custList = new LinkedList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);

}
}

Page 18 of 33
CMJD – Diploma in Comprehensive Master Java Developer

28. Linked List as a “Stack”

import java.util.LinkedList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

public boolean equals(Object obj) {


Customer c1 = (Customer) obj;
return this.code == c1.code;
}

public String toString() {


return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {
LinkedList<Customer> custList = new LinkedList<>();
custList.push(new Customer(1001, "Danapala"));
custList.push(new Customer(1002, "Gunapala"));
custList.push(new Customer(1003, "Siripala"));
custList.push(new Customer(1004, "Somapala"));
custList.push(new Customer(1005, "Amarapala"));
System.out.println(custList);
Customer c1 = custList.pop();
System.out.println("Top customer : " + c1);
System.out.println(custList);
c1 = custList.peek();
System.out.println("Top customer : " + c1);
}
}

Page 19 of 33
CMJD – Diploma in Comprehensive Master Java Developer

29. LinkedList as “Queue”

import java.util.LinkedList;

class Customer {
private int code;
private String name;

Customer(int code, String name) {


this.code = code;
this.name = name;
}

public boolean equals(Object obj) {


Customer c1 = (Customer) obj;
return this.code == c1.code;
}

public String toString() {


return code + "-" + name;
}
}

class Demo {
public static void main(String args[]) {

LinkedList<Customer> custList = new LinkedList<>();


custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
Customer c1 = custList.remove(); // remove, first element of the Queue
System.out.println("Top customer : " + c1);
System.out.println(custList);
c1 = custList.peek();
System.out.println("Top customer : " + c1);
System.out.println(custList);
}
}

Page 20 of 33
CMJD – Diploma in Comprehensive Master Java Developer

Generics in Java

30.

class List{
private int[] items = new int[10];
private int count;

public void add(int item){


items[count++] = item;
}

public int get(int index){


return items[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3);
}
}

31.

class List{

private int[] items = new int[10];


private int count;

public void add(int item){


items[count++] = item;
}

public int get(int index){


return items[index];
}
}

Page 21 of 33
CMJD – Diploma in Comprehensive Master Java Developer

Customer Array

class Customer{}
class CustomerList{
private Customer[] customers = new Customer[10];
private int count;
public void add(Customer customer){
customers[count++] = customer;
}
public Customer get(int index){
return customers[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3);
CustomerList customerList = new CustomerList();
customerList.add(new Customer());
}
}

32.

class Customer{}
class List{
private Object[] items = new Object[10];
private int count;
public void add(Object item){
items[count++] = item;
}
public Object get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3); //==> Integer.valueOf(3); Wrapper Class
list.add("3");
list.add(new Customer());}}

Page 22 of 33
CMJD – Diploma in Comprehensive Master Java Developer

33.

class Customer{}
class List{
private Object[] items = new Object[10];
private int count;
public void add(Object item){
items[count++] = item;
}
public Object get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3); //==> Integer.valueOf(3); Wrapper Class
list.add("3");
list.add(new Customer());

int number = (int) list.get(0); // Explicitly cast


Customer customer = (Customer) list.get(1); // ClassCastException That can identify only Runtime
}
}

34.

class Customer{}

class List<T>{ // Type Parameter for Class


private T[] items = (T[])new Object[10];
private int count;

public void add(T item){


items[count++] = item;
}

public T get(int index){


return items[index];
}
}

Page 23 of 33
CMJD – Diploma in Comprehensive Master Java Developer

class Example {
public static void main(String[] args) {
List<Integer> list = new List<>();
list.add(1);
list.get(0);
//list.add("1"); //Illegal

List<Customer> customerList = new List<>();


customerList.add(new Customer());
}
}

35.

class Customer{}
class List<T>{ // Type Parameter for Class
private T[] items = (T[])new Object[10];
private int count;
public void add(T item){
items[count++] = item;
}
public T get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
///// Wrapper Classes //////////
//byte -> Byte
//short -> Short
//int -> Integer
//long -> Long
//float -> Float
//double -> Double
//char -> Character
//boolean -> Boolean
//List<int> list = new List<>(); // Compile Error.
List<Integer> list = new List<>();
list.add(1); // Boxing
int num = list.get(0); // Unboxing
}
}

Page 24 of 33
CMJD – Diploma in Comprehensive Master Java Developer

36.

class Customer{}

class List<T extends Number>{ // Type Parameter for Class


private T[] items = (T[])new Object[10];
private int count;

public void add(T item){


items[count++] = item;
}
public T get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
List<Number> list = new List<>();
List<Integer> listInt = new List<>();
List<Double> listDouble = new List<>();
//List<Customer> customers = new List<>(); // Illegal. Type can be only number or sub type of number
}
}

JDBC

37. Step 1 (Load MySQL Driver)

public class Main{


public static void main(String[] args) {
Class.forName("com.mysql.cj.jdbc.Driver");
}
}

38. Step 2 (Throw Exceptions)

public class Main{


public static void main(String[] args) throws ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
}
}

Page 25 of 33
CMJD – Diploma in Comprehensive Master Java Developer

39. Step 3 (Create Connection)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main{


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root", "Xhq8nc3mcj");
}
}

40. Step 4 (Execute a Query)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main{


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root", "Xhq8nc3mcj");

Statement statement = connection.createStatement();


ResultSet resultSet = statement.executeQuery("SELECT * FROM Customer");
while(resultSet.next()){
System.out.println(resultSet.getString(1) + " " + resultSet.getString(3) + " " +
resultSet.getDouble(5));
}
}
}

Page 26 of 33
CMJD – Diploma in Comprehensive Master Java Developer

41. Step 4 (Execute an Update)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Main{


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root", "Xhq8nc3mcj");

Statement statement = connection.createStatement();


int result = statement.executeUpdate("INSERT INTO Customer VALUES ('C033', 'Mr', 'Kapila', '1989-
10-15', 100000.00, 'Galle', 'Galle', 'Southern', 10002)");
System.out.println(result >0? "Success" : "Fail");
}
}

42 From 41

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root",
"Xhq8nc3mcj");

System.out.print("Input customer id : ");


String custId = scanner.nextLine();

System.out.print("Input customer title : ");


String title = scanner.nextLine();

Page 27 of 33
CMJD – Diploma in Comprehensive Master Java Developer

System.out.print("Input customer name : ");


String name = scanner.nextLine();

System.out.print("Input customer DOB : ");


String dob = scanner.nextLine();

System.out.print("Input customer address : ");


String address = scanner.nextLine();

System.out.print("Input customer city : ");


String city = scanner.nextLine();

System.out.print("Input customer province : ");


String province = scanner.nextLine();

System.out.print("Input customer zip : ");


String zip = scanner.nextLine();

System.out.print("Input customer salary : ");


int salary = scanner.nextInt();

Statement statement = connection.createStatement();


int result = statement.executeUpdate(
"INSERT INTO Customer VALUES ('"+custId+"', '"+title+"', '"+name+"', '"+dob+"', "+salary+",
'"+address+"', '"+city+"', '"+province"', '"+zip+"')");
System.out.println(result > 0 ? "Success" : "Fail");

}
}

43. Step 5 (PreparedStatement - Insert)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);

Page 28 of 33
CMJD – Diploma in Comprehensive Master Java Developer

Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root",
"Xhq8nc3mcj");

System.out.print("Input customer id : ");


String custId = scanner.nextLine();

System.out.print("Input customer title : ");


String title = scanner.nextLine();

System.out.print("Input customer name : ");


String name = scanner.nextLine();

System.out.print("Input customer DOB : ");


String dob = scanner.nextLine();

System.out.print("Input customer address : ");


String address = scanner.nextLine();

System.out.print("Input customer city : ");


String city = scanner.nextLine();

System.out.print("Input customer province : ");


String province = scanner.nextLine();

System.out.print("Input customer zip : ");


String zip = scanner.nextLine();

System.out.print("Input customer salary : ");


int salary = scanner.nextInt();

PreparedStatement statement = connection.prepareStatement("INSERT INTO Customer VALUES


(?,?,?,?,?,?,?,?,?)");
statement.setString(1, custId);
statement.setString(2, title);
statement.setString(3, name);
statement.setString(4, dob);
statement.setInt(5, salary);
statement.setString(6, address);
statement.setString(7, city);
statement.setString(8, province);

Page 29 of 33
CMJD – Diploma in Comprehensive Master Java Developer

statement.setString(9, zip);

int result = statement.executeUpdate();


System.out.println(result > 0 ? "Success" : "Fail");
}
}

43. Step 6 (PreparedStatement - Select)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root",
"Xhq8nc3mcj");

System.out.print("Input customer province : ");


String province = scanner.nextLine();

PreparedStatement statement = connection.prepareStatement("SELECT * FROM Customer WHERE


province = ?");
statement.setString(1, province);

ResultSet resultSet = statement.executeQuery();


while(resultSet.next()){
System.out.println(resultSet.getString(1) + " " + resultSet.getString(3) + " " +
resultSet.getDouble(5));
}

}
}

Page 30 of 33
CMJD – Diploma in Comprehensive Master Java Developer

Singleton Design Pattern


44. Step 1

class A{
public A(){}
public void myMethod(){
System.out.println("myMethod in : " + this);
}
}
public class Main {
public static void main(String[] args){
A a1 = new A();
a1.myMethod();
A a2 = new A();
a2.myMethod();

}
}

45. Step 2

class A{
private static A a;
public A(){}
public static A getInstance(){
if(a == null){
a = new A();
}
return a;
}
public void myMethod(){
System.out.println("myMethod in : " + this);
}
}
public class Main {
public static void main(String[] args){
A a1 = new A();
a1.myMethod();
A a2 = new A();
a2.myMethod(); }}

Page 31 of 33
CMJD – Diploma in Comprehensive Master Java Developer

46. Step 3

class A{
private static A a;
private A(){}

public static A getInstance(){


if(a == null){
a = new A();
}
return a;
}
public void myMethod(){
System.out.println("myMethod in : " + this);
}
}

public class Main {


public static void main(String[] args){
A a1 = A.getInstance();
a1.myMethod();

A a2 = A.getInstance();
a2.myMethod();
}
}

47 from 43. (DB Connection using Singleton)

DBConnection.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection{


private Connection connection;
private static DBConnection dbConnection;

private DBConnection() throws ClassNotFoundException, SQLException{


Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root",

Page 32 of 33
CMJD – Diploma in Comprehensive Master Java Developer

"Xhq8nc3mcj");
}

public static DBConnection getInstance() throws ClassNotFoundException, SQLException{


if(dbConnection == null){
dbConnection = new DBConnection();
}
return dbConnection;
}
public Connection getConnection(){
return connection;
}
}

Main.java

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);

Connection connection = DBConnection.getInstance().getConnection();

System.out.print("Input customer province : ");


String province = scanner.nextLine();

PreparedStatement statement = connection.prepareStatement("SELECT * FROM Customer WHERE


province = ?");
statement.setString(1, province);

ResultSet resultSet = statement.executeQuery();


while(resultSet.next()){
System.out.println(resultSet.getString(1) + " " + resultSet.getString(3) + " " +
resultSet.getDouble(5));
}
}

Page 33 of 33

You might also like