Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 db;
- import java.sql.*;
- import java.util.ArrayList;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.TableModel;
- import javax.swing.*;
- /**
- *
- * @author Edward
- */
- public class DB5 extends javax.swing.JFrame {
- /**
- * Creates new form DB5
- */
- public DB5() {
- initComponents();
- Show_Users_In_JTable();
- this.setSize(758,490);
- this.setLocationRelativeTo(null);
- }
- public int getSum(){
- int rowCount = JTable1.getColumnCount();
- int sum = 0;
- for(int i = 0; i<rowCount; i++){
- sum = sum+Integer.parseInt(JTable1.getValueAt(i,5).toString());
- }
- return sum;
- }
- public Connection getConnection(){
- Connection con;
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_project", "root","");
- return con;
- }catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
- public ArrayList<Source> getUsersList()
- {
- ArrayList<Source> usersList = new ArrayList<Source>();
- Connection connection = getConnection();
- String query = "SELECT * FROM tbl_stock";
- Statement st;
- ResultSet rs;
- try {
- st = connection.createStatement();
- rs = st.executeQuery(query);
- Source source;
- while(rs.next())
- {
- // source=new Source(rs.getString("Date"),rs.getString("Brand"),rs.getString("Model"),rs.getInt("Quantity"),rs.getInt("Price"),rs.getInt("TotalPrice"),rs.getString("Store"));
- source = new Source(rs.getInt("ContactNO"),rs.getString("LastName"),rs.getString("FirstName"),rs.getString("Address"),rs.getString("Email"));
- usersList.add(source);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return usersList;
- }
- public void Show_Users_In_JTable(){
- ArrayList<Source> list = getUsersList();
- DefaultTableModel model = (DefaultTableModel)JTable1.getModel();
- Object[] row = new Object[6];
- for(int i = 0; i < list.size(); i++)
- {
- row[0] = list.get(i).getLastName();
- row[1] = list.get(i).getFirstName();
- row[2] = list.get(i).getAddress();
- row[3] = list.get(i).getContactNO();
- row[4] = list.get(i).getEmail();
- //row[5] = list.get(i).getTotalPrice();
- model.addRow(row);
- }
- }
- public void executeSQlQuery(String query, String message)
- {
- Connection con = getConnection();
- Statement st;
- try{
- st = con.createStatement();
- if((st.executeUpdate(query)) == 1)
- {
- // refresh jtable data
- DefaultTableModel model = (DefaultTableModel)JTable1.getModel();
- model.setRowCount(0);
- Show_Users_In_JTable();
- JOptionPane.showMessageDialog(null, "Data "+message+" Succefully");
- }else{
- JOptionPane.showMessageDialog(null, "Data Not "+message);
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- /**
- * 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() {
- jScrollPane1 = new javax.swing.JScrollPane();
- JTable1 = new javax.swing.JTable();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- JTable1.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- },
- new String [] {
- "Last Name", "First Name", "Address", "Contact No", "Email"
- }
- ) {
- boolean[] canEdit = new boolean [] {
- false, false, false, true, false
- };
- public boolean isCellEditable(int rowIndex, int columnIndex) {
- return canEdit [columnIndex];
- }
- });
- jScrollPane1.setViewportView(JTable1);
- 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(42, 42, 42)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 698, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(36, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(19, 19, 19)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(28, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- /**
- * @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(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(DB5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(DB5.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 DB5().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JTable JTable1;
- private javax.swing.JScrollPane jScrollPane1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement