-
Notifications
You must be signed in to change notification settings - Fork 1
/
ToXMLCollectionExample.java
131 lines (118 loc) · 3.78 KB
/
ToXMLCollectionExample.java
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
package miscs;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; // import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import model.TaxBean;
import model.TaxCollection;
/**
* An example for serializing the retrieved database
* records as a XML document. Returns a list of the provinces
* that have PST greater than the given number.
*
* Usage from command-line:
*
* java ToXMLCollectionExample <pst>
*
* Example:
*
* $ java ToXMLCollectionExample 9.0
* Connected to database: jdbc:sqlite:/cs/home/vwchu/4413/pkg/sqlite/Models_R_US.db
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
* <taxes>
* <tax>
* <code>NB</code>
* <gst>5.0</gst>
* <name>New-Brunswick</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>NL</code>
* <gst>5.0</gst>
* <name>Newfoundland and Labrador</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>NS</code>
* <gst>5.0</gst>
* <name>Nova Scotia</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>PE</code>
* <gst>5.0</gst>
* <name>Prince Edward Island</name>
* <pst>10.0</pst>
* <type>HST</type>
* </tax>
* <tax>
* <code>QC</code>
* <gst>5.0</gst>
* <name>Québec</name>
* <pst>9.975000000000001</pst>
* <type>QST+GST</type>
* </tax>
* </taxes>
*
* Disconnected from database.
*
*/
public class ToXMLCollectionExample {
private static PrintStream log = System.out;
public static void main(String[] args) {
String home = System.getProperty("user.home");
String url = "jdbc:sqlite:" + home + "/4413/pkg/sqlite/Models_R_US.db";
double pst = Double.parseDouble(args[0]);
try (Connection connection = DriverManager.getConnection(url)) {
log.printf("Connected to database: %s\n", connection.getMetaData().getURL());
String query = "SELECT * FROM Tax WHERE pst > ?";
/**
* !!! This is not secure (SQL injection) !!!
*
* String query = "SELECT * FROM Tax WHERE pst > " + pst;
* Statement statement = connection.createStatement();
* ResultSet rs = statement.executeQuery(query);
*/
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setDouble(1, pst);
try (ResultSet rs = statement.executeQuery()) {
List<TaxBean> list = new ArrayList<>();
TaxCollection collection = new TaxCollection();
while (rs.next()) {
TaxBean bean = new TaxBean();
bean.setName(rs.getString("province"));
bean.setCode(rs.getString("code"));
bean.setType(rs.getString("type"));
bean.setGst(rs.getDouble("gst"));
bean.setPst(rs.getDouble("pst"));
list.add(bean);
}
collection.setTaxes(list);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
JAXBContext context = JAXBContext.newInstance(TaxCollection.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(collection, baos);
log.println(baos);
} catch (Exception e) {
log.println(e);
}
}
}
} catch (SQLException e) {
log.println(e);
} finally {
log.println("Disconnected from database.");
}
}
}