I am creating a JFrameApplet (In Java) with a log in (SQLite) but I am struggling to understand how to compare a wanted username, against a username that is already taken:
For Example: I want the username JoeBloggs, but it is taken, how do I compare a wanted username against one already made.
I have an SQLite users.db and the field is USERNAME.
Thank you for any assistance.
Try searching up the UNIQUE constraint.
When creating a table useCREATE TABLE Users(Id INTEGER, USERNAME TEXT UNIQUE);
so if JoeBloggs is already an entry in the db trying to add it again with INSERT INTO Users VALUES(2, 'JoeBloggs'); will give you an Error: column USERNAME is not unique.
Related
I am currently working on register/login GUI window and l am trying to store passwords and usernames in MySQL DB. I also want to check if a user with such password exists but l don't know how to get just 1 password and 1 user. I want to save from each row the username and the password inside a HashMap (username -> key| password -> value)
This is how my DB looks like.
This is what rows l want to get in the Map
How can l set each username and password into a HashMap?
I only can think of this:
SELECT username, password
FROM table
I can't continue from here...
Your usernames should be unique, so to retrieve one pair of username and its corresponding password, you can limit your query by adding WHERE username= and append whatever username for which you want to retrieve the username/password pair. After that, storing the fields of the result set in a HashMap is pretty trivial.
I want to check userid and password from database. Is it necessary to store id and password to string first? I am trying to check it by resultset.first() method as my table has only one row but it is not working.
If you have only one row, you can either hash or encrypt the userid-password combination.
When user enter the user credentials, hash those details and search in db if its exist or not.
If its exist, it will return that row or if its not exist, it will return null.
I am trying to create a server for POP3 protocol. I'm relatively new to mySql and would like to create a simple database for storing users (username and password) and their corresponding emails(some text, not email-ID).
Example: let's say user1 has username1 and password1. I would like to create another table which points to the user1 and contains his emails there.
My main purpose is to run a query using Java and access users (using their username and password) and their emails.
How can I do this?
Take a look at this answer to know how to store passwords in database.
You can have your design something like this:
users
- id int PK
- username varchar unique not null
- passwordhash varchar not null
user_emails
- id int PK
- user_id int not null FK -> users(id)
- email varchar not null
I am not going to write down DDL for you. Check out vendor documentation for create table syntax.
I save an UUID token in a mysql databse table. In next section of my java program, I need to do a query like this
String sql = "SELECT expiray_time FROM recover_password WHERE token = '"+token+"'";
this token is an UUID value generated before and it is saved in a mysql table.
But when I run my program I get an error saying EmptyResultDataAccessException. I think I get this error because the UUID token value is not in the same form which was there when generating and inserting in to the database. It is in a different form now.
I have saved UUID in my databse as a text type value.
My questions are
Is it correct to save UUID values in the type of text in the database?
How to compare the generated token value with the token value in the database?
Are UUID values get hashed or encrypted in to a different format when getting inserted into a database?
Thank you !
If your UUID won't exceed 255 characters, then it's most preferable to use VARCHAR.
Use MySQL's STRCMP() function for string comparison.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#function_strcmp
I want to create a way to authenticate users. In mysql I do:
SELECT user_id FROM customers WHERE `username` = 'the_username' AND `password` = 'the_password'
If the query doesn't return an empty resultset then user is authorized. Cassandra expects me to know the row key (user_id) upfront. How can I check all rows in a column family for existence of a username and password where I don't know the row key.
I am using Hector api to connect to Cassandra 1.2.2
Some possible solutions:
use the username as row key
create a secondary index on username
I would use the first one. If username is unique (as I assume), you don't need user_id