I'm writing a code that creates a user account and sends the result to the user table in a mysql database.
For example, in the user table I have:
username varchar(15) PRIMARY KEY, password varchar (10), is_Admin enum('Y','N'),
In the Java code I have a user class with similair attributes:
private String username; private String password; private boolean isAdmin;
With all the normal accessors, mutators, etc.
My create an account function is like this:
User user = new User (); out.print("\nUsername: "); user.setUsername( input.nextLine() ); out.print("\nPassword: "); user.setPassword( input.nextLine() ); input.nextLine(); out.print("\n(1) Admin or (2) Limited user: "); if ( input.nextInt() == 1 ) user.setIsAdmin( true ); else user.setIsAdmin ( false );
String query = "INSERT INTO user VALUES ('" + user.getUsername() + "', '" + user.getPassword() + "', " + user.getIsAdmin() + ")";
Needless to say this didn't work so I even changed isAdmin to an int and set either a 1 if the user wanted the new account to be an admin and 0 if it's a restricted user. I thought since in the database it would read the 1 as a 'Y' and 0 as a 'N' but I get some database error that says data truncated. How do I set this enum field in the table?
Nvm (not that anyone did), I fixed it.