Reputation: 325
How can i put into code when i want to prompt a message whenever a user creates a new account or updates his/her username and that username already exist on the sql database. here's a part of my code when i click save button on my CreateNewUserAccount class:
String sql = "insert into tblUserInfo (UserID, LastName, FirstName, PositionID, Username, Password) values(?, ?, ?, ?, ?, ?) " ;
try{
pst = conn.prepareStatement(sql);
pst.setString(1, txtUserID.getText());
pst.setString(2, txtLastName.getText());
pst.setString(3, txtFirstName.getText());
pst.setString(4, (String) comboPosition.getSelectedItem().toString().substring(0, 1));
pst.setString(5, txtUsername.getText());
pst.setString(6, txtPassword.getText());
pst.execute();
if(txtConfirmPassword.getText().equals(txtPassword.getText())){
JOptionPane.showMessageDialog(null, "Saved New User Account");
clear();
}
else{
JOptionPane.showMessageDialog(null, "Incompatible Password");
txtConfirmPassword.setText("");
}
//UpdateEmployeeTable();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
and also my UserAccount class where user can update/edit their information including their username:
String sql = "update tblUserInfo set LastName = ?, FirstName = ?, PositionID = ?, Username = ?, Password = ? where UserID= ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtLastName.getText());
pst.setString(2, txtFirstName.getText());
pst.setString(3, (String) comboPos.getSelectedItem().toString().substring(0, 1));
pst.setString(4, txtUserName.getText());
pst.setString(5, txtPassword.getText());
pst.setString(6, txtUserID.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "User Account Updated");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
how can i put a checking on this? Thanks in advance
Upvotes: 1
Views: 4106
Reputation: 460
you can do like that
Statement st=con.getConnection(.....);
String query= "select username from tables where username="uservalue"";
ResultSet rs=st.executeQuery(query);
int value=0;
while(rs.next())
{
value++;
}
if(value>1)
{
String query= "select username from tables where username="uservalue"";
ResultSet rs=st.executeQuery(query);
while(rs.next())
{
String user=rs.getString("username");
}
String updatequery="update tables set username="updatevalue" where username="updatevalue"";
int q=st.executeupdate(updatequery);
}
else
{
insert query..........
}
Upvotes: 0
Reputation: 533690
I would use something like this
IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
http://blogs.msdn.com/b/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx
Upvotes: 2
Reputation: 6499
One option is to do a count query of that username where the user id isn't the same, such as this:
select count(Username) from tblUserInfo where UserId != ?
If you find a result greater than zero, that user id is used by someone else, display the warning.
Upvotes: 1