-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJdbc.java
105 lines (95 loc) · 2.98 KB
/
Jdbc.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
package org.apache.cordova.plugin;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.StrictMode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc extends CordovaPlugin {
private Connection con = null;
public Jdbc() {
super();
}
@Override
public boolean execute(String action, final JSONArray args,
final CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("open")) {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
JSONObject myArg = args.getJSONObject(0);
int timeOut = 20;
if(myArg.has("timeOut")) {
timeOut = myArg.getInt("timeOut");
}
String url = myArg.getString("url");
// "jdbc:jtds:sqlserver://server/database";
String user = myArg.getString("user");// "user";
String password = myArg.getString("password");// "pass";
try {
// Class.forName("com.mysql.jdbc.Driver");
// Class.forName("net.sourceforge.jtds.jdbc.Driver");
Class.forName(myArg.getString("class")).newInstance();
DriverManager.setLoginTimeout(timeOut);
con = DriverManager.getConnection(url, user, password);
} catch (java.lang.ClassNotFoundException e) {
callbackContext.error(e.getLocalizedMessage());
return true;
} catch (SQLException e) {
callbackContext.error(e.getLocalizedMessage());
return true;
} catch (Exception e) {
callbackContext.error(e.getLocalizedMessage());
return true;
}
callbackContext.success();
return true;
} else if (action.equals("close")) {
try{
con.close();
callbackContext.success();
return true;
} catch(Exception e) {
callbackContext.error(e.getLocalizedMessage());
}
} else if (action.equals("executeQuery")) {
Statement st;
try {
JSONObject myArgs = args.getJSONObject(0);
st = con.createStatement();
ResultSet rs = st.executeQuery(myArgs.getString("query"));
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
int total_rows = rs.getMetaData().getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 0; i < total_rows; i++) {
obj.put(rs.getMetaData().getColumnLabel(i + 1)
.toLowerCase(), rs.getObject(i + 1));
}
jsonArray.put(obj);
}
callbackContext.success(jsonArray);
return true;
} catch (SQLException e) {
callbackContext.error(e.getLocalizedMessage());
return true;
} catch (JSONException e) {
callbackContext.error(e.getLocalizedMessage());
return true;
}
}
} catch (Exception e) {
callbackContext.error(e.getLocalizedMessage());
return true;
}
return false;
}
}