使用java在main方法中操作数据库(调试数据)
【代码】使用java在main方法中操作数据库(调试数据)
·
使用java在main方法中操作数据库
import cn.hutool.core.collection.CollUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class MainService {
private static String appId = "xx";
private static String appSecret = "xxx";
private static String media_id = "xxx";
public static void main(String[] args) {
// 1. 配置数据源
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/base?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true");
config.setUsername("root");
config.setPassword("root");
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
DataSource dataSource = new HikariDataSource(config);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List<Station> stations = jdbc.query("select * from bus_stations where status is null limit 10", new RowMapper<Station>() {
@Override
public Station mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Station(rs.getInt("station_id"), rs.getString("station_name"), rs.getString("address"), rs.getString("phone"), rs.getString("sale_time"), rs.getString("status"));
}
});
for (int i = 0; i < stations.size(); i++) {
List<Checi> checiList = jdbc.query("select * from bus_schedules where station_id="+stations.get(i).getId(), new RowMapper<Checi>() {
@Override
public Checi mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Checi(rs.getString("station_name"), rs.getString("destination"), rs.getString("departure_time"), rs.getString("arrival_miles"), rs.getString("price"), rs.getString("bus_type"));
}
});
// 发布到
List<List<Checi>> checiLists = CollUtil.split(checiList, 200);
for (List<Checi> checis: checiLists) {
push2wxgzh(stations.get(i), checis);
}
//更新status
jdbc.update("update bus_stations set status='1' where station_id="+stations.get(i).getId());
}
}
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)