Advertisement
Guest User

Untitled

a guest
Apr 1st, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package db;
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.table.DefaultTableModel;
  11. import javax.swing.table.TableModel;
  12. import javax.swing.*;
  13. /**
  14. *
  15. * @author Edward
  16. */
  17. public class DB5 extends javax.swing.JFrame {
  18.  
  19. /**
  20. * Creates new form DB5
  21. */
  22. public DB5() {
  23. initComponents();
  24. Show_Users_In_JTable();
  25. this.setSize(758,490);
  26. this.setLocationRelativeTo(null);
  27. }
  28. public int getSum(){
  29. int rowCount = JTable1.getColumnCount();
  30. int sum = 0;
  31. for(int i = 0; i<rowCount; i++){
  32. sum = sum+Integer.parseInt(JTable1.getValueAt(i,5).toString());
  33. }
  34. return sum;
  35. }
  36. public Connection getConnection(){
  37. Connection con;
  38. try {
  39. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_project", "root","");
  40. return con;
  41.  
  42. }catch(Exception e){
  43. e.printStackTrace();
  44. return null;
  45. }
  46. }
  47. public ArrayList<Source> getUsersList()
  48. {
  49. ArrayList<Source> usersList = new ArrayList<Source>();
  50. Connection connection = getConnection();
  51.  
  52. String query = "SELECT * FROM tbl_stock";
  53. Statement st;
  54. ResultSet rs;
  55.  
  56.  
  57. try {
  58. st = connection.createStatement();
  59. rs = st.executeQuery(query);
  60. Source source;
  61. while(rs.next())
  62. {
  63. // source=new Source(rs.getString("Date"),rs.getString("Brand"),rs.getString("Model"),rs.getInt("Quantity"),rs.getInt("Price"),rs.getInt("TotalPrice"),rs.getString("Store"));
  64. source = new Source(rs.getInt("ContactNO"),rs.getString("LastName"),rs.getString("FirstName"),rs.getString("Address"),rs.getString("Email"));
  65. usersList.add(source);
  66. }
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. return usersList;
  71. }
  72. public void Show_Users_In_JTable(){
  73. ArrayList<Source> list = getUsersList();
  74. DefaultTableModel model = (DefaultTableModel)JTable1.getModel();
  75. Object[] row = new Object[6];
  76. for(int i = 0; i < list.size(); i++)
  77. {
  78. row[0] = list.get(i).getLastName();
  79. row[1] = list.get(i).getFirstName();
  80. row[2] = list.get(i).getAddress();
  81. row[3] = list.get(i).getContactNO();
  82. row[4] = list.get(i).getEmail();
  83. //row[5] = list.get(i).getTotalPrice();
  84. model.addRow(row);
  85. }
  86. }
  87. public void executeSQlQuery(String query, String message)
  88. {
  89. Connection con = getConnection();
  90. Statement st;
  91. try{
  92. st = con.createStatement();
  93. if((st.executeUpdate(query)) == 1)
  94. {
  95. // refresh jtable data
  96. DefaultTableModel model = (DefaultTableModel)JTable1.getModel();
  97. model.setRowCount(0);
  98. Show_Users_In_JTable();
  99.  
  100. JOptionPane.showMessageDialog(null, "Data "+message+" Succefully");
  101. }else{
  102. JOptionPane.showMessageDialog(null, "Data Not "+message);
  103. }
  104. }catch(Exception ex){
  105. ex.printStackTrace();
  106. }
  107. }
  108.  
  109.  
  110. /**
  111. * This method is called from within the constructor to initialize the form.
  112. * WARNING: Do NOT modify this code. The content of this method is always
  113. * regenerated by the Form Editor.
  114. */
  115. @SuppressWarnings("unchecked")
  116. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  117. private void initComponents() {
  118.  
  119. jScrollPane1 = new javax.swing.JScrollPane();
  120. JTable1 = new javax.swing.JTable();
  121.  
  122. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  123.  
  124. JTable1.setModel(new javax.swing.table.DefaultTableModel(
  125. new Object [][] {
  126.  
  127. },
  128. new String [] {
  129. "Last Name", "First Name", "Address", "Contact No", "Email"
  130. }
  131. ) {
  132. boolean[] canEdit = new boolean [] {
  133. false, false, false, true, false
  134. };
  135.  
  136. public boolean isCellEditable(int rowIndex, int columnIndex) {
  137. return canEdit [columnIndex];
  138. }
  139. });
  140. jScrollPane1.setViewportView(JTable1);
  141.  
  142. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  143. getContentPane().setLayout(layout);
  144. layout.setHorizontalGroup(
  145. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  146. .addGroup(layout.createSequentialGroup()
  147. .addGap(42, 42, 42)
  148. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 698, javax.swing.GroupLayout.PREFERRED_SIZE)
  149. .addContainerGap(36, Short.MAX_VALUE))
  150. );
  151. layout.setVerticalGroup(
  152. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  153. .addGroup(layout.createSequentialGroup()
  154. .addGap(19, 19, 19)
  155. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)
  156. .addContainerGap(28, Short.MAX_VALUE))
  157. );
  158.  
  159. pack();
  160. }// </editor-fold>
  161.  
  162. /**
  163. * @param args the command line arguments
  164. */
  165. public static void main(String args[]) {
  166. /* Set the Nimbus look and feel */
  167. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  168. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  169. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  170. */
  171. try {
  172. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  173. if ("Nimbus".equals(info.getName())) {
  174. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  175. break;
  176. }
  177. }
  178. } catch (ClassNotFoundException ex) {
  179. java.util.logging.Logger.getLogger(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  180. } catch (InstantiationException ex) {
  181. java.util.logging.Logger.getLogger(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  182. } catch (IllegalAccessException ex) {
  183. java.util.logging.Logger.getLogger(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  184. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  185. java.util.logging.Logger.getLogger(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  186. }
  187. //</editor-fold>
  188.  
  189. /* Create and display the form */
  190. java.awt.EventQueue.invokeLater(new Runnable() {
  191. public void run() {
  192. new DB5().setVisible(true);
  193. }
  194. });
  195. }
  196.  
  197. // Variables declaration - do not modify
  198. private javax.swing.JTable JTable1;
  199. private javax.swing.JScrollPane jScrollPane1;
  200. // End of variables declaration
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement