This tutorial will teach you how to create customer count using java mysql.this program help you find no of customers.
- The user shall be able to register the customer.
- The user shall be able to count the no of customer.
Establish the Database Connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void Connect() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/gsm", "root", ""); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } } |
Add Records
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String name = txtname.getText(); String address = txtaddress.getText(); String phone = txtphone.getText(); try { pst = con.prepareStatement("insert into customer(name,address,phone)values(?,?,?)"); pst.setString(1, name); pst.setString(2, address); pst.setString(3, phone); int k = pst.executeUpdate(); if(k==1) { JOptionPane.showMessageDialog(this, "Record Adddeddd"); txtname.setText(""); txtaddress.setText(""); txtphone.setText(""); txtname.requestFocus(); Customer(); } else { JOptionPane.showMessageDialog(this, "Record Failed"); } } catch (SQLException ex) { ex.printstacktrace(); } } |
Count the No of Customers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public void Customer() { try { pst = con.prepareStatement("SELECT COUNT(*) AS customerCount FROM customer"); ResultSet rs = pst.executeQuery(); while(rs.next()) { int count = rs.getInt("customerCount"); // int count = rs.getInt(1); txtmsg.setText(String.valueOf(count)); } } catch (SQLException ex) { ex.printstacktrace(); } } |