forked from deargle/JavaMysqlSshConnectioninator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBConnect.java
78 lines (65 loc) · 2.28 KB
/
DBConnect.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
package dbconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Deargle
*/
public class DBConnect {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String db_name = "";
String db_host = "localhost";
String db_user = "";
String db_password = "";
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
if (db_name.isEmpty() || db_host.isEmpty() || db_user.isEmpty() || db_password.isEmpty()) {
throw new Exception("Set all of the the db_vars at the beginning of DBConnect, please.");
}
int localBindingPort = PortForwarder.openConnection();
String url = String.format("jdbc:mysql://%s:%s/%s", db_host, localBindingPort, db_name);
con = DriverManager.getConnection(url, db_user, db_password);
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM internetsearch_submit");
while (rs.next()) {
System.out.println(rs.getString(2)); // dpeck5, this is where you work your magic
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(DBConnect.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} catch (Exception e) {
System.err.println(e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(DBConnect.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
// Close the ssh tunnel, too
try {
PortForwarder.closeConnection();
} catch (Exception e) {
System.err.println(e);
}
}
}
}