顯示具有 java 標籤的文章。 顯示所有文章
顯示具有 java 標籤的文章。 顯示所有文章

2015年4月20日 星期一

Java - Oracle 連線使用 tnsname

Java - Oracle 連線使用 tnsname

web server 必需安裝 oracle client,然後設定好 tnsnames.ora
<tnsname> =
 (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(Host = <hostname>)(Port = <port>))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = <sid>)
 )
)

connection string 中的 url 由原本的 jdbc:oracle:thin:@<hostname>:<port>:<sid>
改成 jdbc:oracle:thin:@<tnsname>

然後要讓程式知道 tnsnames.ora 的位置,有二個方法:
1. java 程式在資料庫 open session 前設定系統變數
System.setProperty("oracle.net.tns_admin", "D:/oracle/product/10.2.0/client_1/NETWORK/ADMIN");

2. 將設定加入 tomcat7w 或是啟動 sh 中
-Doracle.net.tns_admin=D:/oracle/product/10.2.0/client_1/NETWORK/ADMIN

2013年9月27日 星期五

javax.xml.ws.WebServiceException: 無法建立 JAXBContext

使用 web service 時如果有 throw exception 的話,會遇到 "javax.xml.ws.WebServiceException: 無法建立 JAXBContext" 的錯誤

java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.lang.StackTraceElement does not have a no-arg default constructor.

把 throw exception 拿掉,改用 try..catch 就可以了

2013年8月30日 星期五

response.setHeader 下載檔名遇到中文字的解決方法


response.setContentType("application/x-x509-ca-cert");
response.setContentLength(byteCert.length);
// caname 是中文字
// Safari & Firefox & Chrome
String fileName = new String(caname.getBytes(), "ISO8859-1");

String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) {
    // IE
    fileName = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", "%20");
}

response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".cer\"");

2013年8月23日 星期五

解決 Linux 主機使用 KeyStore 儲存 pfx 時非常慢的問題


在啟動的 shell script 中 (catalina.sh) 加上下列指令

export JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"

2013年8月22日 星期四

從 web service 取得 ServletContext 物件

// ServletContextListener Class


public void contextInitialized(ServletContextEvent sce) {
  try {
    sce.getServletContext().setAttribute("myBook", "Learning java");
  } catch (Exception ex) {
  }
}


// WebService


@Resource
WebServiceContext wsContext;

@WebMethod(operationName = "test1")
public String test1() {
  ServletContext servletContext = (ServletContext) wsContext.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
  String myBook = (String) servletContext.getAttribute("myBook");
}

2010年9月21日 星期二

java 編譯時出現 "unchecked call to put(K,V)..." 的警告訊息

程式碼 Map result = new HashMap(); 在編譯時出現以下的警告訊息
warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map

解決方法,將程式碼改成以下的方式
Map<Object,Object> result = new HashMap<Object,Object>();

2006年3月24日 星期五

使用 jdbc 連結 MS SQL2000


try {
  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
  String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbname";
  Connection con = DriverManager.getConnection(url, "userid", "password");
  Statement stmt = con.createStatement(1004, 1008);
} catch(Exception e) {
  e.printStackTrace();
}

必要的 JAR 檔來源:到 Microsoft Download Center
輸入關鍵字 jdbc 找 SQL Server 2000 Driver for JDBC Service Pack 3 下載安裝
將安裝目錄下的三個 jar (msbase.jar、mssqlserver.jar、msutil.jar) 放到所需的 lib 目錄下即可。