Using Oracle Auto-Increment/Sequence in Hibernate/MyBatis

Categories: Java; Tagged with: ; @ May 1st, 2013 23:41

(Hibernate) Error 1 : java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

Solution:

Specify the sequence:

package com.liguoliang.jee.vo;

/**
 * Represents Client.
 * @author Guoliang
 *
 */
@Entity
@Table (name="Client")
public class Client {
	
	@Id
	@Column(name="id")
	@SequenceGenerator(name="my_seq", sequenceName="CLIENT_ID_SEQUENCE")
	@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="my_seq")
	private int id;
	
	@Column(name="name")
	String name;
	
	public Client() {
		// TODO Auto-generated constructor stub
	}
//....

}

(MyBatis) Error 2: java.sql.SQLException: Invalid column type: getInt not implemented for class oracle.jdbc.driver.T4CRowidAccessor

Code before change:

	/**
	 * insert new client.
	 * @param client
	 */
	
	@Insert("INSERT INTO CLIENT(name) VALUES (#{name})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void insert(Client client);

Solutions:

1. Directly Remove @Options(useGeneratedKeys=true, keyProperty=”id”)

2. Replace @Options with @SelectKey:

	/**
	 * insert new client.
	 * @param client
	 */
	
	@Insert("INSERT INTO CLIENT(name) VALUES (#{name})")
	@SelectKey(
			keyProperty = "id",
			before = true,
			resultType = Integer.class,
			statement={" select CLIENT_ID_SEQUENCE.nextval AS id from dual"}
			)
	void insert(Client client);

Maven: modify the default JDK 修改Maven默认JDK

Categories: Development Notes; Tagged with: ; @ May 1st, 2013 11:18

I created a maven java project, and I want to use some new feature in Java7, but every time, after update the project, the JRE will be changed to 1.5 automatically.

After googling, got the solution: configure maven-compiler-plugin:

  <build>
    <finalName>J2EE</finalName>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
    </plugins>
  </build>

 

http://stackoverflow.com/questions/3539139/what-causes-a-new-maven-project-in-eclipse-to-use-java-1-5-instead-of-java-1-6-b

Oracle 11 XE & JDBC

Categories: Java; Tagged with: ; @ May 1st, 2013 0:51

How to get Oracle 11 XE for Windows 64?

Download the Oracle 11 XE 32 bit and install it.

No problem for me ( Win7 64)

Forgot the sys password, how to reset?

Conn / as sysdba

passw system

input new password.

[http://rolfje.wordpress.com/2007/01/16/lost-oracle-sys-and-system-password/]

How to get the version of Oracle?

select * from v$version

How to Add Oracle JDBC Driver in Loacl Maven Repo?

mvn install:install-file -Dfile=C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
then add modify your pom:

<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc6</artifactId>
	<version>11.2.0</version>
</dependency>
[http://www.mkyong.com/maven/how-to-add-oracle-jdbc-driver-in-your-maven-local-repository/]

Quick test using JDBC

	public static void main(String[] args) throws Throwable {
		
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection connection = null;
		connection = DriverManager.getConnection(
			"jdbc:oracle:thin:@localhost:1521:xe","root","root");
		System.out.println(connection.getMetaData().getDatabaseMajorVersion());
		connection.close();
	}

Update Log4j Appender at runtime

Categories: Java; Tagged with: ; @ April 17th, 2013 18:20
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

/**
 * Test for adding appender to log4j at runtime.
 * @author SGSCLGLD
 *
 */
public class LogingTest {
	
	static Logger log = Logger.getLogger(LogingTest.class);
	
	public static void main(String[] args) throws IOException {
		// setting up a FileAppender at runtime.
		PatternLayout patternLayout = new PatternLayout();
		patternLayout.setConversionPattern("[%d{d/M/yyyy HH:mm:ss}] %p [%t] %c{1}.%M(%L) | %m%n");
		FileAppender appender = new FileAppender(patternLayout,"D:\\ME\\WS_Share\\test1\\log.txt",false);    
		log.addAppender(appender);
		
		log.info("Hello world...");
	}
}

Java RSA Key Generation/Encryption/Decryption RSA秘钥生成/加密/解密

Categories: Java; Tagged with: ; @ April 3rd, 2013 0:17

Requirement:

Generate RSA key pair using Java,  and test encryption/decryption.

Source code:

package com.rsa;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

public class RSATest {

	/**
	 * Generate key pair and encrypt/decrypt message.
	 * @param args
	 * @throws Throwable
	 */
	public static void main(String[] args) throws Throwable {

		String str = "liguoliang.com";
		
		// init private/public key
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
		keyGen.initialize(512);
		KeyPair keyPair = keyGen.generateKeyPair();
		PrivateKey privateKey = keyPair.getPrivate();
		PublicKey publicKey = keyPair.getPublic();

		System.out.println("privateKey: " + privateKey);
		System.out.println("PublicKey: " + publicKey);

		System.out.println("Original message: [" + str + "]");
		
		// init cipher
		Cipher cipher;
		cipher = Cipher.getInstance("RSA");
		
		// Encrypt
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] encryptedMsg = cipher.doFinal(str.getBytes());
		System.out.println("Encrypted message: [" + new String(encryptedMsg) + "]");

		// Decrypt
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] decryptedMsg = cipher.doFinal(encryptedMsg);
		System.out.println("Decrypted message: [" + new String(decryptedMsg) + "]");
	}
}

Output:

privateKey: Sun RSA private CRT key, 512 bits
  modulus:          6722431852833652083740039
  public exponent:  65537
  private exponent: 171186757391917267536..695551529001
  prime p:          9879891158983359366387147661330…7700131643
  prime q:          680415577930859..89085598145526159671701009583973
  prime exponent p: 320741816528876..9797652301875289
  prime exponent q: 1025757344699..140192255455019921532401
  crt coefficient:  60972956628933..8290909983925983303
PublicKey: Sun RSA public key, 512 bits
  modulus: 672243185283365208374001..
  public exponent: 65537
Original message: [liguoliang.com]
Encrypted message: ['ËŸò¬Œ÷Já°`ð«6L´aZ0޶¹mçZaÐvCóG%P    ƒtÂÌÖ9ØùýY,vÎ`¦Y»ù    h]
Decrypted message: [liguoliang.com]

Reference

RSA Encryption in Java – http://tspycher.com/2012/10/rsa-encryption-in-java/

用实例给新手讲解RSA加密算法 – http://bank.hexun.com/2009-06-24/118958531.html

http://introcs.cs.princeton.edu/java/78crypto/RSA.java.html

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.