// TODO add your handling code here:
PreparedStatement st = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xaramat","root","");
Statement state = con.createStatement();
Statement stat = con.createStatement();
String Username=Username_txt.getText();
String Password=Password_txt.getText();
String Role=this.Role.getSelectedItem().toString();
String sql2="SELECT * FROM admin WHERE Username = '" + Username + "'";
PreparedStatement pstmt = con.prepareStatement(sql2);
ResultSet res=pstmt.executeQuery(sql2);
if(res.next()){
String sql="INSERT INTO admin(Username,Password,Type)"
+ "VALUES ('"+Username+"','"+Password+"','"+Role+"') ";
state.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Account has been added Succsesfully!");
Username_txt.setText("");
Password_txt.setText("");
this.Role.setSelectedIndex(0);
}
} catch (Exception e) {
System.out.println(e);
}
I want to check if the username is taken. The username field is unique and if it's not taken then I want to insert it.