当前位置:网站首页>[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();
}边栏推荐
- Sword finger offer II 041. Average value of sliding window_____ Using queue / loop array implementation
- Unity: test rotation function
- Task02 | EDA initial experience
- Riotboard development board series notes (4) -- using Vpu hardware decoding
- Use of stm32cubemonitor Part II - historical data storage and network access
- Detailed explanation of three factory modes
- Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development
- List type to string type
- P100 MSSQL database penetration test of secondary vocational network security skills competition
- Print the common part of two ordered linked lists
猜你喜欢

Import and export using poi

Force the resumption of game 302 of the week

Use of CCleaner

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

How does Jupiter notebook change themes and font sizes?

Bubble sort / heap sort

B. Almost Ternary Matrix

04 -- two ways of writing el and data

How chemical enterprises choose digital service providers with dual prevention mechanism

Force deduction brush question 26. Delete duplicates in the ordered array
随机推荐
Vscode copy synchronization plug-in expansion
Use of stm32cubemonitor part I - data plotting and instrument display
Consistent hash, virtual node, bloom filter
Question D: pruning shrubs
mysql_ Create temporary table
Table of contents of force deduction questions
Easyexcel sets the style of the last row [which can be expanded to each row]
Dc-2-range practice
Idea configuration
Advantages and disadvantages of zero trust security
Openlayers ol ext: Transform object, rotate, stretch, zoom in
Canvas record
ECMAScript new features
Page performance: how to optimize pages systematically?
Merge sort / quick sort
10. 509 Certificate (structure + principle)
mysql_ Backup restore_ Specify table_ Backup table_ Restore table_ innobackup
JS method encapsulation summary
Implementation principle of virtual DOM
Openlayers draw circles and ellipses