Retrieve a Key from a properties file JAVA TOMCAT - java

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.

Related

how to get data from MySQL DB and set it in HashMap in Java (register/login GUI program)

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.

How can i check id and password from database

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.

How to compare SQLite Column against a user string?

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.

unable to match password field in java with oracle database

I am trying to build a simple login form using Swing in Java. I created a Sample form with two fields.
usernamefield of type TextField
passfield of type PasswordField
Now I have a database and in that a login table which has following structure.
username | password
----------------------
abcd | xyz
Also I created a connection to database. and I am able to access table data by using ResultSet.
I made an object of database connection called conn.
I know that password is stored in the form of char array.
so when I try to match password by using following code it does not work.
if(usernamefield.getText() == conn.username && passfield.getPassword().toString == conn.password) {
system.out.println("Correct");
}else {
system.out.println("Incorrect");
}
Above code always go to else block.
I also noticed that passfield.getPassword() prints the correct password while passfield.getPassword().toString prints some random characters like [C#76dab03c
How to resolve it?
Use equals instead of == and it should work. Never use == on strings, it hardly ever produces the result you expect.
I think the more serious issue with your code is that you're storing passwords in plain text. That is a huge security risk. You should look into password hashing algorithms, for instance bcrypt.
You must compare string with .equals(), not ==, but you're doing this wrong anyway. The password should be hashed at the database, and you should be passing what the user entered to the database and having it do the matching, via a WHERE clause using the appropriate hash function. In other words, ask the database to fetch the user row with this username and password.

Duplicate entries with Google datastore

I am using Google App Engine with the Datastore interface.
Whenever I'm trying to update an entity, a whole new entity is created, this is despite the fact that I'm positive I am saving the same entity, meaning it has the same key for sure.
This is my code:
Key key=KeyFactory.createKey("user",Long.parseLong(ID));
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity=new Entity("user",key);
entity.setProperty // ...whatever, updating the properties
datastore.put(entity); //by putting an entity it's supposed to
// either create a new one if non exists, or update an entity if it already exists
I am sure that the key is the same during all updates as is confirmed in my admin console:
id=3001 600643316
id=3002 600643316
id=3003 600643316
a bunch of entities with the same key (600643316) is created.
The datastore only lets the app create a new entity with a String key name, not a numeric ID. Numeric IDs are system-assigned IDs. If the Key has a numeric ID but not a String key name, then the datastore will ignore it and replace it with a system-assigned numeric ID.
In your example, if ID is a string, then you can just remove the Long.parseLong() bit, or convert it back to a String. KeyFactory.createKey(String kind, String name) creates a Key with a key name.
So it seems Dan is correct and this is the correct way to do it , as explained in google's guides if you want your app to build keys from unique keys that you create you need to use strings .
"You specify whether an entity ought to use an app-assigned key name string or a system-assigned numeric ID as its identifier when you create the object. To set a key name, provide it as the second argument to the Entity constructor:
Entity employee = new Entity("Employee","asalieri");" It seems you're correct , in their example the second argument is indeed a string – user1032663

Categories