Advertisement
yayachadazt

coba

Sep 18th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. /*
  7. * NewJFrame2.java
  8. *
  9. * Created on Sep 13, 2014, 11:45:53 PM
  10. */
  11. package koneksi;
  12.  
  13. import aplikasi.yaya.frame.*;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.text.DecimalFormat;
  20. import java.text.DecimalFormatSymbols;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23.  
  24. /**
  25. *
  26. * @author AINUNJARIAH
  27. */
  28. public class NewJFrame2 extends javax.swing.JFrame {
  29. ResultSet res = null;
  30. Connection con = null;
  31. PreparedStatement pstmt = null;
  32. /** Creates new form NewJFrame2 */
  33. public NewJFrame2() throws ClassNotFoundException, SQLException {
  34. initComponents();
  35. koneksi();
  36. }
  37. private void koneksi(){
  38. try {
  39. String driver = "com.mysql.jdbc.Driver";
  40. String url = "jdbc:mysql://localhost/apmasjid";
  41. String username = "root";
  42. String password = "";
  43. Class.forName(driver);
  44. con = DriverManager.getConnection(url, username, password);
  45. System.out.println("Konekkk");
  46.  
  47. } catch (Exception e) {
  48. System.out.println("gagal koneksi");
  49. }
  50. }
  51. /** This method is called from within the constructor to
  52. * initialize the form.
  53. * WARNING: Do NOT modify this code. The content of this method is
  54. * always regenerated by the Form Editor.
  55. */
  56. @SuppressWarnings("unchecked")
  57. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  58. private void initComponents() {
  59.  
  60. jButton1 = new javax.swing.JButton();
  61.  
  62. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  63.  
  64. jButton1.setText("jButton1");
  65. jButton1.addActionListener(new java.awt.event.ActionListener() {
  66. public void actionPerformed(java.awt.event.ActionEvent evt) {
  67. jButton1ActionPerformed(evt);
  68. }
  69. });
  70.  
  71. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  72. getContentPane().setLayout(layout);
  73. layout.setHorizontalGroup(
  74. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75. .addGroup(layout.createSequentialGroup()
  76. .addGap(105, 105, 105)
  77. .addComponent(jButton1)
  78. .addContainerGap(222, Short.MAX_VALUE))
  79. );
  80. layout.setVerticalGroup(
  81. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82. .addGroup(layout.createSequentialGroup()
  83. .addGap(88, 88, 88)
  84. .addComponent(jButton1)
  85. .addContainerGap(189, Short.MAX_VALUE))
  86. );
  87.  
  88. pack();
  89. }// </editor-fold>
  90.  
  91. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  92. try {
  93. // query untuk membaca SMS yang belum diproses
  94. String sql = "SELECT * FROM inbox WHERE Processed ='false'";
  95. PreparedStatement pstat = con.prepareStatement(sql); // create a statement
  96. res=pstat.executeQuery();
  97. while (res.next())
  98. {
  99. // membaca ID SMS
  100. String id2 = res.getString("ID");
  101. // membaca no pengirim
  102. String noPengirim = res.getString("SenderNumber");
  103. // membaca pesan SMS dan mengubahnya menjadi kapital
  104. String msg1 = res.getString("TextDecoded");
  105. String msg=msg1.toUpperCase();
  106. // proses parsing
  107. // memecah pesan berdasarkan karakter <spasi>
  108. String []pecah = msg.split(" ");
  109. // jika kata terdepan dari SMS adalah 'saldo' maka cari saldo
  110. try {
  111. if ("CEK".equals(pecah[0])&&"SALDO".equals(pecah[1]))
  112. {
  113.  
  114. String query2 = "SELECT * FROM saldomasjid WHERE idmasjid ='12345'";
  115. PreparedStatement hasil2=con.prepareStatement(query2);
  116. res=hasil2.executeQuery();
  117. while (res.next()) {
  118. int saldo=res.getInt("saldo");
  119.  
  120. DecimalFormat df = (DecimalFormat) DecimalFormat.getCurrencyInstance();
  121. DecimalFormatSymbols dfs = new DecimalFormatSymbols();
  122. dfs.setCurrencySymbol("");
  123. dfs.setMonetaryDecimalSeparator(',');
  124. dfs.setGroupingSeparator('.');
  125. df.setDecimalFormatSymbols(dfs);
  126. String hsl = "Rp. " + df.format(saldo);
  127. System.out.println(hsl);
  128. String reply = "Jumlah Saldo Masjid Adalah "+hsl+"";
  129. System.out.println(msg);
  130. String query3 = "INSERT INTO outbox(DestinationNumber, TextDecoded,CreatorID) VALUES ('"+noPengirim+"', '"+reply+"','Gammu')";
  131. PreparedStatement hasil3=con.prepareStatement(query3);
  132. hasil3.executeUpdate();
  133. // ubah saldo 'processed' menjadi 'true' untuk setiap SMS yang telah diproses
  134.  
  135. String query4 = "UPDATE inbox SET Processed ='true' WHERE ID='"+id2+"'";
  136. PreparedStatement hasil4=con.prepareStatement(query4);
  137. hasil4.executeUpdate();
  138. }
  139.  
  140.  
  141.  
  142.  
  143. }else {
  144. String reply = "Maaf perintah salah, Cek FORMAT SMS ANDA : SALDO<spasi>ID MASJID";
  145.  
  146. // membuat SMS balasan
  147.  
  148. String query3 = "INSERT INTO outbox(DestinationNumber, TextDecoded,CreatorID) VALUES ('"+noPengirim+"', '"+reply+"','Gammu')";
  149. PreparedStatement hasil3=con.prepareStatement(query3);
  150. hasil3.executeUpdate();
  151. // ubah saldo 'processed' menjadi 'true' untuk setiap SMS yang telah diproses
  152.  
  153. String query4 = "UPDATE inbox SET Processed ='true' WHERE ID='"+id2+"'";
  154. PreparedStatement hasil4=con.prepareStatement(query4);
  155. hasil4.executeUpdate();
  156. }
  157. } catch (Exception e) {
  158. System.out.println("eor");
  159. }
  160.  
  161. }
  162. System.out.println("suksess");
  163. } catch (SQLException e) {
  164. e.printStackTrace();
  165. }
  166.  
  167.  
  168.  
  169. }
  170.  
  171. public void t(){
  172. try {
  173. String sqlsaldo = "SELECT * FROM saldomasjid WHERE idmasjid ='123456'";
  174. PreparedStatement pstat2 = con.prepareStatement(sqlsaldo); // create a statement
  175. res=pstat2.executeQuery();
  176. while (res.next()) {
  177. String saldo=res.getString("saldo");
  178. String reply="asdasdas '"+saldo+"'";
  179. }
  180. } catch (SQLException ex) {
  181. Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
  182. }
  183. }
  184. /**
  185. * @param args the command line arguments
  186. */
  187. public static void main(String args[]) {
  188. /* Set the Nimbus look and feel */
  189. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  190. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  191. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  192. */
  193. try {
  194. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  195. if ("Nimbus".equals(info.getName())) {
  196. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  197. break;
  198. }
  199. }
  200. } catch (ClassNotFoundException ex) {
  201. java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  202. } catch (InstantiationException ex) {
  203. java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  204. } catch (IllegalAccessException ex) {
  205. java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  206. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  207. java.util.logging.Logger.getLogger(NewJFrame2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  208. }
  209. //</editor-fold>
  210.  
  211. /* Create and display the form */
  212. java.awt.EventQueue.invokeLater(new Runnable() {
  213.  
  214. public void run() {
  215. try {
  216. new NewJFrame2().setVisible(true);
  217. } catch (ClassNotFoundException ex) {
  218. Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
  219. } catch (SQLException ex) {
  220. Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
  221. }
  222. }
  223. });
  224. }
  225. // Variables declaration - do not modify
  226. private javax.swing.JButton jButton1;
  227. // End of variables declaration
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement