Try with resources statement in web app

Background

Trying to write a java try with resources statement Since sqlserver is used, mssql-jdbc-7.0.0.jre8.jar was imported.

At this time, the following code becomes an error

dbConnect.java


try(Connection connection = DriverManager.getConnection(url)) {
}

When I wrote, I got an error saying "No suitable driver found for jdbc: sqlserver: // ~".

When I checked the error, I was asked to specify the driver with Class.forName, so Modified to the following code.

dbConnect.java


try{
//Specify JDBC driver(JDBC3.0 or less)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(url); 
}

I fixed it and eliminated the error, but now that I can't use the try with resources statement, I managed to find a way to get it to work in the first code.

solution

The cause was that jdbc was not specified in the tomcat classpath, so the path to jdbc could not be known without Class.forName.

Since I was using eclipse, I selected "tomcat> Execution configuration> Classpath> Select external JAR" and passed the path to the jdbc jar imported under lib, and it ended normally.

afterwards

I like Azure, so I deployed it on AppService, but I don't know how to add it to the tomcat classpath, so I'll add it when I understand it.

Recommended Posts

Try with resources statement in web app
Try making a calculator app in Java
Try App Clips
Deploy Java web app to Azure with maven
Try RESTful web services with Nablarch's container archetype
Try running an app made with Quarkus on Heroku
Implemented pull-down in the original app with Active Hash
Try DI with Micronaut
Try create with Trailblazer
Rock-paper-scissors app in Java
Try WebSocket with jooby
Try LetCode in Ruby-TwoSum
Security in web applications
Try WildFly with Docker
I searched for a web framework with Gem in Ruby
Create a Hello World web app with Spring framework + Jetty
Try developing a containerized Java web application with Eclipse + Codewind