I want to create a program which will execute three different queries and their result would be mailed along with the user name in a tabular format. Below is the code I am designing,
public class SendNotification {
public static void main(String[] args) throws SQLException, AddressException, MessagingException {
// Database connection details
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String user = "localuser";
String password = "xxxxxx";
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
//User Details
String user1 = "abUser";
String user2 = "xyUser";
String user3 = "cvUser";
// SQL queries for each user
String query1 = "SELECT COUNT(*) FROM dbtest WHERE AUTH = '" +user1+"' AND outdate >= SYSDATE - 7";
String query2 = "SELECT COUNT(*) FROM dbtest WHERE AUTH = '" +user2+"' AND outdate >= SYSDATE - 7";
String query3 = "SELECT COUNT(*) FROM dbtest WHERE AUTH = '" +user3+"' AND outdate >= SYSDATE - 7";
// Create a connection to the database
Connection connection = DriverManager.getConnection(url, user, password);
// Create a statement and execute each query
Statement statement = connection.createStatement();
ResultSet resultSet1 = statement.executeQuery(query1);
ResultSet resultSet2 = statement.executeQuery(query2);
ResultSet resultSet3 = statement.executeQuery(query3);
// Get the results and store them in a table
Object[][] data = {
{"User ID", "Document Count"},
{user1, resultSet1.getInt(1)},
{user2, resultSet2.getInt(1)},
{user3, resultSet3.getInt(1)}
};
// Send an email with the results in tabular format
String from = "devtest@ugc.local";
String to = "PBxyz@gmail.com";
String host = "mail.test.vb.xcv";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Checked-In Document Counts");
StringBuilder table = new StringBuilder();
for (Object[] row : data) {
table.append("<tr><td>").append(row[0]).append("</td><td>").append(row[1]).append("</td></tr>");
}
message.setText("<html><body><table>" + table.toString() + "</table></body></html>", "utf-8", "html");
Transport.send(message);
// Close the statement, result sets, and connection
resultSet1.close();
resultSet2.close();
resultSet3.close();
statement.close();
connection.close();
}
}
But, I am encountering with the below error everytime.
Exception in thread "main" java.sql.SQLException: Closed Resultset: getInt at oracle.jdbc.driver.GeneratedScrollableResultSet.getInt(GeneratedScrollableResultSet.java:237) at com.ura.SendNotification.main(SendTINNotification.java:48)
How shall i proceed to get the desired output?