Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package StockForecast; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.sql.ResultSet; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator; import org.json.JSONObject; /** * * @author c07nw8vqg1hw */ public class User extends javax.swing.JFrame { /** * Creates new form User * @throws java.lang.Exception */ public User() throws Exception { initComponents(); maxDate(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButton4 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); dateChooserCombo1 = new datechooser.beans.DateChooserCombo(); dateChooserCombo2 = new datechooser.beans.DateChooserCombo(); jButton3 = new javax.swing.JButton(); jButton4.setText("jButton4"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Back"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Test"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); dateChooserCombo1.setCurrentView(new datechooser.view.appearance.AppearancesList("Light", new datechooser.view.appearance.ViewAppearance("custom", new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.ButtonPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 255), true, true, new datechooser.view.appearance.swing.ButtonPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 255), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.ButtonPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(128, 128, 128), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.LabelPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.LabelPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(255, 0, 0), false, false, new datechooser.view.appearance.swing.ButtonPainter()), (datechooser.view.BackRenderer) null, false, true))); dateChooserCombo2.setCurrentView(new datechooser.view.appearance.AppearancesList("Light", new datechooser.view.appearance.ViewAppearance("custom", new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.ButtonPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 255), true, true, new datechooser.view.appearance.swing.ButtonPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 255), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.ButtonPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(128, 128, 128), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.LabelPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 255), false, true, new datechooser.view.appearance.swing.LabelPainter()), new datechooser.view.appearance.swing.SwingCellAppearance( new java.awt.Font("Lucida Grande", java.awt.Font.PLAIN, 13), new java.awt.Color(0, 0, 0), new java.awt.Color(255, 0, 0), false, false, new datechooser.view.appearance.swing.ButtonPainter()), (datechooser.view.BackRenderer) null, false, true))); jButton3.setText("test"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jButton1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGap(480, 480, 480).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dateChooserCombo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(dateChooserCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jButton2)).addGap(0, 114, Short.MAX_VALUE)))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3).addGap(267, 267, 267))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jButton1).addGap(23, 23, 23) .addComponent(dateChooserCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(dateChooserCombo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton2) .addGap(43, 43, 43).addComponent(jButton3).addContainerGap(125, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents Connect con = new Connect(); private void maxDate() { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date today = Calendar.getInstance().getTime(); Calendar c = Calendar.getInstance(); c.setTime(today); c.add(Calendar.DATE, -1); dateChooserCombo1.setMaxDate(c); dateChooserCombo2.setMaxDate(c); dateChooserCombo1.setDateFormat(df); dateChooserCombo2.setDateFormat(df); } private void checkDateCond(String prevDate, Date today, Date start, Date end) throws Exception { if (start.equals(end)) { JOptionPane.showMessageDialog(this, "Please Select Range Date"); } else if (start.equals(today)) { JOptionPane.showMessageDialog(this, "Please Select : (" + prevDate + ") as a Maximum Date"); } else if (end.equals(today)) { JOptionPane.showMessageDialog(this, "Please Select : (" + prevDate + ") as a Maximum Date"); } else if (start.compareTo(end) > 0) { JOptionPane.showMessageDialog(this, "Start Date is After End Date, Please Select Proper Range Date"); } else { System.out.println("Mari Berpesta" + "\n" + today + "\n" + start + "\n" + end); loopSymbol(start, end); } } private void loopSymbol(Date start, Date end) throws Exception { ResultSet rs = con.connectSelect("1", "SELECT * FROM ROOT.STOCK"); if (!rs.next()) { System.out.println("No Stock Data"); } do { String sym = rs.getString("symbol"); System.out.println(sym); loopDate(sym, start, end); } while (rs.next()); } private void loopDate(String sym, Date start, Date end) throws Exception { while (start.compareTo(end) <= 0) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(start); String startDate = df.format(start); ResultSet cd = con.connectSelect("1", "SELECT * FROM ROOT.STOCK NATURAL JOIN STOCKDETAILS WHERE SYMBOL='" + sym + "' AND STOCKDATE='" + startDate + "'"); if (!cd.next()) { String s = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22" + sym + ".JK%22%20and%20startDate%20%3D%20%22" + startDate + "%22%20and%20endDate%20%3D%20%22" + startDate + "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="; URL url = new URL(s); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String a = null; String strTemp = ""; while (null != (strTemp = br.readLine())) { a = strTemp; } JSONObject obj = new JSONObject(a); JSONObject query = obj.getJSONObject("query"); int count = query.getInt("count"); if (count == 0) { System.out.println("There Is No Result In This Day (" + startDate + ")"); } else { System.out.println("Query Sukses: " + count); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); JSONObject results = query.getJSONObject("results"); JSONObject quote = results.getJSONObject("quote"); String high = quote.getString("High"); String low = quote.getString("Low"); String volume = quote.getString("Volume"); String adjClose = quote.getString("Adj_Close"); String close = quote.getString("Close"); String date = quote.getString("Date"); String open = quote.getString("Open"); String exactVolume = ""; // System.out.println("Awal: "+sym+", "+date+", "+high+", "+low+", "+volume+", "+exactVolume+", "+adjClose+", "+close+", "+open); if (volume.equals("000")) { Calendar c = Calendar.getInstance(); c.setTime(start); c.add(Calendar.DATE, -1); String prev = df.format(c.getTime()); System.out.println("This is Start: " + start); System.out.println(prev); ResultSet rs = con.connectSelect("1", "SELECT * FROM ROOT.STOCK NATURAL JOIN STOCKDETAILS WHERE SYMBOL='" + sym + "' AND STOCKDATE='" + prev + "'"); if (!rs.next()) { int qcount = 0; String getDate = ""; do { Date minDate = df.parse(prev); Calendar cal = Calendar.getInstance(); cal.setTime(minDate); cal.add(Calendar.DATE, -1); String min = df.format(cal.getTime()); String su = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22" + sym + ".JK%22%20and%20startDate%20%3D%20%22" + min + "%22%20and%20endDate%20%3D%20%22" + min + "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="; URL urlStream = new URL(su); BufferedReader buff = new BufferedReader( new InputStreamReader(urlStream.openStream())); String b = null; String temp = ""; while (null != (temp = buff.readLine())) { b = temp; } JSONObject jsobj = new JSONObject(b); JSONObject qobj = jsobj.getJSONObject("query"); if (qobj.getInt("count") != 0) { JSONObject res = qobj.getJSONObject("results"); JSONObject qu = res.getJSONObject("quote"); if (qu.getString("Volume").equals("000")) { qcount = 0; } else { exactVolume = qu.getString("Volume"); qcount = qobj.getInt("count"); System.out.println("Dapet: " + qu.getString("Volume")); } } else { qcount = qobj.getInt("count"); } System.out.println(min); prev = min; getDate = min; } while (qcount == 0); System.out.println("This Is Get Date: " + getDate); } else { do { exactVolume = rs.getString("exactvolume"); System.out.println("Exact Volume: " + exactVolume); } while (rs.next()); } } else { exactVolume = volume; } System.out.println(sym + ", " + date + ", " + high + ", " + low + ", " + volume + ", " + exactVolume + ", " + adjClose + ", " + close + ", " + open); con.connectInsert("INSERT INTO STOCKDETAILS VALUES (DEFAULT,'" + sym + "','" + date + "', '" + high + "', '" + low + "', '" + volume + "', '" + exactVolume + "', '" + adjClose + "', '" + close + "', '" + open + "')"); } } do { Calendar c = Calendar.getInstance(); c.setTime(start); c.add(Calendar.DATE, 1); start = c.getTime(); //<----- i++ } while (cd.next()); } System.out.println("Request For (" + sym + ") Sukses"); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: new Login().setVisible(true); this.setVisible(false); }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: try { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); // String d = "2015-11-01"; // Date a = df.parse(d); Date today = df.parse(df.format(date)); Calendar c = Calendar.getInstance(); c.setTime(today); c.add(Calendar.DATE, -1); String prevDate = df.format(c.getTime()); String startDate = dateChooserCombo1.getText(); String endDate = dateChooserCombo2.getText(); Date comStart = df.parse(startDate); Date comEnd = df.parse(endDate); System.out.println(prevDate); checkDateCond(prevDate, today, comStart, comEnd); } catch (ParseException ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { System.out.println("ERROR Bro: " + ex); } }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: try { String getDate = ""; String prev = "2015-11-01"; String exactVolume = ""; for (int i = 0; i == 0;) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date minDate = df.parse(prev); Calendar cal = Calendar.getInstance(); cal.setTime(minDate); cal.add(Calendar.DATE, -1); String min = df.format(cal.getTime()); String su = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22ABMM.JK%22%20and%20startDate%20%3D%20%22" + min + "%22%20and%20endDate%20%3D%20%22" + min + "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="; URL urlStream = new URL(su); BufferedReader buff = new BufferedReader(new InputStreamReader(urlStream.openStream())); String b = null; String temp = ""; while (null != (temp = buff.readLine())) { b = temp; } prev = min; getDate = min; JSONObject jsobj = new JSONObject(b); JSONObject qobj = jsobj.getJSONObject("query"); i = qobj.getInt("count"); } System.out.println("This Is Get Date: " + getDate); String surl = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%2222ABMM.JK%22%20and%20startDate%20%3D%20%22" + getDate + "%22%20and%20endDate%20%3D%20%22" + getDate + "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="; URL urlStream = new URL(surl); BufferedReader buff = new BufferedReader(new InputStreamReader(urlStream.openStream())); JSONObject obj = new JSONObject(buff); JSONObject query = obj.getJSONObject("query"); JSONObject results = query.getJSONObject("results"); JSONObject quote = results.getJSONObject("quote"); exactVolume = quote.getString("Volume"); System.out.println(exactVolume); } catch (ParseException ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); } catch (MalformedURLException ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton3ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { new User().setVisible(true); } catch (Exception ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private datechooser.beans.DateChooserCombo dateChooserCombo1; private datechooser.beans.DateChooserCombo dateChooserCombo2; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; // End of variables declaration//GEN-END:variables }