Imported Libraries are:
- import java.sql.Connection;
- import javax.swing.JOptionPane;
- import java.sql.*;
- import java.sql.Statement;
- import java.sql.PreparedStatement;
Validating Sign up Forms:
String fname,lname,uname,password,cpassword,email,contact,account; int len , lenno; boolean validation(){ fname = txt_firstname.getText(); lname = txt_lastname.getText(); uname = txt_username.getText(); password = txt_password.getText(); cpassword = txt_confirm_password.getText(); email = txt_email.getText(); contact = txt_phoneno.getText(); account = (String) txt_combo.getSelectedItem(); len = password.length(); lenno = contact.length(); if(fname.equals("")) { JOptionPane.showMessageDialog(null, "First Name cannot be null"); return false; } if(lname.equals("")) { JOptionPane.showMessageDialog(null, "Last Name cannot be null"); return false; } if(uname.equals("")) { JOptionPane.showMessageDialog(null, "User Name cannot be null"); return false; } if(password .equals("") | len < 4) { JOptionPane.showMessageDialog(null, "Password Enter Correct password"); return false; } if(!cpassword.equals(password)){ JOptionPane.showMessageDialog(null, "Password cannot be Match"); return false; } if(email .equals("")) { JOptionPane.showMessageDialog(null, "Email cannot be null"); return false; } if(contact .equals("") | lenno != 11) { JOptionPane.showMessageDialog(null, "Please enter correct number"); return false; } return true; }
Increment id function:
int id =0; public int getid() { ResultSet rs = null; try{ Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root","root"); String query ="select max(id) from signup"; Statement st = con.createStatement(); rs = st.executeQuery(query); while(rs.next()){ id=rs.getInt(1); id++; } } catch(Exception e){ e.printStackTrace(); } return id; }
Insert Values In DataBase :
void connectdatabase() { try{ //Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root","root"); String query ="Insert into signup values(?,?,?,?,?,?,?,?)"; PreparedStatement pst = con.prepareStatement(query); pst.setInt(1, getid()); pst.setString(2, fname); pst.setString(3, lname); pst.setString(4, uname); pst.setString(5, password ); pst.setString(6, email); pst.setString(7,contact); pst.setString(8,account); int i = pst.executeUpdate(); if(i > 0){ JOptionPane.showMessageDialog(null,"Account successfully created"); } else { JOptionPane.showMessageDialog(null,"Account creation Failed"); } } catch( Exception e){ e.printStackTrace(); } }
0 Comments