2016年11月17日 星期四

Unit test 使用 JNDI

[Need Jar]
ojdbc6.jar
tomcat-juli.jar


[Sample Code]
package com.tv.util.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.junit.Before;
import org.junit.Test;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;

public class JndiConnectionTest {

@Before
public void init() {
try {
// Create initial context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:/comp");
ic.createSubcontext("java:/comp/env");
ic.createSubcontext("java:/comp/env/jdbc");

// Construct DataSource
OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
ds.setURL("jdbc:oracle:thin:@ip:port:sid");
ds.setUser("test");
ds.setPassword("test");

ic.bind("java:/comp/env/jdbc/sid", ds);
} catch (NamingException ex) {
ex.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

@Test
public void simpleConnectionTest() {
Connection con = null;
try {
Context initContext = new InitialContext();
Context webContext = (Context) initContext.lookup("java:/comp/env");

DataSource ds = (DataSource) webContext.lookup("jdbc/sid");
con = ds.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from userprof where userid = '2000131'");
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". BAN:" + rs.getString("BAN") + " User:" + rs.getString("USERID") + " BRIEFNAME:" + rs.getString("BRIEFNAME"));
}
rs.close();
st.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
}
}

2016年2月25日 星期四

javascript replace

javascript replace:

var tel = "#0-9#80-123456#";

var telTemp = tel.replace(/\#|\-/g,"");  //replace 掉 # , -

alert(tel + " , " + telTemp); //#0-9#80-123456# , 0980123456

2016年2月15日 星期一

itext image to pdf 大小設定



Document document = new Document();
String filePath = "D:/J089/J089-workspace/SimpleTest/pdf/temp/tmp.pdf";

PdfWriter.getInstance(document, new FileOutputStream(filePath));

document.open();
Image image1 = Image.getInstance(imgPath);


方法一:

float pageWidth = document.getPageSize().getWidth();
float leftMargin = document.leftMargin();
float rightMargin = document.rightMargin();
float imageWidth = image1.getWidth();

float scaler = (( pageWidth - leftMargin - rightMargin ) / imageWidth ) * 100;

image1.scalePercent(scaler);



方法二:

float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin();
image1.scaleToFit(documentWidth, documentHeight);



document.add(image1);
document.close();