-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentDayGainLoose.java
141 lines (119 loc) · 4.17 KB
/
CurrentDayGainLoose.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
package stockanalysis;
import java.io.*;
import java.sql.*;
import java.text.DecimalFormat;
public class CurrentDayGainerLooser {
public static void insertIntoGL() throws IOException{
DecimalFormat df = new DecimalFormat("#.###");
PercentChange pchng = new PercentChange();
String ticker;
FileReader fr = new FileReader("/home/nani/work/nsetest.txt");
BufferedReader br = new BufferedReader(fr);
Connection conn = GetDBConnection.getConnection("STOCKT_NSE");
Statement stmt=null;
PreparedStatement pst = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
while((ticker = br.readLine())!=null){
String tick = ticker.concat(".NS");
try {
double chng[] = pchng.getCurrentDayChng(tick);
//System.out.println("companies increased");
if(chng[0]>0){
System.out.println("companies increased");
ResultSet rs = conn.getMetaData().getTables(null, null , "gainers" , null);
if(stmt!=null){
if(!rs.next()){
//if table dosent exist create it
System.out.println("creating table gainers..");
String ctquery = "create table gainers(ticker varchar(10), increase double, percent_inc double)";
stmt.executeUpdate(ctquery);
}
System.out.println("checking whether ticker values exists or not "+tick);
String check = "select * from gainers where ticker='"+tick+"'";
System.out.println("*************************");
ResultSet rs1 = stmt.executeQuery(check);
if(rs1.next()){
System.out.println("Updating the query");
System.out.println("Change Inc:"+Double.valueOf(df.format((chng[0]))));
System.out.println("Company :"+tick);
;
/*pst = conn.prepareStatement(uquery);
pst.setDouble(1, Double.valueOf(df.format((chng[0]))));
pst.setDouble(2, Double.valueOf(df.format((chng[1]))));
pst.setString(3, tick);*/
stmt.executeUpdate(uquery);
}
else{
System.out.println("Inserting values");
String inquery = "INSERT INTO gainers values(?,?,?)";
System.out.println(Double.valueOf(df.format((chng[0]))));
PreparedStatement pstmt = conn.prepareStatement(inquery);
pstmt.setString(1, tick);
pstmt.setDouble(2, Double.valueOf(df.format((chng[0]))));
pstmt.setDouble(3, Double.valueOf(df.format((chng[1]))));
System.out.println(Double.valueOf(df.format((chng[1]))));
pstmt.executeUpdate();
}
}
}
else{
System.out.println("companies decreased");
ResultSet rs = conn.getMetaData().getTables(null, null , "losers" , null);
if(stmt!=null){
if(!rs.next()){
//if table dosent exists create it
String ctquery = "create table losers(ticker varchar(10), decrease double, percent_dec double)";
stmt.executeUpdate(ctquery);
}
String check = "select * from losers where ticker='"+tick+"'";
System.out.println("Checked decreased");
ResultSet rs1 = stmt.executeQuery(check);
if(rs1.next()){
System.out.println("Updating decreased");
String uquery = "UPDATE losers SET decrease= "+Double.valueOf(df.format((chng[0]*-1)))+" , percent_dec="+Double.valueOf(df.format((chng[1]*-1)))+" where ticker="+tick;
System.out.println("Updated decreased");
stmt.executeUpdate(uquery);
}
else{
String inquery = "INSERT INTO losers values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(inquery);
pstmt.setString(1, tick);
pstmt.setDouble(2, Double.valueOf(df.format((chng[0]*-1))));
pstmt.setDouble(3, Double.valueOf(df.format((chng[1]*-1))));
pstmt.executeUpdate();
}
}
}
}
catch (SQLException e) {
e.printStackTrace();
}
catch(IOException ie){
ie.printStackTrace();
}
catch(NullPointerException ne){
ne.printStackTrace();
}
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String a[]) throws IOException{
insertIntoGL();
}
}