Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 10:46 PM
Member
 
Join Date: Nov 2007
Posts: 2
oaklander is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 11-13-2007, 12:24 AM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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-13-2007, 01:11 AM
Member
 
Join Date: Nov 2007
Posts: 2
oaklander is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 11:17 AM
JSP - using connection cache Java Tip Java Tips 0 01-30-2008 11:54 AM
no connection even Database 15 01-02-2008 03:50 PM
Database Connection vipinkumarsolanki Advanced Java 2 11-26-2007 08:36 AM
error I/O connection bbq Database 1 07-05-2007 03:33 PM


All times are GMT +3. The time now is 09:37 PM.


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