I'm using javax.persistence.EntityManager in a programme. So I've a user table which use String username as the primary key in a mysql database. So when i find a value from a username i found a problem.
Sample Data :
username : MobileUser001 username : MobileUser002
I used public <T> T find(Class<T> entityClass, Object primaryKey);
So when i send "mobileuser001" , "MobileUser001" , "mobileUser001" as the primary key Persistence searched the correct data. But it gives the username what we input, Not the username with correct case in the database.
I need the correct case "MobileUser001" for any input like i mentioned above.
Related
I have Cassandra Table as below
create table user(
id UUID PRIMARY KEY,
firstname varchar,
secondname varchar,
emailid varchar,
);
From Java - Spring Boot Im trying to access data
Optional<User> findByEmailid(String emailId);
I get error stating
It asks me to use "FILTERING ALLOWED" part of query. Is there anyway to enable this globally or I should change query/db structure?
That error is telling you that emailid is not a valid column that can be filtered on in your WHERE clause. Enabling ALLOW FILTERING or creating a secondary index on that column is one way to do this. But both of those are pretty terrible solutions (because of how Cassandra is works under-the-hood).
With Cassandra you need to take a query-based modeling approach. This means that sometimes (often) queries and tables share a 1:1 ratio. If you really need to query users by email address, then you will need to create a table to serve that query.
CREATE TABLE user_by_email(
id UUID,
firstname varchar,
secondname varchar,
emailid varchar PRIMARY KEY,
);
Then something like this will work:
Optional<UserByEmail> findByEmailid(String emailId);
And if you don't ever plan on querying the user table by id, then there really isn't a reason to use that column as your sole primary key.
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.
so I'd like to know if it's possible to retrieve a key instead of a value from a properties object. I researched it but only came up with examples that list all of the keys. Is there a way to retrieve the key without having to use a loop to filter through all of them to get what I need?
Basically what I have is a login that stores the login info in a properties file where the user name is the key and the value is the password
pass = propertiesObj.getProperty(username);
How would I retrieve the username (the key) from the properties file so I can test if that is the same as what the user entered? I'm just looking for ideas on how to accomplish this, the properties object doesn't have anything to retrieve a key based off a value.
Thanks!
Given the use case you describe I can see no application for retrieving the username from looking it up by password.
The username as entered by the user will not return a password (or rather null) as long as there is not a key with that username. If however the entered username (the key) returns a password which is the same as the password entered by the user, there is a match and you can be sure the key is the same as what the user entered.
Looking up whether a username (key) exists is easy:
String username = "heresjohnny";
boolean isValidUsername = props.keySet().contains(username);
Properties extends Hashtable. Use the keySet method to get the keys.
If you want to fetch keys for values, then create a new HashMap with the keys and values swapped - by iterating through the keys, pulling out the values and add them to the new HashMap.
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.
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