-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathConnectionToDatabase.java
More file actions
195 lines (157 loc) · 5.55 KB
/
ConnectionToDatabase.java
File metadata and controls
195 lines (157 loc) · 5.55 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package daos;
//import com.mysql.jdbc.Driver;
//import com.sun.jdi.connect.Connector;
import java.sql.*;
import java.util.HashSet;
import java.util.Set;
public class ConnectionToDatabase {
private static final String URL = "jdbc:mysql://localhost:5432/cars";
private static final String USER = "root";
private static final String PASS = "root";
private static Connection con;
private static Statement stmt;
private static ResultSet rs;
public static ConnectionToDatabase connectionToDatabase = new ConnectionToDatabase();
public static Connection getConnection() {
try {
// DriverManager.registerDriver(new Driver());
return DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException ex) {
throw new RuntimeException("Error connecting to the database", ex);
}
}
public static void main(String[] args) {
Connection connection = connectionToDatabase.getConnection();
}
public static void javaToPostgres() {
String query = "select count(*) from cars";
try {
con = DriverManager.getConnection(URL, USER, PASS);
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
int count = rs.getInt(1);
System.out.println("Total number of cars in the table : " + count);
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException se) {
}
try {
stmt.close();
} catch (SQLException se) {
}
try {
rs.close();
} catch (SQLException se) {
}
}
}
private Car extractUserFromResultSet(ResultSet rs) throws SQLException {
Car car = new Car();
car.setId(rs.getInt("id"));
car.setMake(rs.getString("make"));
car.setModel(rs.getString("model"));
car.setYear(rs.getInt("year"));
car.setColor(rs.getString("color"));
car.setVin(rs.getString("vin"));
return car;
}
public Car getCar(int id) {
Connection connection = connectionToDatabase.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE id=" + id);
if (rs.next()) {
return extractUserFromResultSet(rs);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
public Car getUserByUserNameAndPassword(String user, String pass) {
Connection connection = connectionToDatabase.getConnection();
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM user WHERE user=? AND pass=?");
ps.setString(1, user);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return extractUserFromResultSet(rs);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
public Set getAllUsers() {
Connection connection = connectionToDatabase.getConnection();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
Set cars = new HashSet();
while (rs.next()) {
Car car = extractUserFromResultSet(rs);
cars.add(car);
}
return cars;
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
public boolean insertUser(Car car) {
Connection connection = connectionToDatabase.getConnection();
try {
PreparedStatement ps = connection.prepareStatement("INSERT INTO user VALUES (NULL, ?, ?, ?,?,?)");
ps.setString(1, car.getMake());
ps.setString(2, car.getModel());
ps.setInt(3, car.getYear());
ps.setString(4, car.getColor());
ps.setString(5, car.getVin());
int i = ps.executeUpdate();
if (i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
public boolean updateUser(Car car) {
Connection connection = connectionToDatabase.getConnection();
try {
PreparedStatement ps = connection.prepareStatement("UPDATE user SET make=?, model=?, year=?,color=?, vin=? WHERE id=?");
ps.setString(1, car.getMake());
ps.setString(2, car.getModel());
ps.setInt(3, car.getYear());
ps.setString(4, car.getColor());
ps.setString(5, car.getVin());
ps.setInt(6, car.getId());
int i = ps.executeUpdate();
if (i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
public boolean deleteUser(int id) {
Connection connection = connectionToDatabase.getConnection();
try {
Statement stmt = connection.createStatement();
int i = stmt.executeUpdate("DELETE FROM user WHERE id=" + id);
if (i == 1) {
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
}