Connection and PreparedStatement



Is this a good way to insert and update into an Oracle database or is
there anything I can improve?


public class Sports
{
private Connection connection;

public Sports() {
connection = new DatabaseConnection().getConnection();
}


public void Inserter(BeanMain objref) throws SQLException
{
if(connection != null)
{
PreparedStatement stat =
connection.prepareStatement("Insert into FranchiseTable (team,
manager) values (?,?)");
stat.setString(1, objref.getTeam());
stat.setString(2, objref.getManager());
stat.executeUpdate();
stat.close();
}
}

public void Updater(BeanMain objref) throw SQLException
{
if(connection != null)
{
PreparedStatement stat = connection.prepareStatement("update
PlayersTable set Id = ? where name LIKE ? ");
stat.setInt(1, objref.getId());
stat.setString(2, objref.getName());
stat.executeUpdate():
stat.close();
}
}


public void DatabasesUpdIns(BeanMain objref)
{
try
{
Inserter(objref);
Updaterer(objref);
}
catch(SQLException ex)
{
System.out.println(ex);
}
finally
{
connection.close();
}

}

.



Relevant Pages

  • Re: Connection and PreparedStatement
    ... private Connection connection; ... Also, there is no exception handling, and exception handling in this place is always a tricky thing, in my experience. ... public void Inserter(BeanMain objref) throws SQLException ...
    (comp.lang.java.databases)
  • Re: Connection and PreparedStatement
    ... I modified it so the connection is established in each method which I ... in my view the use of the 'LIKE' operator is still a serious problem: you are sending a statement to the database that is *not* what you want it to be. ... public void inserter(SportsBean objref) throws SQLException ...
    (comp.lang.java.databases)
  • Re: Connection and PreparedStatement
    ... private Connection connection; ... I recommend against creating the connection in the constructor, but if you do you must make sure that it is closed *at the end of the object's life*, or at least its useful life, and that it cannot be re-used after close. ... public void Updater(BeanMain objref) throw SQLException ...
    (comp.lang.java.databases)
  • Re: Connection and PreparedStatement
    ... I modified it so the connection is established in each method which I ... public void inserter(SportsBean objref) throws SQLException ... PreparedStatement stat = ...
    (comp.lang.java.databases)
  • Re: VS2005 and SQL Express: Internal .Net Framework Data Provider
    ... connection object you use. ... public void CreateConnection{ ... // Close first and dispose the existing data reader is there ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)