Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Timer to Connect Database every minute

Oracle ITselfJul 27 2010 — edited Jul 27 2010
Dear All
i need to make database issue using console java app, first task in this issue is to establish the database connection, and sure i have timer to do this issue every minute.

my problem is : i am getting this error in the minute 19 !! , please how to solve this issue ?

Exception in thread "Timer-0" java.lang.NullPointerException
* at test.RemindTask$mytask.UserNamePassword(RemindTask.java:48)*
* at test.RemindTask$mytask.run(RemindTask.java:24)*
* at java.util.TimerThread.mainLoop(Timer.java:512)*
* at java.util.TimerThread.run(Timer.java:462)*


package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.Timer;
import java.util.TimerTask;

public class RemindTask extends TimerTask {

public void run() {
mytask newtask = new mytask();
Timer t = new Timer();
t.schedule(new mytask(),0, 60 * 1000);
}

public class mytask extends TimerTask {

public void run() {
mytask x = new mytask();
try {
x.UserNamePassword();
} catch (SQLException e) {
}
System.out.println("upupupu");
}

public void UserNamePassword() throws SQLException {
String User = "TEST";
String Password = "TEST";
java.sql.PreparedStatement z = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", User,
Password);

} catch (SQLException e) {
}
try {
if (conn.isValid(1) == true) {
String query = "begin main; end;";
z =
conn.prepareStatement(query);
z.executeUpdate();
} else {
String query = "begin main; end;";
}
} catch (SQLException e) {
}
}
}
public static void main(String[] arg) {
RemindTask ab = new RemindTask();
ab.run();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2010
Added on Jul 27 2010
2 comments
996 views