-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMainApplication.java
More file actions
116 lines (103 loc) · 4.25 KB
/
MainApplication.java
File metadata and controls
116 lines (103 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import com.mysql.cj.jdbc.Driver;
import daos.VehicleRepo;
import models.Vehicle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringJoiner;
/**
* @author git-leon
* @version 1.0.0
* @date 8/2/21 9:49 AM
*/
public class MainApplication {
public static void main(String[] args) {
registerJDBCDriver();
Connection mysqlDbConnection = getConnection("mysql");
VehicleRepo vehicleRepo = new VehicleRepo(mysqlDbConnection);
executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS daolab;");
executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS daolab;");
executeStatement(mysqlDbConnection, "USE daolab;");
executeStatement(mysqlDbConnection, new StringBuilder()
.append("CREATE TABLE IF NOT EXISTS daolab.vehicle(")
.append("primaryId int auto_increment primary key,")
.append("make text not null,")
.append("model text not null,")
.append("color text not null,")
.append("vtype text not null,")
.append("mpg int not null);")
.toString());
Vehicle vehicle = new Vehicle(12L, "Nissan", "Maxima", "Charcoal", "Car", 40 );
vehicleRepo.create(vehicle);
vehicleRepo.create(new Vehicle(13L, "Ford", "F150", "Blue", "Truck", 22 ));
vehicleRepo.update(12L, new Vehicle("Ford", "F150", "Blue", "Truck", 10 ));
vehicleRepo.delete(vehicle);
System.out.println(vehicleRepo.readAll());
}
static ResultSet executeQuery(Connection connection, String sqlQuery) {
try {
Statement statement = getScrollableStatement(connection);
return statement.executeQuery(sqlQuery);
} catch (SQLException e) {
throw new Error(e);
}
}
static void printResults(ResultSet resultSet) {
try {
for (int rowNumber = 0; resultSet.next(); rowNumber++) {
String firstColumnData = resultSet.getString(1);
String secondColumnData = resultSet.getString(2);
String thirdColumnData = resultSet.getString(3);
System.out.println(new StringJoiner("\n")
.add("Row number = " + rowNumber)
.add("First Column = " + firstColumnData)
.add("Second Column = " + secondColumnData)
.add("Third column = " + thirdColumnData));
}
} catch (SQLException e) {
throw new Error(e);
}
}
static void executeStatement(Connection connection, String sqlStatement) {
try {
Statement statement = getScrollableStatement(connection);
statement.execute(sqlStatement);
} catch (SQLException e) {
throw new Error(e);
}
}
static Statement getScrollableStatement(Connection connection) {
int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
try { // scrollable statements can be iterated more than once without closing
return connection.createStatement(resultSetType, resultSetConcurrency);
} catch (SQLException e) {
throw new Error(e);
}
}
static Connection getConnection(String dbVendor) {
String username = "carl";
String password = "carlpass";
String url = new StringBuilder()
.append("jdbc:")
.append(dbVendor)
.append("://127.0.0.1/")
.append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC")
.toString();
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
throw new Error(e);
}
}
static void registerJDBCDriver() {
// Attempt to register JDBC Driver
try {
DriverManager.registerDriver(Driver.class.newInstance());
} catch (InstantiationException | IllegalAccessException | SQLException e1) {
throw new RuntimeException(e1);
}
}
}