Skip to content

Commit 49d8dee

Browse files
committed
Insertion functionality and code refactoring
1 parent 5a51b60 commit 49d8dee

File tree

3 files changed

+103
-28
lines changed

3 files changed

+103
-28
lines changed

jdbc/JDBCDemo/src/App.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ public static void main(String[] args) {
77
Class.forName("com.mysql.cj.jdbc.Driver");
88

99
CityController cities = new CityController();
10-
cities.index();
10+
cities.create();
11+
cities.read(); // prints the details of all cities
12+
cities.readOne(); // prints the details of one city
1113
} catch (Exception e) {
1214
System.out.println("Some error occurred in the application " + e);
1315
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
package controllers;
22

33
import java.sql.ResultSet;
4-
import java.sql.SQLException;
54
import models.City;
65

6+
/**
7+
* Controller class for operations on city table
8+
*/
79
public class CityController {
8-
public void index() { // Read operation
9-
try {
10-
ResultSet result = City.fetchTenCities();
11-
System.out.println("Id\tName\tCountryCode\tDistrict\tPopulation");
12-
while (result != null && result.next()) {
13-
int id = result.getInt(1);
14-
String name = result.getString(2);
15-
String countryCode = result.getString(3);
16-
String district = result.getString(4);
17-
int population = result.getInt(5);
18-
System.out.println(id + "\t" + name + "\t" + countryCode + "\t" + district + "\t" + population);
19-
}
20-
} catch (SQLException | NullPointerException e) {
21-
System.out.println("Error in reading the ResultSet");
22-
}
10+
/**
11+
* Retrieves the details of all the cities and prints them
12+
*/
13+
public void read() {
14+
ResultSet result = City.fetchAllCityDetails();
15+
City.printCityDetails(result);
2316
}
17+
18+
/**
19+
* Retrieves the details of the city correspondinf to the id
20+
*/
21+
public void readOne() {
22+
ResultSet result = City.fetchCityDetailsById(4500);
23+
City.printCityDetails(result);
24+
}
25+
26+
public void create() {
27+
int rowsAffected = City.insertCity(4500, "Old Delhi", "IND", "Delhi", 12737);
28+
System.out.println(rowsAffected + " records were inserted");
29+
}
30+
31+
// public int update() {
32+
33+
// }
34+
35+
// public int delete() {
36+
37+
// }
2438
}

jdbc/JDBCDemo/src/models/City.java

+70-11
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,38 @@
66
public class City {
77
private static final Connection connection = ConnectionManager.getConnection();
88

9-
public static ResultSet fetchTenCities() {
9+
public static void printCityDetails(ResultSet result) {
10+
if (result != null) {
11+
System.out.println("\nId\tName\tCountryCode\tDistrict\tPopulation");
12+
try {
13+
if (!result.next()) {
14+
System.out.println("Empty set!");
15+
} else {
16+
do {
17+
int id = result.getInt(1);
18+
String name = result.getString(2);
19+
String countryCode = result.getString(3);
20+
String district = result.getString(4);
21+
int population = result.getInt(5);
22+
System.out.println(id + "\t" + name + "\t" + countryCode + "\t" + district + "\t" + population);
23+
} while (result.next());
24+
}
25+
} catch (SQLException | NullPointerException e) {
26+
System.out.println("Error while printing the city details: " + e);
27+
}
28+
} else {
29+
System.out.println("ResultSet null");
30+
}
31+
}
32+
33+
public static ResultSet fetchAllCityDetails() {
1034
ResultSet result = null;
1135
String sql = "SELECT * FROM city LIMIT ?";
1236
try {
1337
PreparedStatement preparedStatement = connection.prepareStatement(sql);
1438
preparedStatement.setInt(1, 10);
1539
try {
1640
result = preparedStatement.executeQuery();
17-
// System.out.println("Printed from model");
18-
// while (result != null && result.next()) {
19-
// int id = result.getInt(1);
20-
// String name = result.getString(2);
21-
// String countryCode = result.getString(3);
22-
// String district = result.getString(4);
23-
// int population = result.getInt(5);
24-
// System.out.println(id + "\t" + name + "\t" + countryCode + "\t" + district +
25-
// "\t" + population);
26-
// }
2741
} catch (SQLException e) {
2842
System.out.println("Error in the resultset " + e);
2943
}
@@ -32,4 +46,49 @@ public static ResultSet fetchTenCities() {
3246
}
3347
return result;
3448
}
49+
50+
/**
51+
* Retrieves the details of one city by its id
52+
*
53+
* @param id int
54+
* @return ResultSet
55+
*/
56+
public static ResultSet fetchCityDetailsById(int id) {
57+
ResultSet result = null;
58+
String sql = "SELECT * FROM city WHERE id = ?";
59+
try {
60+
PreparedStatement preparedStatement = connection.prepareStatement(sql);
61+
preparedStatement.setInt(1, id);
62+
try {
63+
result = preparedStatement.executeQuery();
64+
} catch (SQLException e) {
65+
System.out.println("Error in the resultset " + e);
66+
}
67+
} catch (SQLException e) {
68+
System.out.println("Error obtaining an instance of the statement " + e);
69+
}
70+
return result;
71+
}
72+
73+
public static int insertCity(int id, String name, String countryCode, String district, int population) {
74+
int rowsAffected = 0;
75+
String sql = "insert into city (Id, Name, CountryCode, District, Population) VALUES (?, ?, ?, ?, ?)";
76+
try {
77+
PreparedStatement statement = connection.prepareStatement(sql);
78+
statement.setInt(1, id);
79+
statement.setString(2, name);
80+
statement.setString(3, countryCode);
81+
statement.setString(4, district);
82+
statement.setInt(5, population);
83+
try {
84+
rowsAffected = statement.executeUpdate();
85+
} catch (SQLException e) {
86+
System.out.println("Error during insertion " + e);
87+
}
88+
} catch (SQLException e) {
89+
System.out.println("Error obtaining an instance of the statement " + e);
90+
}
91+
return rowsAffected;
92+
}
93+
3594
}

0 commit comments

Comments
 (0)