-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecute.java
More file actions
167 lines (130 loc) · 5.34 KB
/
Execute.java
File metadata and controls
167 lines (130 loc) · 5.34 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.io.*;
import java.net.Socket;
import java.sql.*;
import java.util.*;
import com.jcraft.jsch.*;
import com.mysql.jdbc.ConnectionImpl;
/**
* CS410 database project: Twitter System
* Group 6: Jiahang Li, Kenny Overly, Michael Plaisance
* Driver Class: Connect to DB, run queries/updates
* @author MichaelPlaisance
*
*/
public class Execute {
private String strSshUser; // SSH loging username
private String strSshPassword; // SSH login password
private String strDBname; // Database name
private String strQueryUpdate; // Either 'Query' or 'Update'
private String strTaskNumber; // The task number that will be queried or updated
private String strTaskQueryFile; // The txt file name that contains the command
private String strOutputFile; // The outputted txt file
public static void main(String[] args) throws SQLException {
//if (args.length != 7 || args.length != 8){
// System.out.println("Usage Execute <BroncoUser> <BroncoPassword> <DBname> <query/update> <TaskNumber> <TaskQueryFile> <outputFile> <parametersforQuery>");
//}
// else{
Connection con = null;
Session session = null;
try
{
String strSshUser = args[0]; // SSH loging username
String strSshPassword = args[1]; // SSH login password
String strDBname = args[2]; // Database name
String strQueryUpdate = args[3]; // Either 'Query' or 'Update'
String strTaskNumber = args[4]; // The task number that will be queried or updated
String strTaskQueryFile = args[5]; // The txt file name that contains the command
String strOutputFile = args[6]; // The outputted txt file
if(args.length == 8){
String strParametersForQuery = args[7]; // Parameters if 'Query' is selected
}
String strSshHost = "onyx.boisestate.edu"; // hostname or ip or SSH server
int nSshPort = 22; // remote SSH host port number
String strRemoteHost = "localhost"; // hostname or ip of your database server
int nLocalPort = 3367; // local port number use to bind SSH tunnel
String strDbUser = "msandbox"; // database loging username
String strDbPassword = "123456"; // database login password
int nRemotePort = 10167; // remote port number of your database
/*
* STEP 0
* CREATE a SSH session to ONYX
*
* */
System.out.println("Attempting to connect to " + strSshUser + "...");
session = Execute.doSshTunnel(strSshUser, strSshPassword, strSshHost, nSshPort, strRemoteHost, nLocalPort, nRemotePort);
/*
* STEP 1 and 2
* LOAD the Database DRIVER and obtain a CONNECTION
*
* */
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:"+nLocalPort, strDbUser, strDbPassword);
System.out.println("Connected database successfully...");
/*
* STEP 3
* EXECUTE STATEMENTS
*
* */
Statement stmt = null;
stmt = con.createStatement();
ResultSet resultSet = stmt.executeQuery("select * from `COMPANY`.`EMPLOYEE`");
/*
* STEP 4
* Use result sets (tables) to navigate through the results
*
* */
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = resultSet.getString(i);
System.out.print(columnValue + " " + rsmd.getColumnName(i));
}
System.out.println(" ");
}
/*TO INSERT INTO TABLES
* You can also read from a file and store in a data structure of your choice*/
// String[] data = {"boise", "nampa"};
// insertLocations(con,data);
}
catch( Exception e )
{
e.printStackTrace();
}
finally{
/*
* STEP 5
* CLOSE CONNECTION AND SSH SESSION
*
* */
con.close();
session.disconnect();
}
}
// }
private static Session doSshTunnel( String strSshUser, String strSshPassword, String strSshHost, int nSshPort, String strRemoteHost, int nLocalPort, int nRemotePort ) throws JSchException
{
/*This is one of the available choices to connect to mysql
* If you think you know another way, you can go ahead*/
final JSch jsch = new JSch();
java.util.Properties configuration = new java.util.Properties();
configuration.put("StrictHostKeyChecking", "no");
Session session = jsch.getSession( strSshUser, strSshHost, 22 );
session.setPassword( strSshPassword );
session.setConfig(configuration);
session.connect();
session.setPortForwardingL(nLocalPort, strRemoteHost, nRemotePort);
return session;
}
private static void insertLocations(Connection con, String[] data) throws SQLException {
String sql;
java.sql.Statement stmt = null;
stmt = con.createStatement();
for(int i=0;i<data.length;i++){
sql = "INSERT INTO `COMPANY`.`DEPT_LOCATIONS`(`DNUMBER`,`DLOCATION`)VALUES(1,'"+data[i]+"')";
int res = stmt.executeUpdate(sql);
System.out.println(res);
}
}
}