001.
package
lect9;
002.
003.
import
java.io.*;
004.
import
java.sql.Connection;
005.
import
java.util.*;
006.
007.
import
java.sql.Connection;
008.
import
java.sql.DatabaseMetaData;
009.
import
java.sql.ResultSet;
010.
import
java.sql.SQLException;
011.
import
java.sql.Statement;
012.
013.
public
class
CarDB {
014.
private
Vector<Car> Cars;
015.
016.
public
CarDB() {
017.
018.
Cars =
new
<Car>Vector();
019.
}
020.
021.
private
void
updateCar() {
022.
System.out.println(
"CarDB.updateCar()"
);
023.
try
{
024.
System.out.print(
"Plate:"
);
025.
Scanner scanner =
new
Scanner(System.in);
026.
String temp = scanner.nextLine();
027.
int
plate = Integer.parseInt(temp);
028.
System.out.print(
"Model:"
);
029.
String model = scanner.nextLine();
030.
System.out.print(
"Make:"
);
031.
String make = scanner.nextLine();
032.
String updateSql =
"UPDATE CAR SET MODEL='"
+ model +
"', MAKE='"
+ make +
"' WHERE PLATE="
+ plate;
033.
System.out.println(
"updateSql:"
+ updateSql);
034.
035.
ConnectionDB connDB =
new
ConnectionDB();
036.
Connection con = connDB.getConnection();
037.
038.
Statement stmnt =
null
;
039.
if
(con !=
null
) {
040.
stmnt = con.createStatement();
041.
stmnt.executeUpdate(updateSql);
042.
stmnt.close();
043.
con.close();
044.
System.out.println(
"Car updated successfully."
);
045.
}
046.
}
catch
(NumberFormatException ex) {
047.
System.err.println(
"Error! Invalid plate."
);
048.
}
catch
(SQLException ex) {
049.
ex.printStackTrace();
050.
}
051.
}
052.
053.
private
void
deleteCar() {
054.
055.
System.out.println(
"CarDB.deleteCar()"
);
056.
try
{
057.
System.out.print(
"Plate:"
);
058.
Scanner scanner =
new
Scanner(System.in);
059.
String temp = scanner.nextLine();
060.
int
plate = Integer.parseInt(temp);
061.
String deleteSql =
"DELETE FROM CAR WHERE PLATE="
+ plate;
062.
System.out.println(
"deleteSql:"
+ deleteSql);
063.
064.
ConnectionDB connDB =
new
ConnectionDB();
065.
Connection con = connDB.getConnection();
066.
067.
Statement stmnt =
null
;
068.
if
(con !=
null
) {
069.
stmnt = con.createStatement();
070.
stmnt.executeUpdate(deleteSql);
071.
stmnt.close();
072.
con.close();
073.
System.out.println(
"Deleted successfully."
);
074.
}
075.
}
catch
(NumberFormatException ex) {
076.
System.err.println(
"Error! Invalid plate"
);
077.
}
catch
(SQLException ex) {
078.
ex.printStackTrace();
079.
}
080.
}
081.
082.
private
void
searchCar() {
083.
System.out.println(
"CarDB.search()"
);
084.
try
{
085.
System.out.print(
"Plate:"
);
086.
Scanner scanner =
new
Scanner(System.in);
087.
String temp = scanner.nextLine();
088.
int
plate = Integer.parseInt(temp);
089.
String searchSql =
"SELECT * FROM CAR WHERE PLATE="
+ plate;
090.
System.out.println(
"selectSql:"
+ searchSql);
091.
092.
ConnectionDB connDB =
new
ConnectionDB();
093.
Connection con = connDB.getConnection();
094.
095.
Statement stmnt =
null
;
096.
if
(con !=
null
) {
097.
stmnt = con.createStatement();
098.
ResultSet rs = stmnt.executeQuery(searchSql);
099.
if
(rs.next()) {
100.
plate = rs.getInt(
"PLATE"
);
101.
String model = rs.getString(
"MODEL"
);
102.
String make = rs.getString(
"MAKE"
);
103.
Car car =
new
Car(plate, model, make);
104.
System.out.println(
"Car:"
+ car.toString());
105.
}
106.
stmnt.close();
107.
con.close();
108.
System.out.println(
"Searched successfully."
);
109.
}
110.
}
catch
(NumberFormatException ex) {
111.
System.err.println(
"Error! Invalid plate"
);
112.
}
catch
(SQLException ex) {
113.
ex.printStackTrace();
114.
}
115.
}
116.
117.
private
void
addCars() {
118.
System.out.println(
"CarDB.addCars()"
);
119.
try
{
120.
System.out.print(
"Plate:"
);
121.
Scanner scanner =
new
Scanner(System.in);
122.
String temp = scanner.nextLine();
123.
int
plate = Integer.parseInt(temp);
124.
System.out.print(
"Model:"
);
125.
String model = scanner.nextLine();
126.
System.out.print(
"Make:"
);
127.
String make = scanner.nextLine();
128.
Car car =
new
Car(plate, model, make);
129.
String insertSql =
"INSERT INTO CAR VALUES"
+
"("
+ car.plate +
", '"
+ car.model +
"', '"
+ car.make
130.
+
"')"
;
131.
System.out.println(
"selectSql:"
+ insertSql);
132.
133.
ConnectionDB connDB =
new
ConnectionDB();
134.
Connection con = connDB.getConnection();
135.
136.
Statement stmnt =
null
;
137.
if
(con !=
null
) {
138.
stmnt = con.createStatement();
139.
stmnt.execute(insertSql);
140.
stmnt.close();
141.
con.close();
142.
System.out.println(
"Car added successfully."
);
143.
}
144.
}
catch
(NumberFormatException ex) {
145.
System.err.println(
"Error! Invalid plate."
);
146.
}
catch
(SQLException ex) {
147.
ex.printStackTrace();
148.
}
149.
}
150.
151.
private
void
viewCars() {
152.
System.out.println(
"CarDB.viewCars()"
);
153.
String selectSql =
"SELECT * FROM CAR"
;
154.
System.out.println(
"selectSql:"
+ selectSql);
155.
156.
Vector<Car> cars =
new
Vector();
157.
158.
ConnectionDB connDB =
new
ConnectionDB();
159.
Connection con = connDB.getConnection();
160.
161.
Statement stmnt =
null
;
162.
if
(con !=
null
) {
163.
try
{
164.
stmnt = con.createStatement();
165.
ResultSet rs = stmnt.executeQuery(selectSql);
166.
while
(rs.next()) {
167.
int
plate = rs.getInt(
"PLATE"
);
168.
String model = rs.getString(
"MODEL"
);
169.
String make = rs.getString(
"MAKE"
);
170.
Car car =
new
Car(plate, model, make);
171.
cars.add(car);
172.
}
173.
stmnt.close();
174.
con.close();
175.
System.out.println(
"Retrieved successfully."
);
176.
}
catch
(Exception ex) {
177.
ex.printStackTrace();
178.
}
179.
}
180.
Iterator it = cars.iterator();
181.
while
(it.hasNext()) {
182.
Car Car = (Car) it.next();
183.
System.out.println(Car.toString());
184.
}
185.
}
186.
187.
188.
private
void
mainMenu() {
189.
boolean
cont =
true
;
190.
while
(cont) {
191.
System.out.println(
"=== Main Menu ==="
);
192.
System.out.println(
"1 Add Car."
);
193.
System.out.println(
"2 Update Car."
);
194.
System.out.println(
"3 Search Car."
);
195.
System.out.println(
"4 Delete Car."
);
196.
System.out.println(
"9 View Cars."
);
197.
System.out.println(
"0 Exit"
);
198.
System.out.print(
"Your choice:"
);
199.
Scanner scanner =
new
Scanner(System.in);
200.
String choice = scanner.nextLine();
201.
if
(choice.equals(
"1"
)) {
202.
addCars();
203.
}
else
if
(choice.equals(
"2"
)) {
204.
updateCar();
205.
}
else
if
(choice.equals(
"3"
)) {
206.
searchCar();
207.
}
else
if
(choice.equals(
"4"
)) {
208.
deleteCar();
209.
}
else
if
(choice.equals(
"9"
)) {
210.
viewCars();
211.
}
else
if
(choice.equals(
"0"
)) {
212.
break
;
213.
}
214.
}
215.
}
216.
217.
public
static
void
main(String[] args) {
218.
System.out.println(
"CarDB.main()"
);
219.
CarDB CarDB =
new
CarDB();
220.
CarDB.mainMenu();
221.
}
222.
}