Skip to content

Instantly share code, notes, and snippets.

@testpilot031
Last active April 21, 2023 07:07
Show Gist options
  • Save testpilot031/df6977d10f9bfbe9104876bbfb8a68f5 to your computer and use it in GitHub Desktop.
Save testpilot031/df6977d10f9bfbe9104876bbfb8a68f5 to your computer and use it in GitHub Desktop.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
public class DatabaseInserter {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/your_database";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password";
public static void main(String[] args) {
List<HashMap<String, String>> data = fetchData(); // Replace this with the method that fetches the data
insertDataIntoDatabase(data);
}
private static void insertDataIntoDatabase(List<HashMap<String, String>> data) {
try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD)) {
for (HashMap<String, String> row : data) {
String sql = "INSERT INTO your_table_name (column1, column2, column3) VALUES (?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, row.get("Header1"));
preparedStatement.setString(2, row.get("Header2"));
preparedStatement.setString(3, row.get("Header3"));
preparedStatement.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment