Results 1 to 3 of 3
Thread: Database connection
- 11-12-2007, 09:46 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
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(); } }
- 11-12-2007, 11:24 PM #2
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.
- 11-13-2007, 12:11 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
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
-
connection problem
By subash in forum JDBCReplies: 5Last Post: 04-22-2008, 10:17 AM -
JSP - using connection cache
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 10:54 AM -
no connection
By even in forum JDBCReplies: 15Last Post: 01-02-2008, 02:50 PM -
Database Connection
By vipinkumarsolanki in forum Advanced JavaReplies: 2Last Post: 11-26-2007, 07:36 AM -
error I/O connection
By bbq in forum JDBCReplies: 1Last Post: 07-05-2007, 02:33 PM
Bookmarks