Archive for the ‘MySQL’ Category
JDBC and MySQL: Getting rid of the dreaded message – WARN: Establishing SSL connection without server’s identity verification is not recommended
Written by Chad Darby Friday, 20 May 2016
When connecting to a MySQL database, you may encounter this scary warning message.
Thu Feb 04 14:49:25 IST 2016 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Your app will continue to work fine … it is just the MySQL database yelling at you.
To get rid of the warning message. Append ?useSSL=false to the end of your database connection string.
For example,
Replace – jdbc:mysql://localhost:3306/demo
With – jdbc:mysql://localhost:3306/demo?useSSL=false
Note that I appended ?useSSL=false to the end.
That will get rid of the pesky message … whew!
Posted under How-To, Java, JDBC, MySQL | No Comments
JDBC BLOB: Handling large file sizes
Written by Chad Darby Tuesday, 27 October 2015
JDBC has support for storing binary files in the database using the BLOB datatype. I covered this in a previous blog post: Reading and Writing BLOBs with MySQL.
However, you may run into errors if you need to store large files. In particular, you may encounter the following error messages:
Packets larger than max_allowed_packet are not allowed.
Data too long for column 'xxx' at row 1.
By default, the BLOB datatype can only handle files up to 64KB.
MySQL has multiple datatypes for handling binary data:
TINYBLOB : maximum length of 255 bytes
BLOB : maximum length of 65,535 bytes
MEDIUMBLOB : maximum length of 16,777,215 bytes
LONGBLOB : maximum length of 4,294,967,295 bytes
Storing Larger Files
If you need to store larger files, then follow these steps:
1. Change the databtype of your BLOB column to LONGBLOB
ALTER TABLE `demo`.`employees` CHANGE COLUMN `resume` `resume` LONGBLOB NULL DEFAULT NULL ;
2. Edit the MySQL configuration file
On MS Windows: C:\Program Files\MySQL\MySQL Server x.x\my.ini
On Mac: /usr/local/mysql/my.cnf
In this file, add the line:
max_allowed_packet=256M
See the image below
3. Save the file and restart your MySQL server
4. Test your application again. You will now be able to store large files.
Enjoy 🙂
Posted under How-To, Java, JDBC, MySQL | No Comments
Java JDBC Tutorial – Using MySQL Triggers to keep copy of previous value
Written by Chad Darby Monday, 26 January 2015
I recently had a person ask the following question:
For the employees table, every time I insert a new value in salary column the previous value is overwritten. Can we add new inserted value to the previous salary value before it is overwritten?
Yes, we can accomplish this by using MySQL Triggers. Here’s a quick run of the application so you can see the output.
Employee Salaries Doe John Salary=55000.00 Previous Salary=0.00 Public Mary Salary=75000.00 Previous Salary=0.00 Queue Susan Salary=130000.00 Previous Salary=0.00 Enter new salary for employees: 25000 Employee Salaries Doe John Salary=25000.00 Previous Salary=55000.00 Public Mary Salary=25000.00 Previous Salary=75000.00 Queue Susan Salary=25000.00 Previous Salary=130000.00
MySQL supports triggers. Triggers are similar to stored procedures but they are “triggered” when certain events happen on the database. For example, you can have SQL code that is triggered when data is inserted, updated or deleted. You can read more details about MySQL triggers at this link. But for now, I’ll cover the basics of MySQL triggers to address the question at hand.
We’ll use a trigger to copy the old salary information into a new column for later use. Here’s the database table we’ll use, make note of the new column: previous_salary.
CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `last_name` varchar(64) DEFAULT NULL, `first_name` varchar(64) DEFAULT NULL, `email` varchar(64) DEFAULT NULL, `department` varchar(64) DEFAULT NULL, `salary` DECIMAL(10,2) DEFAULT NULL, `previous_salary` DECIMAL(10,2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Here’s the code for the trigger. In a nutshell, it will copy the old salary information to the new previous_salary column. This is SQL code that you will execute with your MySQL admin tool.
delimiter $$ CREATE TRIGGER salary_trigger BEFORE UPDATE on employees FOR EACH ROW BEGIN set NEW.previous_salary=OLD.salary; END$$
Now that the trigger is in place, whenever we perform an update on the database, then it will execute the trigger. Here’s a Java code snippet where we read a new salary from the user. Then we perform the update in the database:
// // UPDATE SALARIES ... read information from the user // // read new salary from command line scanner = new Scanner(System.in); System.out.print("Enter new salary for employees: "); double newSalary = scanner.nextDouble(); // perform salary update salaryStmt = myConn.prepareStatement("update employees set salary=?"); salaryStmt.setDouble(1, newSalary); salaryStmt.executeUpdate();
That’s it. You can download the full source code, including the SQL scripts, using the link below.
Download Source Code
Posted under How-To, Java, JDBC, MySQL | No Comments