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
@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 / 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-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 / 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());
# Hello, and welcome to makefile basics.
#
# You will learn why `make` is so great, and why, despite its "weird" syntax,
# it is actually a highly expressive, efficient, and powerful way to build
# programs.
#
# Once you're done here, go to
# http://www.gnu.org/software/make/manual/make.html
# to learn SOOOO much more.
@kasramp
kasramp / openbsd_vbox_resolution.sh
Last active September 1, 2022 12:59
Set OpenBSD screen resolution on Virtualbox
#!/bin/sh
# inspired by https://www.tumfatig.net/20190131/customized-resolution-for-openbsd-in-virtualbox/
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
mkdir -p /etc/X11/xorg.conf.d
@kasramp
kasramp / host_vbox_custom_resolution.sh
Last active September 1, 2022 12:59
Set screen resolution on Virtualbox (host machine)
#!/bin/sh
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Pass VBox machine name and screen resolution"
echo "Example: 'OpenBSD New' '1920x1080x32'"
exit 1
fi
VBoxManage setextradata "$1" CustomVideoMode1 "$2"