Victor Carvalho

Engenheiro de Software. Focado em arquitetura de aplicações

Example: PLSQL Cursor with parameters updating the current line

Here is a example how to use cursor with parameters in PLSQL accessing each row
of the given table and changing it’s value.

declare
  cursor c_funcionario(p_matricula in number) 
    is (select * from funcionario where matricula = p_matricula) for update;
begin

  for currentFuncionario in c_funcionario(2) loop
    update funcionario set salario = 300 where current of c_funcionario;
  end loop;
  
  commit;
end;

How to create a PACKAGE in oracle

PACKAGE HEADER

create or replace PACKAGE MY_PACK AS 

  /* TODO enter package declarations (types, exceptions, methods etc) here */ 
    
  PROCEDURE PROCEDURE_1(v_texto in out varchar2);
  FUNCTION FUNCTION_1(v_parametroEntrada in varchar2) return varchar2;

END MY_PACK;

PACKAGE BODY

create or replace package body MY_PACK
IS

  PROCEDURE PROCEDURE_1(v_texto in out varchar2) IS 
  BEGIN
    v_texto := 'my return';
  END PROCEDURE_1;
  
  FUNCTION FUNCTION_1(v_parametroEntrada in varchar2) return varchar2 is
  begin
    return v_parametroEntrada;
  end;

END MY_PACK;

How to send an image in email using commons-email lib

import org.apache.commons.mail.HtmlEmail;
...

  // Create the email message
  HtmlEmail email = new HtmlEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "Me");
  email.setSubject("Test email with inline image");
  
  // embed the image and get the content id
  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
  String cid = email.embed(url, "Apache logo");
  
  // set the html message
  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

  // set the alternative message
  email.setTextMsg("Your email client does not support HTML messages");

  // send the email
  email.send();

Visit the official website: https://commons.apache.org/proper/commons-email/userguide.html

How to get as String the full stack of a exception in your application

<dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.2</version>
</dependency>
//sample of code
try {
     List<Veiculo> result = veiculoService.findAllVeiculos();
 
     List<Veiculo> listaNova = null;
 
     listaNova.add(result.get(0));
     listaNova.add(result.get(1));
     listaNova.add(result.get(2));
 
     msg.reply(Json.encode(listaNova));

 } catch (Exception e) { 
     String msgError = ExceptionUtils.getFullStackTrace(e);

     msg.fail(MessageCode.INTERNAL_SERVER_ERROR, msgError);
 }

Load properties from property file to spring.xml file

datasource.properties

datasource.driverClass=oracle.jdbc.driver.OracleDriver

datasource.jdbcUrl=jdbc:oracle:thin:@10.0.1.100:1521:xe
datasource.user=user
datasource.password=password
datasource.redis.address=127.0.0.1
datasource.redis.port=6379

c3p0.minPoolSize=5
c3p0.maxPoolSize=20	    
c3p0.acquireIncrement=1
c3p0.maxStatements=50
c3p0.idleConnectionTestPeriod=3000
c3p0.loginTimeout=300

spring.xml

<!-- load properties from property file -->
<context:property-placeholder location="classpath:datasource.properties" />

<!-- using properties from property file -->
  <bean id="myContactDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <!-- Connection properties -->
      <property name="driverClass" value="${datasource.driverClass}" />
      <property name="jdbcUrl" value="${datasource.jdbcUrl}" />
      <property name="user" value="${datasource.user}" />
      <property name="password" value="${datasource.password}" />
      
      <!-- Pool properties -->
      <property name="minPoolSize" value="${c3p0.minPoolSize}" />
      <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
      <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
      <property name="maxStatements" value="${c3p0.maxStatements}" />
      <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
      <property name="loginTimeout" value="${c3p0.loginTimeout}" />
  </bean>

How to run a background task using Spring Framework ThreadPoolTaskExecutor

Sometimes we need to execute a task in background in our application
We can use the class ThreadPoolTaskExecutor of Spring Framework to do that

To do it work we just need declare this bean in spring.xml config file as below

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.thread.ThreadPoolTaskExecutor">
  <property name="corePoolSize" value="5000" />
  <property name="maxPoolSize" value="5000" />
  <property name="queueCapacity" value="5000" />
</bean>
public class ReportJob {
  
  @Autowired
  @Qualifier("taskExecutor")
  private ThreadPoolTaskExecutor taskExecutor;
  
  public void startReport() throws IOException{
    taskExecutor.execute(); <- your thread to be executed here
  }
}

Main properties

maxPoolSize – The max number of threads in the pool

corePoolSize – The current size of thread pool

queueCapacity – If all threads are executing in corePoolSize and the size of corePoolSize is equal to maxPoolSize the task will be allocated in the queue until a thread to be free. If you exceed the queueCapacity the task will be ignored

Detect the standard time, the daylight saving time for the time zone

I was looking for a way how to detect the Dayligth Saving Time in JavaScript and I have found the following solution

if (timezonedetect.hasDaylightSavingTime()) {
  console.log('You have daylight saving time in your timezone');
} else {
  console.log('You DO NOT have daylight saving time in your timezone');
}

console.log('Your standard time zone offset in second is ' + jsk.tz.standardTime());
console.log('Your standard time zone offset is : ' + jsk.tz.standardTimeToString());

Visit the github project url to see the full documentation and download: https://github.com/dsimard/timezonedetect

How to generate a zip file from a List of File

public static void addToZipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
  FileInputStream fis = new FileInputStream(file);
  ZipEntry zipEntry = new ZipEntry(file.getName());
  zos.putNextEntry(zipEntry);

  byte[] bytes = new byte[1024];
  int length;
  while ((length = fis.read(bytes)) >= 0) {
    zos.write(bytes, 0, length);
  }

  zos.closeEntry();
  fis.close();
}
FileOutputStream fos = new FileOutputStream(C:\myzipfile.zip);
ZipOutputStream zos = new ZipOutputStream(fos);

for(File f : filesToAdd){
  Util.addToZipFile(f, zos);
}

zos.close();
fos.close();