Last active
March 8, 2025 15:42
-
-
Save unclebean/3fa828517546906c47d3e7c5750c0bb6 to your computer and use it in GitHub Desktop.
csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.opencsv.bean.CsvToBean; | |
import com.opencsv.bean.CsvToBeanBuilder; | |
import java.io.FileReader; | |
import java.io.Reader; | |
import java.util.List; | |
public class CsvReaderService { | |
public static List<Person> readCsv(String filePath) { | |
try (Reader reader = new FileReader(filePath)) { | |
CsvToBean<Person> csvToBean = new CsvToBeanBuilder<Person>(reader) | |
.withType(Person.class) | |
.withIgnoreLeadingWhiteSpace(true) | |
.withSeparator(',') // Define separator if needed | |
.build(); | |
return csvToBean.parse(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static void main(String[] args) { | |
List<Person> people = readCsv("data.csv"); | |
people.forEach(System.out::println); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring: | |
datasource: | |
url: jdbc:postgresql://localhost:5432/mydatabase | |
username: myuser | |
password: mypassword | |
driver-class-name: org.postgresql.Driver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.sql.DataSource; | |
import org.jdbi.v3.core.Jdbi; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class JdbiConfig { | |
@Bean | |
public Jdbi jdbi(DataSource dataSource) { | |
return Jdbi.create(dataSource); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.opencsv.bean.CsvBindByPosition; | |
public class Person { | |
@CsvBindByPosition(position = 0) // Maps to the first column | |
private int id; | |
@CsvBindByPosition(position = 1) // Maps to the second column | |
private String name; | |
@CsvBindByPosition(position = 2) // Maps to the third column | |
private int age; | |
// Getters and Setters | |
public int getId() { return id; } | |
public void setId(int id) { this.id = id; } | |
public String getName() { return name; } | |
public void setName(String name) { this.name = name; } | |
public int getAge() { return age; } | |
public void setAge(int age) { this.age = age; } | |
@Override | |
public String toString() { | |
return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.jdbi</groupId> | |
<artifactId>jdbi3-core</artifactId> | |
<version>3.41.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jdbi</groupId> | |
<artifactId>jdbi3-sqlobject</artifactId> | |
<version>3.41.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jdbi</groupId> | |
<artifactId>jdbi3-postgres</artifactId> | |
<version>3.41.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>42.7.1</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment