当前位置:网站首页>[file upload] parse text files and store them in batches through JDBC connection (dynamic table creation and dynamic storage)
[file upload] parse text files and store them in batches through JDBC connection (dynamic table creation and dynamic storage)
2022-07-25 03:26:00 【No bug is the biggest bug】
Be careful : I use it directly here main Function demonstration , Subsequent integration SpringBoot In the method
Text file format ( Field to !| Division ):

public static void main(String[] args) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:3306/table", "root", "root");// Create a data connection
LineIterator lineIterator = FileUtils.lineIterator(new File("D:\\Flink\\src\\main\\resources\\AMM_ITF_D_PRD_INFO.TXT"), "utf-8");
conn.setAutoCommit(false);
Integer a = 0;
String tableName = "table_" + System.currentTimeMillis();
String[] fields = null;
PreparedStatement ps = null;
String result = "";
while (lineIterator.hasNext()) {
result = lineIterator.next();
if (a == 0) {
fields = result.split("\\!\\|");
// Dynamic table building
String sql = "create table " + tableName + "(";
if (fields != null && fields.length > 0) {
int len = fields.length;
for (int i = 0; i < len; i++) {
// Add fields
sql += fields[i].trim() + " text";
// Prevent the last ,
if (i < len - 1) {
sql += ",";
}
}
}
// Put together Create table statement Set default character set
sql += ")";
System.out.println(" The table creation statement is :" + sql);
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate(sql);
conn.commit();
preparedStatement.close();
sql = "insert into " + tableName + "(";
int length = fields.length;
for (int i = 0; i < length; i++) {
sql += fields[i];
// Prevent the last ,
if (i < length - 1) {
sql += ",";
}
}
sql += ") values(";
for (int i = 0; i < length; i++) {
sql += "?";
// Prevent the last ,
if (i < length - 1) {
sql += ",";
}
}
sql += ");";
ps = conn.prepareStatement(sql);
a++;
} else {
String[] data = result.split("\\!\\|");
if (fields.length == data.length) {
// Injection parameters
for (int i = 0; i < fields.length; i++) {
ps.setString(i + 1, data[i]);
}
ps.addBatch();
}
if (a % 5 == 0) {
ps.executeBatch();
conn.commit();
ps.clearBatch();
}
a++;
}
}
ps.executeBatch();
conn.commit();
ps.clearBatch();
// Close database connection
conn.close();
}边栏推荐
- "Introduction to interface testing" punch in to learn day04: how to abstract the procedural test script into a test framework?
- Hal library serial port for note taking
- [leetcode medium] 34. Find the first and last positions of elements in the sorted array - array double pointer
- Function of each layer of data warehouse
- Visio use
- Unity: test rotation function
- Idea configuration
- NC | progress has been made in the study of the ecological network relationship between dissolved organic carbon and microorganisms in the context of global change
- P100 MSSQL database penetration test of secondary vocational network security skills competition
- Task02 | EDA initial experience
猜你喜欢

Import and export using poi

Hal library serial port for note taking

Common methods of array

Define macros in makefile and pass them to source code

Flink1.15 source code reading - Flink annotations

Network security - comprehensive penetration test -cve-2018-10933-libssh maintain access

Unity: test rotation function

Use of stm32cubemonitor Part II - historical data storage and network access

Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development

Merge sort / quick sort
随机推荐
VMware installation
Analysis of DNS domain name resolution process
292. Nim game
Eslint error
Idea configuration
Chrome debugging skills
NVM installation and use
Lombok detailed introduction
Day 9 (capture traffic and routing strategy)
Force button brushing question 61. rotating linked list
P100 MSSQL database penetration test of secondary vocational network security skills competition
Detailed explanation of three factory modes
List type to string type
Visio use
Question B: shunzi date
LeetCode. 302 weekly games___ 03_ 6121. Query the number smaller than k after cutting the number____ sort
PHP record
Function method encapsulation -- mutual conversion of image types qpixmap, qimage and mat
Import and export using poi
Dc-1-practice