2007年10月5日 星期五

StringUtils 使用subStringBetween

package com.test;
import org.apache.commons.lang.StringUtils;

/**
*
* @author macbook
*/
public class splitString {


public String substringBetween(String var,String open ,String close,String d4 ){
return StringUtils.isBlank(StringUtils.substringBetween(var, open, close))?d4:StringUtils.substringBetween(var, open, close);
}

public String splitLeft(String var){
return StringUtils.substringBetween(var, "(", ",");
}


public String splitLeft(String var ,String d4){
return StringUtils.isBlank(this.splitLeft(var))?d4:this.splitLeft(var);
}

public static void main(String[] arg){
splitString s = new splitString();
System.out.println(s.splitLeft("(,)", "--"));
System.out.print(s.substringBetween("(20070401,)", ",", ")", "--"));

}
}

2007年10月2日 星期二

使用 dbcp 做 connection pool(下)

實做connectionManager

/**
* 2007-08-20 create by jimmy
*
*/
package com.infinitiessoft.sql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.sql.*;
import org.apache.commons.dbcp.BasicDataSource;


/**
*
* @author jimmy
*/
public class ConnectionManager {

public static final String JDBC_MYSQL = "jdbc/mysql";
public static final String JDBC_ORCALE = "jdbc/orcale";
public String JDBC_TYPE;
public static DataSource dataSource = null;
public String connectionStatus = "";

public ConnectionManager(){
this.init();
}
public ConnectionManager(String type) {

if(type.equalsIgnoreCase(this.JDBC_ORCALE)){
this.JDBC_TYPE = this.JDBC_ORCALE;
}else{
this.init();
}
}

public void init(){
this.JDBC_TYPE = this.JDBC_MYSQL;
}

public Connection getConnection(){

Connection conn = null;
DataSource ds = null;
BasicDataSource bds = null;

try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
ds = (DataSource)envContext.lookup(JDBC_TYPE);

BasicDataSource dbs =(BasicDataSource)ds;
this.connectionStatus = "jdbc type:"+this.JDBC_TYPE+"MaxActive:"+dbs.getMaxActive()+",NumActive: " + dbs.getNumActive() + ",NumIdle: " + dbs.getNumIdle();
//System.out.println(this.connectionStatus);

if( dbs.getNumActive() >= dbs.getMaxActive()){
// System.out.println("`" + JDBC_TYPE + "' is an unknown DataSource");
// System.out.println("'Connection is Busy!!");
this.connectionStatus = "Connection is Busy!!";
}else{
conn = ds.getConnection();
}

}catch(Exception ex){
ex.printStackTrace();
}finally{
System.out.println(this.connectionStatus);
}
return conn;
}



public int getMaxActive(BasicDataSource bds){
return bds.getMaxActive();
}


public int getMaxIdle(BasicDataSource bds){
return bds.getMaxIdle();
}


public String printStatus(){
return this.connectionStatus;
}
public void freeConnection(Connection conn){

if(conn!=null){
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}



}

使用 dbcp 做 connection pool(上)

1. context 設定



auth="Container"
driverClassName="com.mysql.jdbc.Driver"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
loginTimeout="10"
maxWait="5000"
maxActive="100"
maxIdle="10"
name="jdbc/mysql"
testOnBorrow="true"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/TICP"
username="root"
password=""
validationQuery="select 1"/>



2. web.xml

....

JNDI JDBC DataSource
jdbc/GUESTBOOK
javax.sql.DataSource
Container

.....

2007年10月1日 星期一

取得localhost ip及轉換DNS TO IP

package com.webtest;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author jimmy
*/
public class ip {

public String getip(){ //local ip
String localip = "";
try{
InetAddress address=InetAddress.getLocalHost();
localip=address.getHostAddress();
} catch(UnknownHostException uhe){
localip = "Unable to find: ";
}
return localip;
}

public String DNS2ip(String hostname){ // server ip
String strvar="";
try{
InetAddress address=InetAddress.getByName(hostname);
strvar=address.getHostAddress();
} catch(UnknownHostException uhe){
strvar="Unable to find: "+hostname;
}
return strvar;
}

static public void main(String args[]){
ip a=new ip();
//System.out.println(aa.getip(args[0]));
System.out.println(a.getip());
System.out.println(a.DNS2ip("www.yahoo.com.tw"));
}

}

2007年9月26日 星期三

JSP Context 備忘 -- 抓取server Path 路徑

抓取server Path 路徑,我比較常用的二個方法


方法一(印出的時侯code比較短)
<%
String addr = request.getRemoteAddr();
int port = request.getServerPort();
String contextpath = request.getContextPath();
String path = "http://"+addr+":"+String.valueOf(port)+contextpath;
pageContext.setAttribute("path",path);
%>
${path} //印出

//方法二(一行搞到)
http://${pageContext.request.remoteAddr}:${pageContext.request.serverPort}

2007年9月17日 星期一

mysql install --Mac OS X (TAR packages)

下載 Mac OS X (TAR packages)

1.解壓縮放到自已方便記住的地方

2.執行以下的command

shell> scripts/mysql_install_db --user=root
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &

3.加上權限

macbook@jimmys-Macbook[[~/data/mysql/bin]$./mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.45 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database testdb character set utf8;
Query OK, 1 row affected (0.04 sec)

mysql> grant all on *.* to jimmy@localhost identified by '1234';
Query OK, 0 rows affected (0.00 sec)

4.測試

macbook@jimmys-Macbook[[~/data/mysql/bin]$./mysql -u jimmy -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.45 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from user;

2007年9月5日 星期三

jforum 討論區 測試

在安裝的過程當中,出現了一個很奇怪的error
Please give write access for the user who is running the webserver
to the file 'index.htm' and for the directory 'WEB-INF/config' and its
subdirectories before continuing.
叫你要把那些地方的權限修改成可以讀寫的,但是都改了仍舊不行
在網路上有找到一個解決的方法

命名一支為 __index.redirect 的檔案,且內容是空的就可以了

太神了,這樣竟然過了。