-
Notifications
You must be signed in to change notification settings - Fork 0
/
JDBCClient.java
197 lines (173 loc) · 5 KB
/
JDBCClient.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
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
191
192
193
194
195
196
197
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
@SuppressWarnings("nls")
public class JDBCClient {
public static void main(String[] args) throws Exception {
execute(getDriverConnection(args[0]), args[1]);
}
static Connection getDriverConnection(String propsFile) throws Exception {
Class.forName("com.sas.net.sharenet.ShareNetDriver");
Properties props = new Properties();
File f = new File(propsFile);
if (f.exists()) {
System.out.println("Using the property file "+f.getName());
props.load(new FileInputStream(f));
}
else {
System.out.println("Property file not found "+f.getName());
}
System.out.println("Properties:"+props);
System.out.println("Trying to Connect...");
return DriverManager.getConnection(props.getProperty("url"), props);
}
public static void execute(Connection connection, String sql) throws Exception {
try {
System.out.println("Connection Sucessful...");
System.out.println("Starting to Execute SQL = "+sql);
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(sql);
ResultSetMetaData metadata = results.getMetaData();
int columns = metadata.getColumnCount();
System.out.println("Results");
for (int row = 1; results.next(); row++) {
System.out.print(row + ": ");
for (int i = 0; i < columns; i++) {
if (i > 0) {
System.out.print(",");
}
Object obj = results.getObject(i+1);
System.out.print(obj);
}
System.out.println();
}
System.out.println("Query Plan");
results.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
public static void executeCall(Connection connection, String sql) throws Exception {
try {
CallableStatement statement = connection.prepareCall(sql);
//statement.registerOutParameter(1, Types.VARCHAR);
boolean haveResults = statement.execute();
if (haveResults) {
ResultSet results = statement.getResultSet();
ResultSetMetaData metadata = results.getMetaData();
int columns = metadata.getColumnCount();
System.out.println("Results");
for (int row = 1; results.next(); row++) {
System.out.print(row + ": ");
for (int i = 0; i < columns; i++) {
if (i > 0) {
System.out.print(",");
}
System.out.print(results.getString(i+1));
}
System.out.println();
}
results.close();
statement.close();
}
else {
System.out.println("result="+statement.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
public static void executeUpdate(Connection connection, String csvFile) throws Exception {
try {
List<List<String>> rows = new ArrayList<List<String>>();
String sql = parseCSV(csvFile, rows);
PreparedStatement statement = connection.prepareStatement(sql);
for (List<String> row:rows) {
for (int i = 0; i < row.size(); i++) {
String value = row.get(i);
if (value.equals("null")) {
statement.setObject(i+1, null);
}
else {
statement.setObject(i+1, value);
}
}
statement.addBatch();
}
int[] updateCount = statement.executeBatch();
System.out.println("update-count = ");
for (int i = 0; i < updateCount.length; i++) {
System.out.println(updateCount[i]);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
private static String parseCSV(String csvFile, List<List<String>> rows) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(csvFile));
String sql = reader.readLine();
try {
String line = null;
while((line=reader.readLine()) != null) {
line = line.trim();
if (line.length() > 0) {
List<String> row = new ArrayList();
StringTokenizer st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
row.add(st.nextToken());
}
rows.add(row);
}
}
} finally {
reader.close();
}
return sql;
}
public static void writeCSV(Connection connection, String sql) throws Exception {
try {
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(sql);
ResultSetMetaData metadata = results.getMetaData();
int columns = metadata.getColumnCount();
for (int row = 1; row < columns; row++) {
System.out.print(metadata.getColumnLabel(row));
System.out.print(",");
}
System.out.println();
while (results.next()){
for (int i = 0; i < columns; i++) {
if (i > 0) {
System.out.print(",");
}
System.out.print(results.getString(i+1));
}
System.out.println();
}
results.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
}