Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 08:46 PM
Member
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0
oaklander is on a distinguished road
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:

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();
       }

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-12-2007, 10:24 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-12-2007, 11:11 PM
Member
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0
oaklander is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
connection problem subash Database 5 04-22-2008 09:17 AM
JSP - using connection cache Java Tip Java Tips 0 01-30-2008 09:54 AM
no connection even Database 15 01-02-2008 01:50 PM
Database Connection vipinkumarsolanki Advanced Java 2 11-26-2007 06:36 AM
error I/O connection bbq Database 1 07-05-2007 01:33 PM


All times are GMT +2. The time now is 11:13 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org