MySQL驱动时不时报错

J2EE 码拜 9年前 (2016-03-19) 1051次浏览
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/huanwei?characterEncoding=UTF8
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at com.galaxylab.huanwei.dao.GenericDAO.getConnection(GenericDAO.java:37)
at com.galaxylab.huanwei.dao.GenericDAO.executeQuery(GenericDAO.java:49)
at com.galaxylab.huanwei.dao.TokenDAO.getToken(TokenDAO.java:16)
at com.galaxylab.huanwei.service.TokenRefresher.run(AccessTokenGenerationService.java:50)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
2016-05-24 15:35:56,639 [ERROR] [pool-3-thread-1:4802146][com.galaxylab.huanwei.service.TokenRefresher] – Exception
java.lang.NullPointerException
at com.galaxylab.huanwei.dao.TokenDAO.getToken(TokenDAO.java:17)
at com.galaxylab.huanwei.service.TokenRefresher.run(AccessTokenGenerationService.java:50)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
这一段错误时不时的会报出来,但是又找不到原因,程序又能正常运行,请高手指点指点,怎么下手处理这个异常
这是本人的一个定时线程进入的DAO

package com.galaxylab.huanwei.service;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.galaxylab.huanwei.dao.TokenDAO;
import com.galaxylab.huanwei.domain.Token;
import com.galaxylab.huanwei.dto.GetTokenResponse;
import com.galaxylab.huanwei.jdbc.CommonConfig;
import com.galaxylab.huanwei.util.HttpUtil;
import com.google.gson.Gson;
public class AccessTokenGenerationService {
	private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

	private final Logger logger = LoggerFactory.getLogger(getClass());

	public AccessTokenGenerationService() {}

	public void start() {
		Runnable task = new TokenRefresher();
		logger.info("Start AccessTokenGenerationService()");
		scheduledExecutorService.scheduleWithFixedDelay(task, 0, 600,TimeUnit.SECONDS);
	}


}
class TokenRefresher implements Runnable{

	private final TokenDAO tokenDAO = new TokenDAO();

	private final long GRACE_PERIOD = 1800000;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	public Gson gson = new Gson();

	@SuppressWarnings("static-access")
	@Override
	public void run() {
		try{
			Token token = tokenDAO.getToken();

			logger.info("latest token is {}",token);

			if(token.getGoodUntil().getMillis() - DateTime.now().getMillis() < GRACE_PERIOD) {
				logger.info("Start getting new token");

				String url = CommonConfig.WECHAT_ACCESS_TOKEN_SERVICE_URL
						.replace("APPID", CommonConfig.WECHAT_APP_ID)
						.replace("APPSECRET", CommonConfig.WECHAT_SECURITY_KEY);

				logger.info("URL is {}",url);

				String response = HttpUtil.get(url);

				GetTokenResponse getTokenResponse = gson.fromJson(response, GetTokenResponse.class);

				Token new_token = new Token(getTokenResponse.getAccess_token(),
						new DateTime().now().plusSeconds(Integer.valueOf(getTokenResponse.getExpires_in())));

				tokenDAO.updataToken(new_token);
			}
		}catch(Exception e) {
			logger.error("Exception",e);
		}
	}
}
解决方案

20

项目引用的mysql驱动可能比较旧
更新一下试试

10

TokenDAO 里面代码是不是有问题

10

java.lang.NullPointerException
at com.galaxylab.huanwei.dao.TokenDAO.getToken(TokenDAO.java:17)
看看这里是啥

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明MySQL驱动时不时报错
喜欢 (0)
[1034331897@qq.com]
分享 (0)