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.
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.
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