I think the issue Is in the getConnection

try(Connection con = DriverManager.getConnection(myDB.dbUrl, myDB.user,
"");)

This syntax closed the connection  when exit from try block, in your case
when it return


On Wed, Feb 13, 2019, 5:35 AM Madhavi <[email protected] wrote:

> I'm trying to use H2 database in my program and I keep getting "Object
> already closed" exception when the createStatement() method is called.
> Below is my Java program. Can anyone help me fix the error please?
>
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
>
> import org.h2.engine.Database;
>
> public class MyConnections {
>
> String dbUrl = "jdbc:h2:~/ctgen";
> String user = "sa";
> public static void main(String[] args) {
> Connection conn = getConnection();
> String name = "";
> if(conn != null) {
> System.out.println("Connection Successful !!");
> // Get username
> name = getData(conn, 101);
> System.out.println("User Name before update : " + name);
> }
> if(conn != null) {
> // Update name
> String newName = "NewUser";
> updateUser(conn, newName, 101);
> }
> if(conn != null) {
> // Check if the name is updated
> name = getData(conn, 101);
> System.out.println("User Name after update : " + name);
> }
> try {
> if(conn != null)
> conn.close();
> } catch (SQLException e) {
> e.printStackTrace();
> }
> }
> public static Connection getConnection() {
> MyConnections myDB = new MyConnections();
> try(Connection con = DriverManager.getConnection(myDB.dbUrl, myDB.user,
> "");){
> con.setAutoCommit(true);
> return con;
> } catch (SQLException e) {
> e.printStackTrace();
> return null;
> }
> }
> public static String getData(Connection conn, int id) {
> String username = null;
> Statement stmt = null;
> ResultSet rs = null;
> if(conn != null) {
> try {
> stmt = conn.createStatement();
> rs = stmt.executeQuery("SELECT * FROM USER WHERE ID = '" + id + "'");
> while(rs.next()) {
> username = rs.getString("NAME");
> }
> rs.close();
> stmt.close();
> } catch (SQLException e) {
> e.printStackTrace();
> }
> }
> return username;
> }
> public static boolean updateUser(Connection conn, String newName, int id) {
> Statement stmt = null;
> if(conn != null) {
> try {
> stmt = conn.createStatement();
> int result = stmt.executeUpdate("UPDATE TABLE USER SET NAME = '"+ newName
> +"' WHERE ID = '" + id + "'");
> if(result == 1) {
> return true;
> }
> stmt.close();
> } catch (SQLException e) {
> e.printStackTrace();
> return false;
> }
> }
>
> return false;
> }
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/h2-database.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Reply via email to