generated from ohjelmointi2/gradle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcDemoMain.java
More file actions
105 lines (93 loc) · 4.05 KB
/
JdbcDemoMain.java
File metadata and controls
105 lines (93 loc) · 4.05 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
package databases.part01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* A simple application that prints all artists in the Chinook database.
* This version uses JDBC directly, and the result is verbose, hard to test and
* not reusable. It is still useful to understand the basics of JDBC.
*
* We'll improve this in the next part.
*/
public class JdbcDemoMain {
/**
* The connection string used to connect to the database. If you are using a
* MySQL database, you will need to change this string. For example:
*
* jdbc:mysql://localhost:3306/Chinook?user=CHANGE&password=CHANGE
*
* If you change the connection string, *please change it back* before
* submitting your code, as the first test depends on the SQLite database.
*/
private static final String JDBC_CONNECTION_STRING = "jdbc:sqlite:data/Chinook_Sqlite.sqlite";
public static void main(String[] args) {
/*
* These variables are declared outside of the try block so they can be closed
* in the finally block. This ensures that the resources are closed even if an
* exception is thrown and the try block is exited early.
*/
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// Create a connection to the database
connection = DriverManager.getConnection(JDBC_CONNECTION_STRING);
// Create a prepared statement to execute a query
preparedStatement = connection.prepareStatement("SELECT ArtistId, Name FROM Artist ORDER BY ArtistId ASC");
// Execute the query and get the result set
resultSet = preparedStatement.executeQuery();
/*
* Iterate over the result set and print the results. The result set contains
* the rows returned by the query. Each time next() is called, the result set
* moves to the next row.
*
* If next() returns false, then there are no more rows in the result set, and
* the loop terminates.
*/
while (resultSet.next()) {
/*
* getLong() and getString() are used to retrieve the values from the current
* row in the result set. The argument passed to these methods is the name of
* the column in the result set. There are also methods for other types of
* data, such as getInt() and getDouble().
*/
String name = resultSet.getString("Name");
System.out.println(name);
}
} catch (SQLException e) {
/*
* Operations that access the database can throw SQLExceptions. SQLException is
* a checked exception, so it must be caught or thrown. Here we don't really
* handle the exception, we just print the stack trace and exit.
*/
e.printStackTrace();
} finally {
/*
* Close the result set, prepared statement, and connection in the finally block
* to ensure they are closed even if an exception is thrown.
*
* This is a bit verbose, and you could either implement a utility method to
* close the resources, or use a try-with-resources block.
*/
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
/*
* Even operations that close the resources can throw exceptions, so we still
* need to catch them.
*/
e.printStackTrace();
}
}
}
}