I tried to write a below code where in two DB connections are getting open and close so will there be any issue in terms of TOO MANY CONNECTION issue? I am not getting any error but just wanted to confirm is there any issue in below code.
<%
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, username, password);
statement = connection.createStatement();
try {
rs1 = statement.executeQuery("SELECT QUERY");
if (rs1.next()) {
try {
PreparedStatement ps1 = DriverManager.getConnection(connectionURL, username, password).prepareStatement("UPDATE QUERY);
ps1.execute();
ps1.close();
} catch (Exception e) {
out.println(e);
}
} else {
rs1 = statement.executeQuery("SELECT QUERY");
if (rs1.next()) {
CONDITION.....
}
}
rs1.close();
}
}
finally {
if (!connection.isClosed() && connection != null) {
connection.close();
}
}
%>
Some HTML CODE.....HERE
<%
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, username, password);
statement = connection.createStatement();
try {
rs1 = statement.executeQuery("SELECT QUERY");
if (rs1.next()) {
try {
PreparedStatement ps1 = DriverManager.getConnection(connectionURL, username, password).prepareStatement("UPDATE QUERY);
ps1.execute();
ps1.close();
} catch (Exception e) {
out.println(e);
}
} else {
rs1 = statement.executeQuery("SELECT QUERY");
if (rs1.next()) {
CONDITION.....
}
}
rs1.close();
}
}
finally {
if (!connection.isClosed() && connection != null) {
connection.close();
}
}
%>