473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Database helper class

I have a JDBC working with Oracle 9i in my Tomcat 4.1.3 Container.

The Database classes I have used for the past year are working great but I wonder if I should be closing anything in my database helper class with Prepared statements:
Expand|Select|Wrap|Line Numbers
  1. public class DbInsert
  2. {
  3.     private PreparedStatement stat;
  4.     private Connection connection;
  5.  
  6.     public DbInsert(Connection connection)
  7.     {
  8.          this.connection = connection;
  9.     }
  10.  
  11.     public void cityInserter(FormBean city) throws SQLException
  12.     {
  13.         stat = connection.prepareStatement("Insert into City (street, school) values (?,?)");
  14.         stat.setString(1, city.getStreet());
  15.         stat.setString(2, city.getSchool());
  16.        stat.executeUpdate();
  17.     }
  18.  
  19.     //more Methods with preparedstatements here....
  20. }
  21.  
  22. ....
  23.  
Expand|Select|Wrap|Line Numbers
  1. public class DbWork
  2. {
  3.  
  4.     private Connection connection = new ConnectionMgr().getConnection();
  5.  
  6.    public dbMethod(FormBean city)
  7.    {
  8.         try 
  9.        {
  10.                new DbInsert(connection).cityInserter(city);
  11.        }
  12.        catch(SQLException ex)
  13.        {
  14.               System.out.println(ex);
  15.        }
  16.        finally
  17.        {
  18.              connection.close();
  19.        }
  20.    }
  21.  
  22.    //more db methods using prepared statements here
  23.  
  24. .....
  25. }
  26.  
When I experiment and put a close statement in the DbInsert class method then my database insert wont work because it would be closed when it is called in the DbWork class?
Expand|Select|Wrap|Line Numbers
  1.  public void cityInserter(FormBean city) throws SQLException
  2.     {
  3.         stat = connection.prepareStatement("Insert into City (street, school) values (?,?)");
  4.         stat.setString(1, city.getStreet());
  5.         stat.setString(2, city.getSchool());
  6.        stat.executeUpdate();
  7.        connection.close();
  8.     }
Please advise.
Nov 9 '07 #1
3 3241
r035198x
13,262 8TB
Sorry I don't quite get the question but if you close the connection then you most certainly won't be able to perform another insert after that without creating a new connection.
Nov 9 '07 #2
Thanks in my example I have a Database connection in my Constructor.
Is it not a good idea to put a Database connection in a Constructor?

Is the below way more efficient???

Expand|Select|Wrap|Line Numbers
  1. public class DbWork
  2. {
  3.  
  4.     private Connection connection = new ConnectionMgr().getConnection();
  5.     private PreparedStatement stat;
  6.  
  7.      public void cityInserter(FormBean city) throws SQLException
  8.     {
  9.         stat = connection.prepareStatement("Insert into City (street, school) values (?,?)");
  10.         stat.setString(1, city.getStreet());
  11.         stat.setString(2, city.getSchool());
  12.         stat.executeUpdate();
  13.     }
  14.    //more db methods to insert and update tables
  15.  
  16.    public void dbMethod(FormBean city)
  17.    {
  18.         try 
  19.        {
  20.                cityInserter(city);
  21.        }
  22.        catch(SQLException ex)
  23.        {
  24.               System.out.println(ex);
  25.        }
  26.        finally
  27.        {
  28.              connection.close();
  29.        }
  30.    }
  31.    //more db methods that call my insert and update methods
  32. }
Nov 10 '07 #3
heat84
118 100+
Thats more like it. You can execute more queries as long as the connection is not closed.
Nov 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tim Lesher | last post by:
I'm writing a database helper class that represents records in a SQL database as objects. I want to be able to instantiate the objects (from the database), play with their values locally, then...
4
by: Robert Ferrell | last post by:
I have a style question. I have a class with a method, m1, which needs a helper function, hf. I can put hf inside m1, or I can make it another method of the class. The only place hf should ever...
1
by: shaun roe | last post by:
When should a function be a private member, and when simply a standalone function in the .cpp file? I'm in the middle of writing a class which bridges between two packages, and so I need some...
6
by: mailforpr | last post by:
Suppose you have a couple of helper classes that are used by 2 client classes only. How can I hide these helper classes from other programmers? Do you think this solution is a good idea?: class...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.