大数据全系列 教程
1869个小节阅读:468k
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
xxxxxxxxxx
package com.itbaizhan.utils;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 加载初始数据
*
*/
public class JdbcUtils {
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, InterruptedException {
//通过java访问mysql数据库
//这个对象获取数据库链接
//注册驱动,输入链接地址,用户名,密码
String driver = "com.mysql.cj.jdbc.Driver";
Class.forName(driver);
//访问本机的mysql数据库,格式 jdbc:数据库://本地端口3306/数据库名?跨时区参数
String url = "jdbc:mysql://192.168.66.100:3306/order?serverTimezone=Asia/Shanghai";
String username = "root";
String password = "123456";
//获取到数据库链接
Connection connection = DriverManager.getConnection(url, username, password);
List<String> strings = FileUtils.readLines(new File("C:\\Users\\wcc\\Desktop\\百战出行\\源码\\数据\\dwv_order_make_haikou_1.txt"), "utf-8");
for (String orderInfo : strings) {
////文件中包含文件头,所以需要正则判断是否是数据行
Pattern pattern = Pattern.compile("^[0-9].*");
Matcher matcher = pattern.matcher(orderInfo);
if (matcher.matches()){
String[] orderInfos = orderInfo.split("\t", -1);
if (orderInfos == null || (orderInfos.length != 24)) {
return ;
}
String sql = "INSERT INTO `order`.`orders`(`order_id`, `product_id`, `city_id`, `district`, `county`, `type`, " +
"`combo_type`, `traffic_type`, `passenger_count`, `driver_product_id`, `start_dest_distance`, " +
"`arrive_time`, `departure_time`, `pre_total_fee`, `normal_time`, `bubble_trace_id`, `product_1level`, " +
"`dest_lng`, `dest_lat`, `starting_lng`, `starting_lat`, `year`, `month`, `day`) " +
"VALUES ( ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?)";
//创建一个Statement语句对象
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,orderInfos[0]);
ps.setString(2,orderInfos[1]);
ps.setString(3,orderInfos[2]);
ps.setString(4,orderInfos[3]);
ps.setString(5,orderInfos[4]);
ps.setString(6,orderInfos[5]);
ps.setString(7,orderInfos[6]);
ps.setString(8,orderInfos[7]);
ps.setString(9,orderInfos[8]);
ps.setString(10,orderInfos[9]);
ps.setString(11,orderInfos[10]);
ps.setString(12,orderInfos[11] + "");
ps.setString(13,orderInfos[12] + "");
ps.setString(14,orderInfos[13] + "");
ps.setString(15,orderInfos[14] + "");
ps.setString(16,orderInfos[15]);
ps.setString(17,orderInfos[16]);
ps.setString(18,orderInfos[17]);
ps.setString(19,orderInfos[18]);
ps.setString(20,orderInfos[19]);
ps.setString(21,orderInfos[20]);
ps.setString(22,orderInfos[21]);
ps.setString(23,orderInfos[22]);
ps.setString(24,orderInfos[23]);
int r = ps.executeUpdate();
if (r >0){
System.out.println("添加成功");
Thread.sleep(5000);
}
}
}
}
}