How does UUID values get saved in a mysql database - java

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

Related

android how to check in java before saving record the string value already exist in mysql data base

I have 6 string value but I want to check one string for the record, the value before the record in MySQL database.
I have a dot, where I want to check in java is PHP.
$billno = $_POST["billno"];
$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$customer_number = $_POST["customer_number"];
$remark = $_POST["remark"];
$amount = $_POST["amount"];
I want to insert data and also want to check customer_number already exist.
Set the customer number column to unique in Mysql DB. It will give an error in response while trying to insert duplicate customer number into db.

Setting DB2SECURITYLABEL upon insert using Hibernate

I have a requirement to set the DB2SECURITYLABEL of a row upon inserting that row into the database via Hibernate. I've been unsuccessful thus far and I'm seeking advice on how to set the DB2SECURITYLABEL in Java and have Hibernate do the insert for me.
Inside of my entity, I have defined the following:
#Column(name="slabel") //this matches the name of the DB2SECURITYLABEL column
private byte[] slabel;
public void setSlabel(byte[] slabel){
this.slabel = slabel;
}
public byte[] getSlabel(){
return this.slabel;
}
In my controller I convert the hex string that represents my security label into a byte[], setSlabel with that byte[], and save the record through a Spring Data Repository .save(entity). Upon saving that record, the db2 driver throws an error:
SQLCODE=-20402 SQLSTATE=42519 .
This error code is indicative of a record being saved without a required DB2SECURITYLABEL.
Assumptions:
I am able to insert/select into/from this table via commandline using the same
security label. insert into schema.table (field1, slabel) values
(123,x'FFF01010101000CABBBBFFF') <-not my real label. I can select the records that I insert via commandline and in my application.
I am able to retrieve existing records with DB2SECURITYLABELS
and read the label as a byte[] and as hex string using my application.
I believe I am inserting a correct label. I have verified the byte[]
that I am applying to the object is correct. I was able to insert a
record with a valid DB2SECURITYLABEL via the commandline, and
retrieved it in my application; the two byte[] for the
DB2SECURITYLABELs are identical and convert back to the same hex
string.
Has anyone set a DB2SECURITYLABEL in a Java object as a byte[] and used Hibernate to save the data to a db2 database?
I appreciate any assistance.

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.

Getting rid of binary code from string before inserting row in MySQL database

I am fetching tweets from Twitter and storing them in a database for future use. I am using UTF-8 encoding in my driver, utf8_mb4_bin in my VARCHAR fields and utf8mb4_general_ciserver collation. The problem with that is that when inserting a value in a VARCHAR field, if the text has any binary code then it will throw an exception since VARCHAR utf8 does not accept binary.
Here is an example, I am fetching the text from here and try inserting it in my database and I get the error:
Incorrect string value: '\xF0\x9F\x98\xB1\xF0\x9F...' for column 'fullTweet' at row 1
My guess is that the two emoticons are causing this. How do I get rid of them before inserting the tweet text in my database?
Update:
Looks like I can manually enter the emoticons. I run this query:
INSERT INTO `tweets`(`id`, `createdAt`, `screenName`, `fullTweet`, `editedTweet`) VALUES (450,"1994-12-19","john",_utf8mb4 x'F09F98B1',_utf8mb4 x'F09F98B1')
and this is what the row in the table looks like:
You can remove non ascii characters from tweet string before inserting.
tweetStr = tweetStr.replaceAll("[^\\p{ASCII}]", "");
It looks like utf8mb4 support is still not configured correctly.
In order to use utf8mb4 in your fields you need to do the following:
Set character-set-server=utf8mb4 in your my.ini or my.cnf. Only character-set-server really matters here, other settings don't.
Add characterEncoding=UTF-8 to connection URL:
jdbc:mysql://localhost:3306/db?characterEncoding=UTF-8
Configure collation of the field

Blob field in MySQL DB

I have a public key value that I want to store it in DB. I stored it as Blob. The Java concerned lines are:
byte[] pk = ((BigInteger) Modulus ).toByteArray();
and
preparedStmt.setBytes(1, pk);
When I open MySQL DB in the workbench GUI, I don't see the value itself as bytes. Rather, I see the following:
Not sure if this is how it should appear or there is something missing.

Categories