6
6
public class City {
7
7
private static final Connection connection = ConnectionManager .getConnection ();
8
8
9
- public static ResultSet fetchTenCities () {
9
+ public static void printCityDetails (ResultSet result ) {
10
+ if (result != null ) {
11
+ System .out .println ("\n Id\t Name\t CountryCode\t District\t Population" );
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 () {
10
34
ResultSet result = null ;
11
35
String sql = "SELECT * FROM city LIMIT ?" ;
12
36
try {
13
37
PreparedStatement preparedStatement = connection .prepareStatement (sql );
14
38
preparedStatement .setInt (1 , 10 );
15
39
try {
16
40
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
- // }
27
41
} catch (SQLException e ) {
28
42
System .out .println ("Error in the resultset " + e );
29
43
}
@@ -32,4 +46,49 @@ public static ResultSet fetchTenCities() {
32
46
}
33
47
return result ;
34
48
}
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
+
35
94
}
0 commit comments