To prevent a memory leak, the JDBC Driver has been forcibly unregistered

Since version 6.0.24, Tomcat ships with a memory leak detection feature, which in turn can lead to this kind of warning messages when there’s a JDBC 4.0 compatible driver in the webapp’s /WEB-INF/lib which auto-registers itself during webapp’s startup using the ServiceLoader API, but which did not auto-deregister itself during webapp’s shutdown. This message is … Read more

How to establish a connection pool in JDBC?

If you need a standalone connection pool, my preference goes to C3P0 over DBCP (that I’ve mentioned in this previous answer), I just had too much problems with DBCP under heavy load. Using C3P0 is dead simple. From the documentation: ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( “org.postgresql.Driver” ); //loads the jdbc driver cpds.setJdbcUrl( “jdbc:postgresql://localhost/testdb” ); … Read more

Difference between Statement and PreparedStatement

Advantages of a PreparedStatement: Precompilation and DB-side caching of the SQL statement leads to overall faster execution and the ability to reuse the same SQL statement in batches. Automatic prevention of SQL injection attacks by builtin escaping of quotes and other special characters. Note that this requires that you use any of the PreparedStatement setXxx() … Read more

How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception

As for every “3rd-party” library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp’s /WEB-INF/lib. It will then be available in webapp’s default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to … Read more

JDBC vs Web Service for Android

You think it’s simpler and faster to do it with JDBC because you aren’t considering the real world operating environment of phones and portable devices. They often have flakey connectivity through buggy traffic rewriting proxies and insane firewalls. They’re typically using a network transport layer that has high and variable packet loss rates and latencies … Read more

How to get the insert ID in JDBC?

If it is an auto generated key, then you can use Statement#getGeneratedKeys() for this. You need to call it on the same Statement as the one being used for the INSERT. You first need to create the statement using Statement.RETURN_GENERATED_KEYS to notify the JDBC driver to return the keys. Here’s a basic example: public void … Read more

The infamous java.sql.SQLException: No suitable driver found

The infamous java.sql.SQLException: No suitable driver found This exception can have basically two causes: #1. JDBC driver is not loaded You need to ensure that the JDBC driver is placed in server’s own /lib folder. Or, when you’re actually not using a server-managed connection pool data source, but are manually fiddling around with DriverManager#getConnection() in … Read more

Solving a “communications link failure” with JDBC and MySQL [duplicate]

I have had the same problem in two of my programs. My error was this: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. I spent several days to solve this problem. I have tested many approaches that … Read more