Java Date Time

Development/Java 2009. 2. 26. 13:16
참조: http://docstore.mik.ua/orelly/java-ent/jnut/ch04_04.htm
Posted by zennken
Runtime r = Runtime.getRuntime();
r.gc();

참조: http://www.devdaily.com/java/edu/pj/pj010008/pj010008.shtml
Posted by zennken

Java Reflection

Development/Java 2009. 2. 19. 21:43
Trail: The Reflection API: Table of Contents
http://java.sun.com/docs/books/tutorial/reflect/TOC.html

Summary: Loading class, Instantiate an object, Invoking method
// Loading class
URLClassLoader urlClassLoader = new URLClassLoader(new URL[], file.toURL());
Class sClass = urlClassLoader.loadClass("ClassName");

// Instantiate an object
SpecificObjectType obj = sClass.newInstance();

// Invoking method
//do something: return type of "MethodName" is String in this example
Method method = sClass.getMethod("MethodName", args types ...);
result = (String) method.invoke(obj, args...);
Posted by zennken
도구 -> 장번호 매기기 -> 번호 매기기(탭) -> 수준과 단락 스타일 설정 (번호는 없음으로 선택)
Posted by zennken

출처: http://www.exampledepot.com/egs/javax.crypto/DesFile.html

두 프로그램간의 암호화된 XML 파일교환을 위하여 출처의 파일(DES기반)을 PBE기반으로 바꾸었다.

사용법은 보내는 쪽과 받는 쪽, 그리고 암호화 클래스(DesEncrypter)로 구성되어 있다.

이때 XML파일의 인코딩은 UTF-8을 사용한다.


보내는 쪽 (String을 파일에 암호화 시켜 저장) 
         String FILENAME = "test.xml";
         String str2Encryption = "aaaa";

         DesEncrypter encrypter = DesEncrypter.getInstance();
         
         ByteArrayInputStream in = new ByteArrayInputStream(str4Encryption .getBytes());
         
         encrypter.encrypt(in, new FileOutputStream(FILENAME));
            
         in.close();
받는 쪽 (암호화 된 파일을 풀어서 스트링으로 받기)
         String FILENAME = "test.xml";

         DesEncrypter decrypter = DesEncrypter.getInstance();
         
         ByteArrayOutputStream out = new ByteArrayOutputStream();

         decrypter.decrypt(new FileInputStream(FILENAME),out);
         
         String result = new String(out.toByteArray());

         out.close();     

PBE기반 암호화/복호화 클래스

// Singletone
public class DesEncrypter
{
 private static DesEncrypter INSTANCE;
 
 private final static String ENCRYPTION_SALT = "abcdefgh"; // must be 8 bytes
 private final static String ENCRYPTION_PASSWORD = "password";
 private final static String ENCRYPTION_ALGORITHM = "PBEWithMD5AndDES";
 private final static int  ENCRYPTION_ITERATION = 20;

    private Cipher ecipher;
    private Cipher dcipher;
 
 public static DesEncrypter getInstance()
 {
  if (INSTANCE == null)
  {
   INSTANCE = new DesEncrypter();
  }
  return INSTANCE;
 }
   
    private DesEncrypter() {
        // Create an 8-byte initialization vector
  PBEKeySpec    pbeKeySpec;
  PBEParameterSpec  pbeParameterSpec;
  SecretKeyFactory  secretKeyFactory;
  SecretKey    secretKey;

        try {
          
   pbeParameterSpec = new PBEParameterSpec(ENCRYPTION_SALT.getBytes(), ENCRYPTION_ITERATION);
   
   pbeKeySpec = new PBEKeySpec(ENCRYPTION_PASSWORD.toCharArray());

   secretKeyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
   
   secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
   
   dcipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
   ecipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
   
   dcipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);
   ecipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
           
        } catch (java.security.InvalidAlgorithmParameterException e) {
        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        } catch (InvalidKeySpecException e) {
  }
    }

    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[1024];

    public void encrypt(InputStream in, OutputStream out) {
        try {
            // Bytes written to out will be encrypted
            out = new CipherOutputStream(out, ecipher);

            // Read in the cleartext bytes and write to out to encrypt
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            out.close();
        } catch (java.io.IOException e) {
        }
    }

    public void decrypt(InputStream in, OutputStream out) {
        try {
            // Bytes read from in will be decrypted
            in = new CipherInputStream(in, dcipher);

            // Read in the decrypted bytes and write the cleartext to out
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            //out.close();
        } catch (java.io.IOException e) {
        }
    }
}



Posted by zennken

The Java Virtual Machine Specification

SDN Home > Products & Technologies > Java Technology > Java Platform,
Standard Edition (Java SE) >
http://java.sun.com/docs/books/jvms/index.html


Inside the Java Virtual Machine
http://www.artima.com/insidejvm/ed2/index.html



JVM Resource
http://www.artima.com/insidejvm/resources/index.html


Posted by zennken
출처: http://java.sun.com/products/jdbc/reference/faqs/index.html

4. How do I start debugging problems related to the JDBC API?

A good way to find out what JDBC calls are doing is to enable JDBC tracing. The JDBC trace contains a detailed listing of the activity occurring in the system that is related to JDBC operations.

If you use the DriverManager facility to establish your database connection, you use the DriverManager.setLogWriter method to enable tracing of JDBC operations.

If you use a DataSource object to get a connection, you use the DataSource.setLogWriter method to enable tracing. (For pooled connections, you use the ConnectionPoolDataSource.setLogWriter method, and for connections that can participate in distributed transactions, you use the XADataSource.setLogWriter method.)

Posted by zennken

JDBC Row Count

Development/Java 2008. 11. 28. 10:45

출처: http://java.sun.com/products/jdbc/reference/faqs/index.html

18. There is a method getColumnCount in the JDBC API. Is there a similar method to find the number of rows in a result set?

No, but it is easy to find the number of rows. If you are using a scrollable result set, rs, you can call the methods rs.last and then rs.getRow to find out how many rows rs has. If the result is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.

Posted by zennken

Summary of regular-expression constructs: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

package regExpression;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class regExTest
{
   
    public static void main(String[] args)
    {
        int count = 3;
        int i, pos, from, to;
       
        String[] texts = new String[count];
        String regEx = "%[a-zA-Z]+\\w*%";
        String matchedString;
       
        Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher;
       
        texts[0] = "-=+";
        texts[1] = "%abc%--- abc --- %ba%a a123-";
        texts[2] = "a%a23456789_%";

        for (i = 0; i < count; i++)
        {
            pos = 0;
           
            matcher = pattern.matcher(texts[i]);

            System.out.println("Original String: " + texts[i]);

            while(matcher.find(pos))
            {
                matchedString = matcher.group();

                pos = matcher.end();
               
                from = matcher.start();
                to = pos -1;

                System.out.println("Matched String: " + matchedString + " (" + from  + " ~ " + to + ")");

            }
           
            System.out.println("");
        }
    }
}



실행 결과값
Original String: -=+

Original String: %abc%--- abc --- %ba%a a123-
Matched String: %abc% (0 ~ 4)
Matched String: %ba% (17 ~ 20)

Original String: a%a23456789_%
Matched String: %a23456789_% (1 ~ 12)

Posted by zennken

ALTIBASE
jdbc:Altibase://<address>:<port_number>/<database_name>
dirver file name: Altibase.jar

ORACLE
jdbc:oracle:<drivertype>:<username/password>@<database>
<database>:
* //<host>:<port>/<service>
* <host>:<port>:<SID>
* <TNSName>

String url = "jdbc:oracle:thin:@192.168.3.38:" + port + ":orcl";
Default port: 1521

SID: select instance from v$thread;
DB Name: select name from v$database;

DB2
jdbc:db2://[serverName][:port][/DBName]
Default port 50000

The IBM® Data Server Driver for JDBC and SQLJ
--------------------------------------------------------------------------------------------
Driver package name  Level of JDBC support  Minimum level of SDK for Java required
--------------------------------------------------------------------------------------------
db2jcc.jar and sqlj.zip JDBC 3.0 and earlier  1.4.2
db2jcc4.jar and sqlj4.zip1  JDBC 4.0 and earlier  6

MS-SQL
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
port: 1433
sqljdbc.jar

MySQL
jdbc:mysql://<host_name>:<port_number>/<database_name>
Port: 3306

Posted by zennken
123

zennken

달력