2012年2月27日星期一

use reflection to invoke a method


import java.lang.reflect.Method;

public class InvokeTester
{
public int add(int param1, int param2)
{
return param1 + param2;
}

public String echo(String message)
{
return "hello: " + message;
}

public static void main(String[] args) throws Exception
{
InvokeTester test = new InvokeTester();
System.out.println(test.add(1, 2));
System.out.println(test.echo("John"));
                //the above is to use normal ways to use a class and invoke a method
                //the following is to use reflection

Class<?> classType = InvokeTester.class;

Object invokeTester = classType.newInstance();

System.out.println(invokeTester instanceof InvokeTester);

Method addMethod = classType.getMethod("add", new Class[] { int.class,
int.class });

Object result = addMethod.invoke(invokeTester, new Object[]{1, 5});
                // this is something like
                // result=invokeTester.add(1,2)

System.out.println((Integer)result);

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

Method echoMethod = classType.getMethod("echo", new Class[]{String.class});

Object result2 = echoMethod.invoke(invokeTester, new Object[]{"Clinton"});

System.out.println((String)result2);


}
}

2012年2月26日星期日

multi thread dead lock




class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag = flag;
}

public void run()
{
if(flag)
{
while(true)
{
synchronized(MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+"...if locka ");
synchronized(MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"..if lockb");
}
}
}
}
else
{
while(true)
{
synchronized(MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"..else lockb");
synchronized(MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+".....else locka");
}
}
}
}
}
}


class MyLock
{
static Object locka = new Object();
static Object lockb = new Object();
}

class  DeadLockTest
{
public static void main(String[] args)
{
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}

2012年2月22日星期三

PreparedStatement


Before running the following program,you should first create a table in a mssql database.

package liulixin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCDemo2 {
public static void main(String[] args) {
try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {

Connection conn = DriverManager.getConnection(
"jdbc:sqlserver://*****.com;databaseName=****", "****",
"*****");

String sql ="select uName as userName  from tbl_user where uid=?";
PreparedStatement preStmt = conn.prepareStatement(sql);
preStmt.setInt(1, 2);
ResultSet rs=preStmt.executeQuery();
while(rs.next()){

System.out.print(rs.getString("userName")+"  " );
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}