Results 1 to 2 of 2
  1. #1
    Pumbaa is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Permanent reusable connection to DB

    main class calls init() method in ConnectDB class to establish connection with DB:
    PHP Code:
    public class Main {
    
        private static ServerConnect aServerConnect;
        public static ConnectDB connect;
        
        public static void main(String[] args) throws Exception {
    
        connect = new ConnectDB();
            connect.init();
        }
    PHP Code:
    public class ConnectDB {
    
        private static String databaseurl = "jdbc:oracle:thin:@10.1.1.7:1521:orcl";
        private static String user = "oracle";
        private static String password = "1234";
        private static String driverName = "oracle.jdbc.OracleDriver";
        public Connection conn;
        public ResultSet rs;
        public Statement statement;
        public PreparedStatement ps;
    
        public final Connection init() throws SQLException {
    
            try {
                Class.forName(driverName);
                conn = (Connection) DriverManager.getConnection(databaseurl, user, password);
                System.out.println(conn + " : Connected to DB at : " + new java.util.Date());
                System.out.println();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ServerConnect.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(ServerConnect.class.getName()).log(Level.SEVERE, null, ex);
            }
            return conn;
        }
    
        public void insert(String sql) throws SQLException {
           // init();
            statement = (Statement)conn.createStatement();
            int st = statement.executeUpdate(sql);
          //  destroy();
            statement.close();
        }
    
        public void destroy() {
            try {
                conn.close();
                System.out.println(conn +" : Disconnected at : " + new java.util.Date());
                System.out.println();
            } catch (SQLException ex) {
                Logger.getLogger(ServerConnect.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    connection established OK:
    PHP Code:
    oracle.jdbc.driver.T4CConnection@6ef137d : Connected to  DB at : Mon Nov 22 20:21:09 EET 2010
    but after this insert method doesn't see conn and create NullPointerException to
    PHP Code:
    statement = (Statement)conn.createStatement();
    as soon I remove comments from init(); and destroy(); into insert method all data starts to write into DB, but with big delay because of each time connect and disconnect.
    Please advice what is wrong in my code, why insert method doesn't see and use connection established from main method?
    And works only with direct calling of init();
    Last edited by Pumbaa; 11-23-2010 at 11:51 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Something else is going on.
    Either the insert() is being called before the init() or some other bit of code is setting the conn to null.
    Obviously we can't tell here because we don't have your code.

    One comment, your statement.close() should be in a finally block to ensure it gets called if there's an error. Also doing a generic insert() method like that means you can't use PreperaedStatements easily, so you might want to revisit that side of things.

    ETA: The other possibility is you're not using the same ConnectDB object for the insert as the one on which you called init().

Similar Threads

  1. Replies: 0
    Last Post: 02-09-2010, 04:42 PM
  2. Mysql reusable
    By Kligham in forum New To Java
    Replies: 1
    Last Post: 09-13-2009, 07:08 AM
  3. Permanent connection to an ArrayList
    By carderne in forum New To Java
    Replies: 11
    Last Post: 12-09-2008, 10:13 AM
  4. Java Web Developers Needed - Permanent Position
    By ncortel in forum Jobs Offered
    Replies: 0
    Last Post: 08-04-2008, 06:36 AM
  5. Java Web Developer - Permanent Role - Swindon
    By Mike Brown in forum Jobs Offered
    Replies: 0
    Last Post: 02-12-2008, 05:21 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
  •