<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="/luweb/checkCode"></body><a href="javascript:void(0);">看不清?换一张</a>
</body>
<script>
window.onload=function () {
let img = document.getElementsByTagName("img")[0];
img.onclick=function () {
//给图片设置时间戳
var data=new Date().getTime();
img.src="/luweb/checkCode?"+data;
}
let a = document.getElementsByTagName("a")[0].onclick=function () {
var data=new Date().getTime();
img.src="/luweb/checkCode?"+data;
}
}
</script>
</html>
package com.itcast.JDBCUtils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* druid的工具类
*/
public class DruidUtils {
private static DataSource dataSource;
//静态代码块
static{
Properties pro=new Properties();
InputStream resourceAsStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(resourceAsStream);
try {
dataSource= DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
//释放资源
public static void close(Connection con, PreparedStatement pre){
close(con,pre,null);
}
//释放资源
public static void close(Connection con, PreparedStatement pre, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(pre!=null){
try {
pre.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}