-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCProxy.java
More file actions
146 lines (133 loc) · 5.13 KB
/
JDBCProxy.java
File metadata and controls
146 lines (133 loc) · 5.13 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
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
import java.io.*;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.apache.commons.cli.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.net.URI;
class Connection {
private java.sql.Connection connection;
private String driverClass = null;
private String connectionString = null;
private String username = null;
private String password = null;
public Connection(String driverClass, String connectionString,
String username, String password){
this.driverClass = driverClass;
this.connectionString = connectionString;
this.username = username;
this.password = password;
this.connect();
}
public void connect(){
try{
Class.forName(this.driverClass);
connection = DriverManager.getConnection(
connectionString, username, password);
}catch(Exception e){
System.out.println("[ERROR] there was an error when connecting "+
"to the data source");
System.out.println("[ERROR] Exception: " + e);
System.out.println(e.getMessage());
System.exit(1);
}
}
public void disconnect() throws java.sql.SQLException{
this.connection.close();
}
public void printTableNames() throws SQLException{
DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while(rs.next()){
System.out.println(rs.getString(3));
}
}
public void execute(String query){
execute(query, null);
}
@SuppressWarnings("unchecked")
public void execute(String query, String file){
JSONArray result = new JSONArray();
try{
PreparedStatement statement = connection.prepareStatement(query);
if(file != null){
File f = new File(file);
statement.setBinaryStream(1, new FileInputStream(f),
(int)f.length());
}
Boolean res;
if(query.toUpperCase().startsWith("UPDATE")){
System.out.println(statement.executeUpdate());
return;
}else if(!query.toUpperCase().startsWith("SELECT")){
System.out.println(statement.execute(query));
return;
}else{
statement.execute(query);
}
ResultSet rs = statement.getResultSet();
ResultSetMetaData rsFields = rs.getMetaData();
while(rs.next()){
JSONArray row = new JSONArray();
for(int i=1; i<=rsFields.getColumnCount(); i++){
row.add(rs.getObject(i));
}
result.add(row);
}
rs.close();
statement.close();
}catch(Exception e){
System.out.println(e);
}
System.out.println(result);
}
}
public class JDBCProxy {
public static void main(String[] args) throws java.io.IOException,
java.sql.SQLException{
CommandLine commandLine = null;
try{
Options options = new Options();
options.addOption("u", true, "Username");
options.addOption("p", true, "Password");
options.addOption("c", true, "Connection string");
options.addOption("d", true, "class name for jdbc connection");
options.addOption("t", false, "print table names and exit");
options.addOption("f", true, "file location for use in prepared statement");
options.addOption("help", false, "print this help message");
CommandLineParser parser = new PosixParser();
commandLine = parser.parse(options, args);
if(commandLine.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("jdbcproxy", options);
System.exit(0);
}
}catch( ParseException exp ) {
System.out.println( "Unexpected exception:" + exp.getMessage() );
System.exit(1);
}
Connection conn = new Connection(commandLine.getOptionValue("d"),
commandLine.getOptionValue("c"),
commandLine.getOptionValue("u"),
commandLine.getOptionValue("p"));
if(commandLine.hasOption("t")){
conn.printTableNames();
System.exit(0);
}
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
String query = "";
String line = null;
while((line = f.readLine()) != null){
if(query.length()>0) query += "\n";
query += line;
}
String fileUri = commandLine.getOptionValue("f");
conn.execute(query, fileUri);
conn.disconnect();
}
}