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
- public class DbInsert
- {
- private PreparedStatement stat;
- private Connection connection;
- public DbInsert(Connection connection)
- {
- this.connection = connection;
- }
- public void cityInserter(FormBean city) throws SQLException
- {
- stat = connection.prepareStatement("Insert into City (street, school) values (?,?)");
- stat.setString(1, city.getStreet());
- stat.setString(2, city.getSchool());
- stat.executeUpdate();
- }
- //more Methods with preparedstatements here....
- }
- ....
Expand|Select|Wrap|Line Numbers
- public class DbWork
- {
- private Connection connection = new ConnectionMgr().getConnection();
- public dbMethod(FormBean city)
- {
- try
- {
- new DbInsert(connection).cityInserter(city);
- }
- catch(SQLException ex)
- {
- System.out.println(ex);
- }
- finally
- {
- connection.close();
- }
- }
- //more db methods using prepared statements here
- .....
- }
Expand|Select|Wrap|Line Numbers
- public void cityInserter(FormBean city) throws SQLException
- {
- stat = connection.prepareStatement("Insert into City (street, school) values (?,?)");
- stat.setString(1, city.getStreet());
- stat.setString(2, city.getSchool());
- stat.executeUpdate();
- connection.close();
- }