Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refresh connection to database every 5 seconds through a java program 3

Status
Not open for further replies.

pjeypal

Programmer
Apr 17, 2006
36
US
Hi

I am writing a small java program to connect every 5 seconds via the jdbc-odbc bridge driver. Is there any option to refresh connection every 5 secs?

Thanks in advance

 
Why would you want to refresh the connection? Does the driver requires that?

What's your question about: how to refresh, how to launch every 5 seconds ...?

Cheers,
Dian
 
Thanks Dian for your reply

I need to launch the connection every 5 secs. Is this possible?
 
Why on earth would you need to do that ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Use the SwingWorker class to fire off a background thread and loop forever with "wait(5000);launchconnection()"
 
... but we would still be interested to know why you'd like to do this.

Tim
 
Timer and TimerTask classes are intented for repetitive or scheduled work, but I agree it's not a good thing to do.

Cheers,
Dian
 
Thanks for all the replies

Actually what i need to do is connect to an oracle database, wait for 2 secs and then disconnect (This need to be done only once).

 
This is the script

import java.sql.*;

class JdbcTest1 {

public static void main (String[] args) {
try {

// Step 1: Load the JDBC driver.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Step 2: Establish the connection to the database.
String url = "jdbc:eek:dbc:contact_mgr";
Connection conn = DriverManager.getConnection(url,"user1","password");

} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
 
Please use code tags when posting

Code:
import java.sql.*;  

class JdbcTest1 {  
      
    public static void main (String[] args) {  
        try {  

            // Step 1: Load the JDBC driver.  
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  

            // Step 2: Establish the connection to the database.  
            String url = "jdbc:odbc:contact_mgr";  
            Connection conn = DriverManager.getConnection(url,"user1","password");   
            Thread.currentThread.sleep(2000);
             conn.close();
        } catch (Exception e) {  
            System.err.println("Got an exception! ");  
            System.err.println(e.getMessage());  
        }  
    }  
}

Cheers,
Dian
 
... or

Code:
import java.sql.*;  

class JdbcTest1 {  
      
    public static void main (String[] args) {  
        Connection conn = null;
        try {  

            // Step 1: Load the JDBC driver.  
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  

            // Step 2: Establish the connection to the database.  
            String url = "jdbc:odbc:contact_mgr";  
            conn = DriverManager.getConnection(url,"user1","password");   
            Thread.currentThread.sleep(2000);
        } catch (Exception e) {  
            System.err.println("Got an exception! ");  
            System.err.println(e.getMessage());  
        } finally {
            try {
                if ( conn != null )
                    conn.close();
            } catch (SqlException ex){}
        }
    }  
}

Just in case the sleep is interrupted by another thread, to guarantee the connection is closed.

Tim
 
Any idea why this error


C:\>javac JdbcTest.java
JdbcTest.java:14: cannot resolve symbol
symbol : variable currentThread
location: class java.lang.Thread
Thread.currentThread.sleep(2000);
^
1 error

C:\>
 
#1 You really should be using the oracle driver (a jar called classes12.jar or ojdbc14.jar - either is fine) to connect to Oracle.

#2 To sleep for two seconds :

Code:
try {
  Thread.sleep(2000);
} catch (InterruptedException ie) {
   ie.printStackTrace(System.err);
}



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top