Advertisement
yayachadazt

menggunakan prepastatmant

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