Skip to content

Instantly share code, notes, and snippets.

View kasramp's full-sized avatar
:octocat:
Chasing bugs

Kasra Madadipouya kasramp

:octocat:
Chasing bugs
View GitHub Profile
#! /bin/sh
""":"
exec python $0 ${1+"$@"}
"""
# This file is part of indicator-weather.
# Indicator Weather is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
@kasramp
kasramp / Option-5.java
Last active July 4, 2023 12:23
Option-5 (Unnest insertion)
public void insertNames(List<String> names) {
try (Connection connection = config.getConnection()) {
PreparedStatement statement = connection.prepareStatement("INSERT INTO student (name) SELECT * FROM UNNEST(?)");
statement.setArray(1, connection.createArrayOf("text", names.toArray(new String[names.size()])));
statement.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@kasramp
kasramp / Option-3.java
Last active July 4, 2023 09:31
Option-3 (Batch insertion)
public void insertStudents(List<Student> students) {
String statement = "INSERT INTO student VALUES(?,?,?)";
try (Connection connection = config.getConnection()) {
try (PreparedStatement ps = connection.prepareStatement(statement)) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
int j = 0;
ps.setString(++j, student.getGuid());
ps.setString(++j, student.getName());
ps.setInt(++j, student.getId());
@kasramp
kasramp / Option-2.java
Last active July 4, 2023 07:45
Option-2 (Single insert with keeping a connection alive for all insertion)
public void insertStudents(List<Student> students) {
String statement = "INSERT INTO student VALUES(?,?,?)";
try (Connection connection = config.getConnection()) {
try (PreparedStatement ps = connection.prepareStatement(statement)) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
int j = 0;
ps.setString(++j, student.getGuid());
ps.setString(++j, student.getName());
ps.setInt(++j, student.getId());
@kasramp
kasramp / Option-1.java
Last active July 4, 2023 07:25
Option-1 (Single insert with making connection for each insert)
public void insertStudents(List<Student> students) {
String statement = "INSERT INTO student VALUES(?,?,?)";
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
try (Connection connection = config.getConnection()) {
try (PreparedStatement ps = connection.prepareStatement(statement)) {
int j = 0;
ps.setString(++j, student.getGuid());
ps.setString(++j, student.getName());
ps.setInt(++j, student.getId());
@kasramp
kasramp / Option-4.java
Last active July 4, 2023 12:00
Option-4 (Multi rows batch insertion)
public void insertStudents(List<Student> students) {
String initialInsertStatement = "INSERT INTO student VALUES";
StringBuilder query = new StringBuilder(initialInsertStatement);
try (Connection connection = config.getConnection()) {
try (Statement statement = connection.createStatement()) {
for (int i = 0; i < students.size(); i++) {
if (i == 0) {
query.append(asSqlQuery(students.get(i)));
} else if (i % 200 != 0) {
query.append(",")
@kasramp
kasramp / Pom.xml
Created January 30, 2016 16:05
Maven dependency for Tomcat JDBC Connection Pool
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.0.30</version>
</dependency>
@kasramp
kasramp / DataSource.java
Last active July 8, 2023 21:26
JDBC connection pool class
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
public class DataSourceConfig {
public static final String DATA_SOURCE_CLASS_NAME = "org.postgresql.Driver";
public static final String DATA_SOURCE_URL = "jdbc:postgresql://%s:%s/%s";
public static final String JDBC_INTERCEPTORS = "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer";
@kasramp
kasramp / SampleProgram.java
Last active July 8, 2023 21:17
JDBC connection pool test connection
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SampleProgram {
public static void main(String[] args) throws SQLException {
DataSourceConfig ds = configureConnection();
String statement = "SELECT id FROM test_tbl";
try (Connection con = ds.getConnection()) {
@kasramp
kasramp / Star.cpp
Last active April 18, 2016 23:17
Star
#include<iostream>
using namespace std;
int main (int args, char *argv[]) {
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
cout << "*";
}
for (int j = 0; j <= ((i + 1) * 2); j++) {
cout << " ";
}