posts - 3,  comments - 1,  trackbacks - 0
  2006年1月20日

       有同事问到在程序中怎样知道数据库表中那些字段是主键。当时不知道,晚上回来看了看JDK的文档。

       在使用JDBC来查询数据库的时候,通常的步骤是:

1.  注册驱动程序

2.  获取数据库连接

3.  执行查询语句

4.  关闭连接。

       在获得数据库连接后,就可以通过getMetaData()方法来获取DatabaseMetaData;然后通过DatabaseMetaDatagetPrimaryKeys ()方法来获取主键的信息。

       下面是我做的示例程序,该程序在JBuilder2005oracle8i下通过:
import java.sql.*;

import javax.sql.*;

public class TestJDBC {

    public TestJDBC() {

    }

 

    public static void main(String[] args) throws SQLException {

        Connection con = null;

        Statement st = null;

        ResultSet rst = null;

        try{

                     //注册数据库驱动程序

            Class.forName("oracle.jdbc.driver.OracleDriver");

                     //获取数据库连接

            con = DriverManager.getConnection("jdbc:oracle:thin:@10.60.203.80:1521:TestDB","123","123");

                     //获取主键信息

            rst = con.getMetaData().getPrimaryKeys(null,null,"USER");

                     //打印主键信息

            if (!rst.isAfterLast()) {

                rst.next();

                System.out.println(rst.getString("TABLE_NAME") + "  " +

                                   rst.getString("COLUMN_NAME"));

            }

        }

        catch (Exception e){

            System.out.println(e.getLocalizedMessage());

        }

        finally{

            try{

                            //关闭连接

                if (rst != null)

                    rst.close();

                if (con != null)

                    con.close();

            }

            catch (SQLException e){

                throw e;

            }

        }

 

    }

}

 

上面的程序中,在获取主键信息的时候,语句

rst = con.getMetaData().getPrimaryKeys(null,null,"USER");

用来获取主键信息。关于该函数的详细信息,请参阅JDK的文档。这里要说的是,在测试中发现第三个参数(数据库表名)是大小写敏感的,如果写成user是查不到结果的。

posted @ 2006-01-20 10:52 afrag 阅读(224) | 评论 (1)编辑 收藏
  2005年11月2日

在Spring 的AOP中,如果一个Proxy同时实现MethodBeforeAdvice、AfterReturningAdvice和MethodInterceptor接口,那么这三个Advice的执行顺序是什么样的呢?
经过试验,是和xml文件中的定义顺序有关的。
如果Proxy的接口实现定义为
     
         
            MethodBeforeAdvice
            AfterReturningAdvice
            MethodInterceptor
         

     

那么执行的结果是
MethodBeforeAdvice

MethodInterceptor: before call

Really method excuting

MethodInterceptor: after call

AfterReturningAdvice

也就是说,执行顺序是:MethodBeforeAdvice,MethodInterceptor的调用前的部分,目标方法,MethodInterceptor的调用后的部分,AfterReturningAdvice。

如果proxy的定义是
     
         
            MethodBeforeAdvice
            MethodInterceptor
            AfterReturningAdvice
         

     

执行的结果是
MethodBeforeAdvice

MethodInterceptor: before call

Really method excuting

AfterReturningAdvice

MethodInterceptor: after call
也就是说,执行的顺序是:MethodBeforeAdvice,MethodInterceptor的调用前的部分,目标方法,AfterReturningAdvice,MethodInterceptor的调用后的部分。

如果proxy的定义是
     
         
            MethodInterceptor
            MethodBeforeAdvice
            AfterReturningAdvice
         

     

执行的结果是:
MethodInterceptor: before call

MethodBeforeAdvice

Really method excuting

AfterReturningAdvice

MethodInterceptor: after call
也就是说,执行的顺序是:MethodInterceptor的调用前的部分,MethodBeforeAdvice,目标方法,AfterReturningAdvice,MethodInterceptor的调用后的部分。
以上的顺序是在springframework 1.2.5中测试的。

posted @ 2005-11-02 18:10 afrag 阅读(984) | 评论 (0)编辑 收藏
  2005年10月29日

今天在看Spring in Action,看到了一个很有意思的例子。
假如你编写了一个类:

public class ClassA{
      private String fieldA;

      public ClassA(){
         fieldA = "This is Class A";
     }

      public myPrint(){
            System.out.println(fieldA);
      }
}

OK。按照面向对象的思想,其他的类应该不能够修改ClassA的fieldA字段吧?因此无论什么时候你调用ClassA的myPrint方法,都应该打印的是“This is Class A”。
      但是,实际情况并不是如此,请看下面的例子:
import java.lang.reflect.Field;
public class  TestChangeApp
{
    public static void main(String[] args)
    {
        System.out.println("Begin to test change.");
        ClassA test = new ClassA();
        test.myPrint();

        Field[] fields = test.getClass().getDeclaredFields();
  
        try
        {
            for (int i=0;i            {
                if (fields[i].getType().equals(java.lang.String.class)){
                   fields[i].setAccessible(true);
                   fields[i].set(test, "This field have been changed!");
               }
            }
   
        }
        catch (Exception e)
        {
        }

        test.myPrint();
    }
}
运行结果是什么呢?是
Begin to test change.
This is ClassA
This field have been changed!
也就是说,在TestChangeApp类中,可以修改ClassA的私有成员变量。

posted @ 2005-10-29 16:26 afrag 阅读(309) | 评论 (0)编辑 收藏
仅列出标题  
<2008年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

常用链接

留言簿

随笔分类

随笔档案

文章档案

搜索

  •  

积分与排名

  • 积分 - 1972
  • 排名 - 1902

最新评论

阅读排行榜

评论排行榜