Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * NewJFrame2.java
- *
- * Created on Sep 13, 2014, 11:45:53 PM
- */
- package koneksi;
- import aplikasi.yaya.frame.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.DecimalFormat;
- import java.text.DecimalFormatSymbols;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author AINUNJARIAH
- */
- public class NewJFrame2 extends javax.swing.JFrame {
- ResultSet res = null;
- Connection con = null;
- PreparedStatement pstmt = null;
- /** Creates new form NewJFrame2 */
- public NewJFrame2() throws ClassNotFoundException, SQLException {
- initComponents();
- koneksi();
- }
- private void koneksi(){
- try {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost/apmasjid";
- String username = "root";
- String password = "";
- Class.forName(driver);
- con = DriverManager.getConnection(url, username, password);
- System.out.println("Konekkk");
- } catch (Exception e) {
- System.out.println("gagal koneksi");
- }
- }
- /** 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">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jButton1.setText("jButton1");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(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()
- .addGap(105, 105, 105)
- .addComponent(jButton1)
- .addContainerGap(222, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(88, 88, 88)
- .addComponent(jButton1)
- .addContainerGap(189, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- // query untuk membaca SMS yang belum diproses
- String sql = "SELECT * FROM inbox WHERE Processed ='false'";
- PreparedStatement pstat = con.prepareStatement(sql); // create a statement
- res=pstat.executeQuery();
- while (res.next())
- {
- // membaca ID SMS
- String id2 = res.getString("ID");
- // membaca no pengirim
- String noPengirim = res.getString("SenderNumber");
- // membaca pesan SMS dan mengubahnya menjadi kapital
- String msg1 = res.getString("TextDecoded");
- String msg=msg1.toUpperCase();
- // proses parsing
- // memecah pesan berdasarkan karakter <spasi>
- String []pecah = msg.split(" ");
- // jika kata terdepan dari SMS adalah 'saldo' maka cari saldo
- try {
- if ("CEK".equals(pecah[0])&&"SALDO".equals(pecah[1]))
- {
- String query2 = "SELECT * FROM saldomasjid WHERE idmasjid ='12345'";
- PreparedStatement hasil2=con.prepareStatement(query2);
- res=hasil2.executeQuery();
- while (res.next()) {
- int saldo=res.getInt("saldo");
- DecimalFormat df = (DecimalFormat) DecimalFormat.getCurrencyInstance();
- DecimalFormatSymbols dfs = new DecimalFormatSymbols();
- dfs.setCurrencySymbol("");
- dfs.setMonetaryDecimalSeparator(',');
- dfs.setGroupingSeparator('.');
- df.setDecimalFormatSymbols(dfs);
- String hsl = "Rp. " + df.format(saldo);
- System.out.println(hsl);
- String reply = "Jumlah Saldo Masjid Adalah "+hsl+"";
- System.out.println(msg);
- String query3 = "INSERT INTO outbox(DestinationNumber, TextDecoded,CreatorID) VALUES ('"+noPengirim+"', '"+reply+"','Gammu')";
- PreparedStatement hasil3=con.prepareStatement(query3);
- hasil3.executeUpdate();
- // ubah saldo 'processed' menjadi 'true' untuk setiap SMS yang telah diproses
- String query4 = "UPDATE inbox SET Processed ='true' WHERE ID='"+id2+"'";
- PreparedStatement hasil4=con.prepareStatement(query4);
- hasil4.executeUpdate();
- }
- }else {
- String reply = "Maaf perintah salah, Cek FORMAT SMS ANDA : SALDO<spasi>ID MASJID";
- // membuat SMS balasan
- String query3 = "INSERT INTO outbox(DestinationNumber, TextDecoded,CreatorID) VALUES ('"+noPengirim+"', '"+reply+"','Gammu')";
- PreparedStatement hasil3=con.prepareStatement(query3);
- hasil3.executeUpdate();
- // ubah saldo 'processed' menjadi 'true' untuk setiap SMS yang telah diproses
- String query4 = "UPDATE inbox SET Processed ='true' WHERE ID='"+id2+"'";
- PreparedStatement hasil4=con.prepareStatement(query4);
- hasil4.executeUpdate();
- }
- } catch (Exception e) {
- System.out.println("eor");
- }
- }
- System.out.println("suksess");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void t(){
- try {
- String sqlsaldo = "SELECT * FROM saldomasjid WHERE idmasjid ='123456'";
- PreparedStatement pstat2 = con.prepareStatement(sqlsaldo); // create a statement
- res=pstat2.executeQuery();
- while (res.next()) {
- String saldo=res.getString("saldo");
- String reply="asdasdas '"+saldo+"'";
- }
- } catch (SQLException ex) {
- Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * @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(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame2.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 NewJFrame2().setVisible(true);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement