我无法在timertask类中的run方法中打开mysql连接。它会抛出一个异常。当我试图使用timer.scheduleAtFixedRate(backgroundexecution,0,5000;方法在我的主类中运行这个类时,它不起作用。有人知道我怎么能解决这个问题吗?
@Override
public void run() {//here is my run method
backgroundtask obj = new backgroundtask();
try{
Statement statement = obj.openConnection(); //it throws an exception at this line
String mysqlcommand = "Select *from abc";
ResultSet sonuc = statement.executeQuery(mysqlcommand);
while(sonuc.next()){
if(search.contentEquals(sonuc.getString("ssa"))){
System.out.println(sonuc.getString(2));
}
}
statement.close();
}catch(Exception ex) {
System.out.println("fail");
}
}发布于 2014-06-03 18:59:58
在尝试执行db查询之前,需要添加这一行(它加载数据库驱动程序类);
Class.forName("com.mysql.jdbc.Driver");如果使用不同的db驱动程序,则字符串可能有所不同。还要确保jdbc在您的类路径中。
https://stackoverflow.com/questions/24022813
复制相似问题