-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB.java
209 lines (187 loc) · 6.23 KB
/
DB.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
198
199
200
201
202
203
204
205
206
207
208
209
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.sql.*;
public class DB
{
// public Statement stmt;
public ResultSet rs;
Connection con;
/**
* Constructor for DB
*/
public DB() {
}
/**
* Update DB this DB object with sql statement
* @param sql the sql statement as prepared statement whit 1 ?. Must start with SELECT
* @param value the value of ?
*/
public void UpdateSQL(String sql, String value1, String value2, String value3)
{
PreparedStatement preparedStatement = null;
rs = null;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://mysql/shipplotter";
con = DriverManager.getConnection(url, "shipplotter", "shipplotter");
}
catch (Exception e) {
System.out.println("Connection Failed! Check output console" + e);
return;
}
System.out.println("MySQL JDBC Driver Registered!");
try{
System.out.printf("trying sql statement %s \n", sql);
String selectSql = sql;
preparedStatement = con.prepareStatement(selectSql);
preparedStatement.setString(1, value1);
preparedStatement.setString(2, value2);
preparedStatement.setString(3, value3);
// preparedStatement.setInt(0, 1001);
rs = preparedStatement.executeQuery();
}
catch (Exception e){
System.out.println("failed to execute statement");
System.err.println(e.getMessage());
}
}
public void close() {
try {
if(con != null)
con.close();
System.out.println("Connection closed !!");
}
catch (SQLException e) {
e.printStackTrace();
}
System.out.println("");
}
public void upload(String[] line) throws Exception
{
PreparedStatement preparedStatement = null;
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
System.out.println("MySQL JDBC Driver Registered!");
int count = 0;
try {
connection = DriverManager.getConnection("jdbc:mysql://mysql/shipplotter", "shipplotter", "shipplotter");
for (int i = 49; i < line.length; i++)
{
String[] la = line[i].split(",");
count++;
if ((count % 10) == 0) {
}
preparedStatement = connection.prepareStatement("INSERT IGNORE INTO shipplotter values (?, ?, ?, ? , ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?)");
preparedStatement.setString(1, la[0]);
preparedStatement.setString(2, la[1]);
preparedStatement.setString(3, la[2]);
preparedStatement.setString(4, la[3]);
preparedStatement.setString(5, la[4]);
preparedStatement.setString(6, la[5]);
preparedStatement.setString(7, la[6]);
preparedStatement.setString(8, la[7]);
preparedStatement.setString(9, la[8]);
preparedStatement.setString(10, la[9]);
preparedStatement.setString(11, la[10]);
preparedStatement.setString(12, la[11]);
preparedStatement.setString(13, la[12]);
preparedStatement.setString(14, la[13]);
preparedStatement.setString(15, la[14]);
preparedStatement.setString(16, la[15]);
preparedStatement.setString(17, la[16]);
preparedStatement.setString(18, la[17]);
preparedStatement.setString(19, la[18]);
preparedStatement.setString(20, la[19]);
preparedStatement.setString(21, la[20]);
preparedStatement.setString(22, la[21]);
preparedStatement.executeUpdate();
}
System.out.println(line[0]);
System.out.println(line[50]);
System.out.println(line[1]);
System.out.println(line[51]);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
System.out.println(e);
return;
} finally {
try {
if(connection != null)
connection.close();
System.out.println("Connection closed !!");
}
catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("");
}
public void uploadArea(String[] line) throws Exception
{
PreparedStatement preparedStatement = null;
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
System.out.println("MySQL JDBC Driver Registered!");
int count = 0;
try {
connection = DriverManager.getConnection("jdbc:mysql://mysql/shipplotter", "shipplotter", "shipplotter");
for (int i = 0; i < line.length; i++)
{
String[] la = line[i].split(":");
count++;
String[] poly = la[2].split(",0 ");
String polyStatement = "";
for(int j = 0; j < poly.length; j++){
polyStatement = polyStatement + poly[j].trim().replace(",", " ");
if(j + 1 < poly.length){
polyStatement = polyStatement + ",";
}
}
System.out.println(la[0].replace("\"", " "));
System.out.println(la[1].replace("\"", " "));
System.out.println("print statement");
System.out.println(polyStatement);
preparedStatement = connection.prepareStatement("INSERT IGNORE INTO areas (type, name, area) values (?, ?, ?)");
preparedStatement.setString(1, la[0].replace("\"", " "));
preparedStatement.setString(2, la[1].replace("\"", " "));
preparedStatement.setString(3, polyStatement);
preparedStatement.executeUpdate();
Scanner in2 = new Scanner(System.in);
System.out.println("Enter for next");
String s = in2.nextLine();
System.out.println("");
System.out.println("");
}
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
System.out.println(e);
return;
} finally {
try {
if(connection != null)
connection.close();
System.out.println("Connection closed !!");
}
catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("");
}
}