Results 1 to 3 of 3
  1. #1
    oaklander is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default Database connection

    I created my first Java class that inserts and updates an Oracle 9i database. I am running single insert and single update statements.
    The below is what I have been using and was wondering if it is efficient or anyway to improve it:

    Java Code:
    public class DbWork
    {
    
        private Connection connection = new ConnectionMgr().getConnection();
        private PreparedStatement stat;
    
         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();
        }
    
        public void cityUpdater(FormBean city) throws SQLException  {
            stat = con.prepareStatement("update Person set PersonId = ? where name LIKE ? ");
            stat.setInt(1, city.getPersonId());
            stat.setString(2, city.getName());
            stat.executeUpdate():
        }
    
    
       public void dbMethod(FormBean city)
       {
            try 
           {
                   cityInserter(city);
                   cityUpdater(city);
           }
           catch(SQLException ex)
           {
                  System.out.println(ex);
           }
           finally
           {
                 connection.close();
           }
    
    }

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Looks just like something that I wrote for to update some DB2 tables. It was my first time connecting to databases with Java so I'm not sure how much weight my thoughts carry.

  3. #3
    oaklander is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    I hope we are doing it correctly because I am concerned about efficiency and I read that I shouldnt keep the Connection and PreparedStatement as an instance variable. Also what are consequences to making the connection happen at construction time and making 'connection' and 'stat' instance variables?

Similar Threads

  1. connection problem
    By subash in forum JDBC
    Replies: 5
    Last Post: 04-22-2008, 09:17 AM
  2. JSP - using connection cache
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 09:54 AM
  3. no connection
    By even in forum JDBC
    Replies: 15
    Last Post: 01-02-2008, 01:50 PM
  4. Database Connection
    By vipinkumarsolanki in forum Advanced Java
    Replies: 2
    Last Post: 11-26-2007, 06:36 AM
  5. error I/O connection
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •