Skip to content

Commit 2b0ed3f

Browse files
committed
csv to database
0 parents  commit 2b0ed3f

7 files changed

Lines changed: 141 additions & 0 deletions

File tree

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>CSVtoDatabase</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
18+
<dependency>
19+
<groupId>mysql</groupId>
20+
<artifactId>mysql-connector-java</artifactId>
21+
<version>8.0.25</version>
22+
</dependency>
23+
24+
</dependencies>
25+
26+
</project>

src/main/java/CSVConsume.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.BufferedReader;
2+
import java.io.FileReader;
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.PreparedStatement;
6+
7+
import static java.lang.Integer.parseInt;
8+
9+
public class CSVConsume {
10+
public static void main(String[] args) {
11+
String jdbcUrl="jdbc:mysql://localhost:3306/ems";
12+
String username="root";
13+
String password="";
14+
15+
String filePath="C:\\Users\\laser\\Desktop\\data.csv";
16+
17+
int batchSize=20;
18+
19+
Connection connection=null;
20+
21+
22+
try{
23+
connection= DriverManager.getConnection(jdbcUrl,username,password);
24+
connection.setAutoCommit(false);
25+
26+
String sql="insert into employee(id,name,address,salary) values(?,?,?,?)";
27+
28+
PreparedStatement statement=connection.prepareStatement(sql);
29+
30+
BufferedReader lineReader=new BufferedReader(new FileReader(filePath));
31+
32+
String lineText=null;
33+
int count=0;
34+
35+
lineReader.readLine();
36+
while ((lineText=lineReader.readLine())!=null){
37+
String[] data=lineText.split(",");
38+
39+
String id=data[0];
40+
String name=data[1];
41+
String address=data[2];
42+
String salary=data[3];
43+
44+
statement.setInt(1,parseInt(id));
45+
statement.setString(2,name);
46+
statement.setString(3,address);
47+
statement.setInt(4,parseInt(salary));
48+
statement.addBatch();
49+
if(count%batchSize==0){
50+
statement.executeBatch();
51+
}
52+
}
53+
lineReader.close();
54+
statement.executeBatch();
55+
connection.commit();
56+
connection.close();
57+
System.out.println("Data has been inserted successfully.");
58+
59+
}
60+
catch (Exception exception){
61+
exception.printStackTrace();
62+
}
63+
64+
}
65+
}

target/classes/CSVConsume.class

2.43 KB
Binary file not shown.

0 commit comments

Comments
 (0)