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 Store; import com.mysql.jdbc.StringUtils; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; import static java.lang.Thread.sleep; import java.util.*; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import org.json.JSONArray; import org.json.JSONObject; /** * * @author AnonymousHacker */ public class SendToDocs extends javax.swing.JFrame { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; /** * Creates new form SendToDocs */ public SendToDocs() { initComponents(); conn = DBConnection.ConnecrDB(); CurrentDateAndTime(); Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Store/icons/H-2-icon.png")); setIconImage(img); } public void CurrentDateAndTime() { Thread clock = new Thread() { public void run() { for (;;) { Calendar cal = new GregorianCalendar(); int yr = cal.get(Calendar.YEAR); int mnth = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); sdate.setText("Date " + yr + "/" + (mnth + 1) + "/" + day); int sec = cal.get(Calendar.SECOND); int min = cal.get(Calendar.MINUTE); int hr = cal.get(Calendar.HOUR); stime.setText("Time " + hr + ":" + min + ":" + sec); try { sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(Signup.class.getName()).log(Level.SEVERE, null, ex); } } } }; clock.start(); } /** * 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() { jToolBar1 = new javax.swing.JToolBar(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); d_no = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); d_txt = new javax.swing.JTextArea(); dsend = new javax.swing.JButton(); dclr = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); txt_dssrc = new javax.swing.JTextField(); dsrc = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); sdate = new javax.swing.JMenu(); stime = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); jToolBar1.setRollover(true); jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Store/icons/lightbox-prev.png"))); // NOI18N jButton1.setToolTipText("Back"); jButton1.setFocusable(false); jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jToolBar1.add(jButton1); jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Store/icons/logout-icon_small.png"))); // NOI18N jButton2.setToolTipText("LOg Out"); jButton2.setFocusable(false); jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jToolBar1.add(jButton2); jLabel1.setText("Message"); d_no.setEditable(false); d_no.setToolTipText("Enter Phone Number,Start +254..."); d_no.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { d_noMouseClicked(evt); } }); d_no.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { d_noKeyTyped(evt); } }); d_txt.setColumns(20); d_txt.setRows(5); d_txt.setText("Type Your Message..."); d_txt.setToolTipText("Enter Phone Number,Start +254.."); d_txt.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { d_txtMouseClicked(evt); } }); jScrollPane1.setViewportView(d_txt); dsend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Store/icons/messages-icon2.png"))); // NOI18N dsend.setText("Send"); dsend.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dsendActionPerformed(evt); } }); dclr.setText("Clear"); dclr.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dclrActionPerformed(evt); } }); jLabel2.setText("Phone Number"); txt_dssrc.setText("Enter Doctor's Number To Search"); dsrc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Store/icons/search-icon.png"))); // NOI18N dsrc.setText("Search"); dsrc.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dsrcActionPerformed(evt); } }); jMenu1.setText("File"); jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Store/icons/logout-icon_small.png"))); // NOI18N jMenuItem1.setText("Log Out"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); sdate.setText("Date"); jMenuBar1.add(sdate); stime.setText("Time"); jMenuBar1.add(stime); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(195, 195, 195) .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(325, 325, 325).addComponent(dsend) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(dclr)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(135, 135, 135) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18)) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addComponent(jLabel2).addGap(32, 32, 32))) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(txt_dssrc, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(d_no, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)) .addGap(40, 40, 40).addComponent(dsrc)) .addComponent(jScrollPane1)))) .addContainerGap(186, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(dsrc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txt_dssrc)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(d_no, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(dsend).addComponent(dclr)) .addGap(69, 69, 69))); setSize(new java.awt.Dimension(829, 535)); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void dsendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dsendActionPerformed // Specify your login credentials String username = "sheldon"; String apiKey = "a96461493f41f452edb727ae33e8fca45fc983a331ce2ea452451f2c3e68976c"; // Specify the numbers that you want to send to in a comma-separated list // Please ensure you include the country code (+254 for Kenya in this case) String recipients = "+254" + d_no.getText(); // And of course we want our recipients to know what we really do //String message = "We are lumberjacks. We code all day and sleep all night"; String message = d_txt.getText(); // Create a new instance of our awesome gateway class AfricasTalkingGateway gateway = new AfricasTalkingGateway(username, apiKey); // Thats it, hit send and we'll take care of the rest. Any errors will // be captured in the Exception class below try { JSONArray results = gateway.sendMessage(recipients, message); for (int i = 0; i < results.length(); ++i) { try { JSONObject result = results.getJSONObject(i); String msgid = result.getString("messageId"); String no = result.getString("number"); String msg = d_txt.getText(); String cost = result.getString("cost"); if (!StringUtils.isNullOrEmpty(msgid) && !StringUtils.isNullOrEmpty(no) && !StringUtils.isNullOrEmpty(msg) && !StringUtils.isNullOrEmpty(cost)) { String sql = "insert into doc_msgs values('" + msgid + "','" + no + "','" + msg + "','" + cost + "')"; pst = conn.prepareStatement(sql); pst.execute(); System.out.print(result.getString("status") + ","); // status is either "Success" or "error message" System.out.print(result.getString("number") + ","); System.out.print(result.getString("messageId") + ","); System.out.println(result.getString("cost")); JOptionPane.showMessageDialog(null, "Message Sent Successfully"); } else { JOptionPane.showMessageDialog(null, "Please,Fill all the values!!!"); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Could not sends the message"); } finally { try { pst.close(); rs.close(); } catch (Exception e) { } } } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Encountered an error while sending " + e.getMessage()); } finally { try { pst.close(); rs.close(); } catch (Exception e) { } } }//GEN-LAST:event_dsendActionPerformed private void dclrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dclrActionPerformed d_no.setText(""); d_txt.setText(""); }//GEN-LAST:event_dclrActionPerformed private void d_noMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d_noMouseClicked // d_no.setText(""); }//GEN-LAST:event_d_noMouseClicked private void d_txtMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d_txtMouseClicked d_txt.setText(""); }//GEN-LAST:event_d_txtMouseClicked private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed new Admin().setVisible(true); dispose(); }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed new Login().setVisible(true); dispose(); JOptionPane.showMessageDialog(null, "You Logged Out"); }//GEN-LAST:event_jButton2ActionPerformed private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed new Login().setVisible(true); dispose(); JOptionPane.showMessageDialog(null, "You Logged Out"); }//GEN-LAST:event_jMenuItem1ActionPerformed private void d_noKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_d_noKeyTyped /*char c=evt.getKeyChar(); if(!(Character.isDigit(c)) || (c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE)){ getToolkit(); evt.consume(); }*/ }//GEN-LAST:event_d_noKeyTyped private void dsrcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dsrcActionPerformed try { String sql = "select * from doctors where Doc_No=?"; pst = conn.prepareStatement(sql); pst.setString(1, txt_dssrc.getText()); rs = pst.executeQuery(); while (rs.next()) { String phone = rs.getString("Phone_Number"); d_no.setText(phone); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }//GEN-LAST:event_dsrcActionPerformed /** * @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(SendToDocs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(SendToDocs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(SendToDocs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(SendToDocs.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() { new SendToDocs().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField d_no; private javax.swing.JTextArea d_txt; private javax.swing.JButton dclr; private javax.swing.JButton dsend; private javax.swing.JButton dsrc; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JMenu jMenu1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JToolBar jToolBar1; private javax.swing.JMenu sdate; private javax.swing.JMenu stime; private javax.swing.JTextField txt_dssrc; // End of variables declaration//GEN-END:variables }