how to copy data from file to PostgreSQL using JDBC?

This works… import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import org.postgresql.copy.CopyManager; import org.postgresql.core.BaseConnection; public class PgSqlJdbcCopyStreamsExample { public static void main(String[] args) throws Exception { if(args.length!=4) { System.out.println(“Please specify database URL, user, password and file on the command line.”); System.out.println(“Like this: jdbc:postgresql://localhost:5432/test test password file”); } else { System.err.println(“Loading driver”); Class.forName(“org.postgresql.Driver”); System.err.println(“Connecting to ” + args[0]); … Read more

Using a prepared statement and variable bind Order By in Java with JDBC driver

Placeholders ? can only be used for parameter values but not with column and sort order directions. So the standard way to do this as is pointed e.g. here is to use String#format() or something similar to append your column name and order value to your query. Another option is to use Spring Data JPA … Read more

How to set up datasource with Spring for HikariCP?

you need to write this structure on your bean configuration (this is your datasource): <bean id=”hikariConfig” class=”com.zaxxer.hikari.HikariConfig”> <property name=”poolName” value=”springHikariCP” /> <property name=”connectionTestQuery” value=”SELECT 1″ /> <property name=”dataSourceClassName” value=”${hibernate.dataSourceClassName}” /> <property name=”maximumPoolSize” value=”${hibernate.hikari.maximumPoolSize}” /> <property name=”idleTimeout” value=”${hibernate.hikari.idleTimeout}” /> <property name=”dataSourceProperties”> <props> <prop key=”url”>${dataSource.url}</prop> <prop key=”user”>${dataSource.username}</prop> <prop key=”password”>${dataSource.password}</prop> </props> </property> </bean> <!– HikariCP configuration –> <bean … Read more

Spring JDBC Template for calling Stored Procedures

There are a number of ways to call stored procedures in Spring. If you use CallableStatementCreator to declare parameters, you will be using Java’s standard interface of CallableStatement, i.e register out parameters and set them separately. Using SqlParameter abstraction will make your code cleaner. I recommend you looking at SimpleJdbcCall. It may be used like … Read more

Error when connect to impala with JDBC under kerberos authrication

Forget about the Hadoop UGI: a JDBC driver just needs the raw JAAS configuration to create a Kerberos ticket on-the-fly (with useKeyTab raised and useTicketCache lowered). System properties java.security.krb5.conf => (optional) non-defaut Kerberos conf java.security.auth.login.config => JAAS config file javax.security.auth.useSubjectCredsOnly => must be forced to “false” (the default has changed in some Java release, duh) … Read more

How to execute IN() SQL queries with Spring’s JDBCTemplate effectively?

You want a parameter source: Set<Integer> ids = …; MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue(“ids”, ids); List<Foo> foo = getJdbcTemplate().query(“SELECT * FROM foo WHERE a IN (:ids)”, parameters, getRowMapper()); This only works if getJdbcTemplate() returns an instance of type NamedParameterJdbcTemplate

tech