oracle
修改密码漏洞修复完成 1 year ago
parent 4c6a87b354
commit b32202b1c4

@ -37,6 +37,13 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

@ -0,0 +1,75 @@
package com.rehome.mqttclienttemperature.controller;
import com.rehome.mqttclienttemperature.datasource.DataSource;
import com.rehome.mqttclienttemperature.dto.ResponseDto;
import com.rehome.mqttclienttemperature.utils.JdbcUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*
* 使MyBatisJPA便
* jdbcMyBatisJPA
*
* fastjson,druid,mysqlpom.xml
*
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>fastjson</artifactId>
* <version>1.2.62</version>
* </dependency>
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>druid</artifactId>
* <version>1.1.9</version>
* </dependency>
* <!-- Mysql -->
* <dependency>
* <groupId>mysql</groupId>
* <artifactId>mysql-connector-java</artifactId>
* </dependency>
*
*/
/**
*
* https://download.csdn.net/download/lxyoucan/85094574
*
*
* https://github.com/freakchick/DBApi
*
*SpringBootjdbc
* https://blog.csdn.net/lxyoucan/article/details/124042295
*
*
*/
@RestController
public class JdbcDemoController {
public static DataSource ds = new DataSource();
static {
//配置数据源
ds.setId("1");
ds.setName("mysql");
//ds.setUrl("jdbc:mysql://192.168.3.7:3306/appserver?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8");
ds.setUrl("jdbc:mysql://192.168.3.7:3306/appserver?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true");
ds.setUsername("root");
ds.setPassword("Skyinno251,");
ds.setDriver("com.mysql.cj.jdbc.Driver");
}
/**
*
* @return
*/
//@RequestMapping("/api/list")
public ResponseDto queryList()
{
String sql = "show tables";
ResponseDto responseDto = JdbcUtil.executeSql(ds,sql);
return responseDto;
}
}

@ -0,0 +1,18 @@
package com.rehome.mqttclienttemperature.datasource;
import lombok.Data;
/**
*
*
*/
@Data
public class DataSource {
String id;
String name;
String url;
String username;
String password;
String driver;
}

@ -0,0 +1,43 @@
package com.rehome.mqttclienttemperature.dto;
import lombok.Data;
/**
*
*/
@Data
public class ResponseDto {
String msg;
Object data;
boolean success;
public static ResponseDto apiSuccess(Object data) {
ResponseDto dto = new ResponseDto();
dto.setData(data);
dto.setSuccess(true);
dto.setMsg("接口访问成功");
return dto;
}
public static ResponseDto successWithMsg(String msg) {
ResponseDto dto = new ResponseDto();
dto.setData(null);
dto.setSuccess(true);
dto.setMsg(msg);
return dto;
}
public static ResponseDto successWithData(Object data) {
ResponseDto dto = new ResponseDto();
dto.setData(data);
dto.setSuccess(true);
return dto;
}
public static ResponseDto fail(String msg) {
ResponseDto dto = new ResponseDto();
dto.setSuccess(false);
dto.setMsg(msg);
return dto;
}
}

@ -2,8 +2,14 @@ package com.rehome.mqttclienttemperature.service;
import com.google.gson.Gson;
import com.rehome.mqttclienttemperature.controller.JdbcDemoController;
import com.rehome.mqttclienttemperature.dto.ResponseDto;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author huangwenfei
@ -14,4 +20,18 @@ import org.springframework.stereotype.Component;
@Component
public class ScheduledService {
@Resource
private JdbcDemoController jdbcDemoController;
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
@Scheduled(cron = "0/10 * * * * *")
public void getNmcWeatherProvince() {
ResponseDto responseDto = jdbcDemoController.queryList();
System.out.println(new Gson().toJson(responseDto));
}
}

@ -0,0 +1,85 @@
package com.rehome.mqttclienttemperature.utils;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rehome.mqttclienttemperature.datasource.DataSource;
import com.rehome.mqttclienttemperature.dto.ResponseDto;
import lombok.extern.slf4j.Slf4j;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class JdbcUtil {
/**
* sql
*
* @param datasource
* @param sql
*/
public static ResponseDto executeSql(DataSource datasource, String sql) {
return executeSql(datasource,sql,new ArrayList<Object>());
}
/**
* sql
*
* @param datasource
* @param sql
* @param jdbcParamValues
*/
public static ResponseDto executeSql(DataSource datasource, String sql, List<Object> jdbcParamValues) {
log.info(sql);
log.info(JSON.toJSONString(jdbcParamValues));
DruidPooledConnection connection = null;
try {
connection = PoolManager.getPooledConnection(datasource);
PreparedStatement statement = connection.prepareStatement(sql);
for (int i = 1; i <= jdbcParamValues.size(); i++) {
statement.setObject(i, jdbcParamValues.get(i - 1));
}
boolean hasResultSet = statement.execute();
if (hasResultSet) {
ResultSet rs = statement.getResultSet();
int columnCount = rs.getMetaData().getColumnCount();
List<String> columns = new ArrayList<>();
for (int i = 1; i <= columnCount; i++) {
String columnName = rs.getMetaData().getColumnLabel(i);
columns.add(columnName);
}
List<JSONObject> list = new ArrayList<>();
while (rs.next()) {
JSONObject jo = new JSONObject();
columns.stream().forEach(t -> {
try {
Object value = rs.getObject(t);
jo.put(t, value);
} catch (SQLException e) {
e.printStackTrace();
}
});
list.add(jo);
}
return ResponseDto.apiSuccess(list);
} else {
int updateCount = statement.getUpdateCount();
return ResponseDto.apiSuccess("sql修改数据行数" + updateCount);
}
} catch (Exception e) {
e.printStackTrace();
return ResponseDto.fail(e.getMessage());
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

@ -0,0 +1,81 @@
package com.rehome.mqttclienttemperature.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.rehome.mqttclienttemperature.datasource.DataSource;
import lombok.extern.slf4j.Slf4j;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
*/
@Slf4j
public class PoolManager {
private static Lock lock = new ReentrantLock();
private static Lock deleteLock = new ReentrantLock();
//所有数据源的连接池存在map里
static Map<String, DruidDataSource> map = new HashMap<>();
public static DruidDataSource getJdbcConnectionPool(DataSource ds) {
if (map.containsKey(ds.getId())) {
return map.get(ds.getId());
} else {
lock.lock();
try {
log.info(Thread.currentThread().getName() + "获取锁");
if (!map.containsKey(ds.getId())) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setName(ds.getName());
druidDataSource.setUrl(ds.getUrl());
druidDataSource.setUsername(ds.getUsername());
druidDataSource.setPassword(ds.getPassword());
druidDataSource.setDriverClassName(ds.getDriver());
druidDataSource.setConnectionErrorRetryAttempts(3); //失败后重连次数
druidDataSource.setBreakAfterAcquireFailure(true);
map.put(ds.getId(), druidDataSource);
log.info("创建Druid连接池成功{}", ds.getName());
}
return map.get(ds.getId());
} catch (Exception e) {
return null;
} finally {
lock.unlock();
}
}
}
//删除数据库连接池
public static void removeJdbcConnectionPool(String id) {
deleteLock.lock();
try {
DruidDataSource druidDataSource = map.get(id);
if (druidDataSource != null) {
druidDataSource.close();
map.remove(id);
}
} catch (Exception e) {
log.error(e.toString());
} finally {
deleteLock.unlock();
}
}
public static DruidPooledConnection getPooledConnection(DataSource ds) throws SQLException {
DruidDataSource pool = PoolManager.getJdbcConnectionPool(ds);
DruidPooledConnection connection = pool.getConnection();
// log.info("获取连接成功");
return connection;
}
}
Loading…
Cancel
Save