-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMainApplication.java
More file actions
115 lines (101 loc) · 4.36 KB
/
MainApplication.java
File metadata and controls
115 lines (101 loc) · 4.36 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
import com.mysql.cj.jdbc.Driver;
import daos.CarRepository;
import models.Car;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringJoiner;
public class MainApplication {
public static void main(String[] args) {
registerJDBCDriver();
Connection mysqlDbConnection = getConnection("mysql");
CarRepository carRepository = new CarRepository(mysqlDbConnection);
executeStatement(mysqlDbConnection, "DROP DATABASE IF EXISTS carDatabase;");
executeStatement(mysqlDbConnection, "CREATE DATABASE IF NOT EXISTS carDatabase;");
executeStatement(mysqlDbConnection, "USE carDatabase;");
executeStatement(mysqlDbConnection, new StringBuilder()
.append("CREATE TABLE IF NOT EXISTS carDatabase.carTable(")
.append("id int auto_increment primary key,")
.append("make text not null,")
.append("model text not null,")
.append("year int,")
.append("color text,")
.append("vin int);")
.toString());
carRepository.create(new Car(1L,"honda", "accord", 1995, "black", 100L));
// carRepository.create(new Car());
System.out.println(carRepository.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);
String fourthColumnData = resultSet.getString(4);
String fifthColumnData = resultSet.getString(5);
String sixthColumnData = resultSet.getString(6);
System.out.println(new StringJoiner("\n")
.add("Row number = " + rowNumber)
.add("First Column = " + firstColumnData)
.add("Second Column = " + secondColumnData)
.add("Third column = " + thirdColumnData)
.add("Fourth Column = " + fourthColumnData)
.add("Fifth Column = " + fifthColumnData)
.add("Sixth Column = " + sixthColumnData));
}
} 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 = "root";
String password = "password";
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);
}
}
}