Repository navigation
Expand file tree
/
Copy pathDBConnect.java
More file actions
183 lines (139 loc) · 4.78 KB
/
Copy pathDBConnect.java
File metadata and controls
183 lines (139 loc) · 4.78 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
public class DBConnect {
private Connection con;
private Statement st;
private ResultSet rs;
public int[] arr = new int[3];
public DBConnect(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/textminingrss", "root", "");
st = con.createStatement();
} catch (Exception ex) {
System.out.println("Error " + ex);
}
}
public void insertQ(String term, int C_id){
try
{
PreparedStatement preparedStatement =
con.prepareStatement("INSERT into term_table (C_id, Term) VALUES (?,?)",
Statement.RETURN_GENERATED_KEYS);
preparedStatement.setInt(1,C_id);
preparedStatement.setString(2,term);
preparedStatement.executeUpdate();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
System.out.println(e);
}
}
public void deleteTable(){
try {
st = con.createStatement();
st.executeUpdate("DROP TABLE temptable");
}catch(Exception io){
System.out.println(io);
}
}
public void createnewtable(){
try{
st = con.createStatement();
st.execute("CREATE TABLE temptable AS select Term,C_id,count(*) as freq from term_table GROUP by C_id,Term");
}catch (Exception io){
System.out.println(io);
}
}
public void formulate(ArrayList<String> article){
for(String var : article){
getTermCid(var);
}
}
public void getTermCid(String term){
try{
ArrayList<Integer> freqq= new ArrayList<>();
ArrayList<Integer> cid= new ArrayList<>();
String query = "SELECT Term,C_id,freq FROM `temptable` WHERE term = '"+term+"'";
rs = st.executeQuery(query);
while (rs.next()){
System.out.println("Term :"+rs.getString("Term")+" Freq :"+rs.getInt("freq") +" C_ID:"+rs.getInt("C_id"));
cal(rs.getInt("freq"),rs.getInt("C_id"));
}
}catch(Exception ex)
{
System.out.println(ex);
}
}
public void cal(int fre, int cid){
if(cid == 1)
{
arr[0] = arr[0] + fre;
}else if(cid == 2){
arr[1] = arr[1] + fre;
}else{
arr[2] = arr[2] + fre;
}
}
public int[] AnswerArr(){
return arr;
}
public int getIndexOfLargest( int[] array )
{
if ( array == null || array.length == 0 ) return -1; // null or empty
int largest = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[largest] ) largest = i;
}
return largest; // position of the first largest found
}
public void AnswerCategory(int ans){
if(ans == 0){
System.out.println("The string is of category SCIENCE");
}
else if(ans == 1){
System.out.println("The string is of category POLITICS");
}else{
System.out.println("The string is of category SPORT");
}
}
public ArrayList<ForExcel> check(ArrayList<String> abc,int cid){
ArrayList<ForExcel> exp = new ArrayList<>();
for(String var : abc){
exp.add(InsertingCSVvalues(cid,var ));
}
return exp;
}
public ForExcel InsertingCSVvalues(int cid,String term){
ForExcel exl = null;
try {
exl = new ForExcel();
String query = "select * from temptable where term = '" + term+"' and C_id = '"+cid+"'";
rs = st.executeQuery(query);
while(rs.next()){
//System.out.println(rs.getString("Term")+" "+rs.getInt("freq")+" "+rs.getInt("C_id"));
exl.setTerm1(rs.getString("Term"));
exl.setTerm2(rs.getInt("freq"));
}
}catch(Exception e){
System.out.println(e);
}
return exl;
}
public ArrayList<String> removeDup(ArrayList<String> ex){
ArrayList<String> temp = new ArrayList<>();
Set<String> setString = new LinkedHashSet<>();
for(int i=0;i<ex.size();i++){
setString.add(ex.get(i));
}
for(String exx : setString){
temp.add(exx);
}
return temp;
}
}