/
ExtractFromDB.java
144 lines (110 loc) · 4.8 KB
/
ExtractFromDB.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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.*;
public class ExtractFromDB {
private static String sqlUser = "databaseUsername";
private static String sqlPass = "databasePassword";
private static String database = "databasename";
private static String pathToItemsXml = "C:\path to\items.xml";
public static void main(String[] args) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
// handle the error
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/"+database+"?" +
"user="+sqlUser+"&password="+sqlPass);
} catch (SQLException ex) {
// handle any errors
System.err.println("SQLException: " + ex.getMessage());
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("VendorError: " + ex.getErrorCode());
System.exit(1);
}
String item = "\t<item ", ATTR = "\t\t<attribute ",ENDITEM = "\t</item>\n";
String ID = "id", NAME = "name", FROMID = "fromid", TOID = "toid", ARTICLE = "article", KEY = "key", VALUE = "value", CHANCE = "chance", RANDOM_MIN="random_min", RANDOM_MAX="random_max";
String EQ = "=\"",CLOSE = "\" ",ENDSLASH="/>\n",END=">\n";
String SELECT_ATTR_QUERRY = "SELECT * FROM item_attributes WHERE item_id=";
BufferedWriter writer = null;
try{
writer = new BufferedWriter(new FileWriter(pathToItemsXml));
writer.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
writer.write("<items>\n");
stmt = conn.createStatement();
String query = "SELECT * FROM items;";
//System.out.println(query);
ResultSet items = stmt.executeQuery(query);
int size = items.getFetchSize();
int count=0;
while(items.next()){
if( count == size/4 )
System.out.println("Quarter way done!");
if( count == size/2 )
System.out.println("Half way done!");
if( count == (size*3)/4 )
System.out.println("Three quarters done!");
count++;
int id = items.getInt(ID);
String name = items.getString(NAME);
int fromid = items.getInt(FROMID);
int toid = items.getInt(TOID);
String article = items.getString(ARTICLE);
writer.write(item);
if( id == fromid )
writer.write(FROMID+EQ+fromid+CLOSE);
else
writer.write(ID+EQ+id+CLOSE);
if( toid != -1 )
writer.write(TOID+EQ+toid+CLOSE);
if( article != null )
writer.write(ARTICLE+EQ+article+CLOSE);
if( name != null )
writer.write(NAME+EQ+name+CLOSE);
writer.flush();
boolean hasAttributes = false;
stmt = conn.createStatement();
ResultSet attributes = stmt.executeQuery(SELECT_ATTR_QUERRY+id);
while( attributes.next() ){
if( !hasAttributes ) {
writer.write(END);
hasAttributes = true;
}
writer.write(ATTR);
String key = attributes.getString(KEY);
String value = attributes.getString(VALUE);
int chance = attributes.getInt(CHANCE);
int random_min = attributes.getInt(RANDOM_MIN);
int random_max = attributes.getInt(RANDOM_MAX);
writer.write(KEY+EQ+key+CLOSE);
writer.write(VALUE+EQ+value+CLOSE);
if( chance != -1 )
writer.write(CHANCE+EQ+chance+CLOSE);
if( random_min != -1 )
writer.write(RANDOM_MIN+EQ+random_min+CLOSE);
if( random_max != -1 )
writer.write(RANDOM_MAX+EQ+random_max+CLOSE);
writer.write(ENDSLASH);
}
if(hasAttributes)
writer.write(ENDITEM);
else
writer.write(ENDSLASH);
writer.flush();
}
writer.write("</items>");
//Close writer
writer.close();
}
finally{
writer.close();
}
System.out.println("COMPLETE!");
}
}