一周回顾

原计划这个周末考试数据库,可惜被通知推迟了。这样一来,我的进度加快了。

从jsp语法到el,可谓是“大跃进”了。

这本书的第五章讲述了隐含对象,Implicit object,

我在阅读的时候发现知识太凌乱,可能因为我太心急,不求甚解。

看了第六章el的知识,这种感觉更强烈。

顺便看了第六章JSTL,明白了我原先使用tablib报错的原因了。

btw:自己被授命“顾问”,还真有点,,

嘿嘿,一定努力!!

好了,今天好累,先睡

struts下开发

休息来一早上,现在来吧poi应用移植到struts下,周末快结束了,我必须迅速了结这个任务

,为了简单开发,我还是用eclipse2.1+easy struts ,恩如果你正在我的日记,(实际上,我倒是一直努力将它写成一个给初学者的实例教程)并对easy struts插件不是很了解,你应该先看看"使用 Easy Struts for Eclipse 开发 Struts "这篇文章(在网站内我已经给过它的连接),现在我的各种插件已经配置完成,  开始开发吧!!

 1 ,创建一个tomcat应用,并添加easy struts 支持,看过文章后 这很使简单。

 2 ,创建一个fileuploadfom  and fileuploadbean   ,还是很简单,唯一要提及的form 属性:

     FormFile file;   String fileName; String size;   恩如果你在提示里找不到FormFile 这个类请在类型里键入

     org.apache.struts.upload.FormFile 他关联的html标签相应的是html:file;接着你应该修改一下,formbean

     的reset函数,

  3,恩,还需要给函数加上验证,加入一下代码!同时在propertie 文件中加入error.file 消息

   ActionErrors errors = new ActionErrors();

    if (file == null )

       {

          errors.add("file", new ActionError("error.file")); 

       }

     return errors;

 4, 在我写action志前,我应当将poitest.java 进行修改以适应action的调用,首先在我们的项目文件里加入

    javabean包!现在声成一个javabean excelReadBean,显然它的目的,一眼就可以看出来。

    现在把poitest.java 的read(String table) 方法的代码拷贝过来,恩很显然,少了全局变量:xlsfile(参照原     代码);这里不得不注意这样的问题,为了满足,线程无关性,将read方法变为,read(String table,String xlsfile);

    现在似乎完成了,然而,我们不准备使用,手动连接,为了更方便以后更改,现在来吧数据源放在struts的配置文件里,(参照"使用 Easy Struts for Eclipse 开发 Struts ",现在来完成 DATABASE配置)

  5,既然,要通过Struts框架连接数据库,我想我们的代码也应改修改一下,不过我总是期望以后再次使用这个类,也许并不在Struts框架下,所以我决定使用夺态,在建一个read方法(懒家伙总是向我这样)。

   public boolean read(String table,String xslfile,Connection conn)

{

return false;

} // 为此又不得不引进,sql包,

   现在有理由相信,里面很简单,我们要做的仅仅是将同明方法的代码拷过来!然后删除,曾在上文提及的Rs方法,因为我们用不着他了,加入一下代码:

   Statement stmt = null;

   boolean   rs= false;

  然后将,rs.execute(sql.toString())替换为

  stmt=conn.createStatement();rs=stmt.execute(sql.toString()); 最后return rs;  好一切噢可!如果不想再改观这个类,我想这份工作已经完成了,可是我还是一个类200多行的代码,就是冗余阿,还是修改修改 恩,把读和写剥离开,就这枚简单!  现在来看看修改了后的javabean,  代码又和以前差不多了,呵呵,功能却没有减

/*

 * Created on 2004-11-21

 *

 * To change the template for this generated file go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

package com.phone.struts.javabean;

 

import java.io.*;

import java.util.*;

import javax.sql.*;

import java.sql.*;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.hssf.model.*;

import org.apache.poi.poifs.filesystem.*;

import com.cims.db.*;

/**

 * @author Administrator

 *

 * To change the template for this generated type comment go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

public class excelReadBean {

public excelReadBean()

{

}

/**

*

* @param table  this is the name of table in databse

* @param xslfile the excel file which you want to deel with

* @return

*/

public boolean read(String table,String xslfile)

{  

boolean   rs= false;

   rs=read(table,xslfile,null);

return rs;

}

/**

*

* @param table

* @param xslfile

* @param conn

* @return

*/

public boolean read(String table,String xslfile,Connection conn)

{

//@rs for the the result of executing sql

boolean   rs= false;

//construts a poifile

POIFSFileSystem fs=null;

HSSFWorkbook   wb=null; 

try

{

fs =new POIFSFileSystem(new FileInputStream(xslfile));

wb= new HSSFWorkbook(fs);

}catch(IOException e)

{

System.out.println("io exception :"+e);

}

HSSFSheet sheet = wb.getSheetAt(0);

//

System.out.println("the sheet name of  poifile="+wb.getSheetName(0));

int rownum=sheet.getPhysicalNumberOfRows();

System.out.println("the physical Number of rows="+rownum);

for(int rowflag=1;rowflag<rownum;rowflag++)

{

      //enter into row loop

      HSSFRow row = sheet.getRow(rowflag);

      int cells = row.getPhysicalNumberOfCells();

      System.out.println("the total number of cell in this row"+cells);

      System.out.println("this ROW= " + row.getRowNum());

            

      //

      String cellV[]=new String[cells];

      for(int cellflag=0;cellflag<cells;cellflag++)

  {

         //enter into cell loop

                 

         HSSFCell  cell= row.getCell((short)cellflag);

         double    valuenum = 0;

         String    valuestr=null;

         String    type=null;

// i got the code below from org.apache.poi.hssf.dev.hssf class

// and modify some

switch (cell.getCellType())

{

                             case HSSFCell.CELL_TYPE_FORMULA :

      type = "FORMULA ";

      valuestr="null";

  break;

case HSSFCell.CELL_TYPE_NUMERIC :

type = "NUMERIC";

valuenum=cell.getNumericCellValue();

break;

case HSSFCell.CELL_TYPE_STRING :

type = "STRING";

valuestr=cell.getStringCellValue();

break;

   default :

type=null;

}

if(type!=null)

{

System.out.print("the type of this cell is==="+type);

if(valuestr!=null)

{

cellV[cellflag]=valuestr.trim();

System.out.println("the value of this cell is==="+valuestr);

  } else  

  {

  cellV[cellflag]=Double.toString(valuenum);

  System.out.println("the value of this cell is==="+valuenum);

  }

} else

{

  cellV[cellflag]="null";

  System.out.println("erro in the file ,there is a null type cell :"+cellflag);

}

  }

  //here i will manipulate the 'insert' directive of database

  StringBuffer sql=new StringBuffer("insert into "+table+"  values (");

  for(int j=1;j<cells;j++)

  {

sql.append("'"+cellV[j-1]+"',");

  }

  sql.append("'"+cellV[cells-1]+"','"+rowflag+"')");

  System.out.println("sql  "+"="+sql.toString());

//call the write() function

      if(conn!=null)

  {

       rs=this.write(conn,sql.toString());

  }else

        rs=this.write(sql.toString());

     

}

return rs;

}

/**

*

* @param conn  the jdbc connection

* @param sql   the sentence of sql

* @return

*/

private boolean write(Connection conn,String sql)

{

Statement stmt = null;

boolean   rs= false;

try{ 

System.out.println("Start connet to db");

             stmt=conn.createStatement();

         rs=stmt.execute(sql.toString());

        }catch(Exception e)

        {

                 System.out.println("  sql exception "+e);

        }

return rs;

}

/**

*

* @param sql

* @return

*/

private boolean write(String sql)

    {

Rs  resultset=new Rs();

    boolean   rs= false;

    try



   System.out.println("Start connet to db");

           rs=resultset.execute(sql.toString());

}catch(Exception e)

    {

System.out.println("  sql exception "+e);

}

return rs;

}

}

写数据库

晚饭过后,终于有了点精神,顺便把写数据库给做了,这么小的程序就用jdbc吧! 呵呵省力!!!

首先引进import com.cims.db.*;包,其中封装了两个类,conn(创立连接),rs(执行sql)

呵呵接着在 row 循环里续代码了

  /**

   *  below i will connection the database and execute the sql .

              **/

              try

              { 

                    System.out.println("Start connet to db");

                   

                   

                   

                    resultset.execute(sql.toString());

              }catch(Exception e)

              {

                   

                    System.out.println("  sql exception "+e);

              }

就这几行了,所有的数据库操作已经简单封装在被引用的包里。 呵呵  如果您在看我的日记 不是很名白com.cims.db.*

拿酒来看看它中的代码吧!

  conn:  一个创立连接的类  它提供了getConnection方法返回一个connection

  public Connection getConnection() throws SQLException,IOException

{  

String url=null;

String username=null;

    String password=null;

   Connection conn=null;

   Properties props=new Properties();  

   InputStream in = getClass().getResourceAsStream("/database.properties");

   props.load(in);

   System.out.println("hello2");

   in.close();    

    try{

String drivers=props.getProperty("jdbc.drivers");

System.out.println(drivers);

if (drivers!=null)

    Class.forName(drivers).newInstance();

    System.out.println("test for url");

url=props.getProperty("jdbc.url");

System.out.println(url);

username=props.getProperty("username");

password=props.getProperty("password");

System.out.println(password);

   }

   catch(Exception e){System.out.println(e.getMessage());}

   return DriverManager.getConnection(url,username,password);

}

   Rs :sql执行类 其中

      public ResultSet executeQuery(String sql) throws SQLException,IOException方法用于

     数据库查询

      public int executeUpdate(String sql) throws SQLException,IOException 用于数据更新

     当然,还用更通用的方法

     public boolean execute (String sql) throws SQLException,IOException 让他来完成我们的

    数据插入

   看看conn 你就会发现使用jdbc怎么配置数据源了 呵呵  来看看MySQL数据库下配置数据源 

   database.properties的代码:

     jdbc.drivers=org.gjt.mm.mysql.Driver

     jdbc.url=jdbc:mysql://localhost:3306/crm

     username=root

     password=cimserp

  如果你使用的是oracle, 拿酒看看下面例子

    jdbc.drivers=oracle.jdbc.driver.OracleDriver

    jdbc.url=jdbc:oracle:thin:@10.11.12.46:1521:cimserp

    username=cimserp

    password=cimserp

  好了轻轻松松的就将excel中的数据插入数据库了,poi的测试也就到此结束!!我也可以给我朋友说,no problem,

  你需要的很快竟能完成,:)

  看看还需要足作些什么  哦!!  后面几天将把它移植到struts框架下,将用eclipse 2.1 版本+easystruts 快速开发 

    

print 内容

恩,我想应该是print excel 文件的内容的时候了!在read()方法中加入一下代码,并且给read()添加一个参数string tabe  print处所有的sql语句来

for(int rowflag=0;rowflag<rownum;rowflag++)

        {

        //enter into row loop

             HSSFRow row = sheet.getRow(rowflag);

             int cells = row.getPhysicalNumberOfCells();

             System.out.println("the total number of cell in this row"+cells);

             System.out.println("this ROW= " + row.getRowNum());

            

             //

             String cellV[]=new String[cells];

             for(int cellflag=0;cellflag<cells;cellflag++)

             {

             //enter into cell loop

                 

                HSSFCell  cell= row.getCell((short)cellflag);

                double    valuenum = 0;

                String    valuestr=null;

                String    type=null;

                // i got the code below from org.apache.poi.hssf.dev.hssf class

                // and modify some

                switch (cell.getCellType())

                {

                                case HSSFCell.CELL_TYPE_FORMULA :

                                    type = "FORMULA ";

                                    valuestr="null";

                                    break;

                                case HSSFCell.CELL_TYPE_NUMERIC :

                                    type = "NUMERIC";

                                     valuenum=cell.getNumericCellValue();

                                    break;

                                case HSSFCell.CELL_TYPE_STRING :

                                    type = "STRING";

                                    valuestr=cell.getStringCellValue();

                                    break;

                                default :

                                    type=null;

                 }

                 if(type!=null)

                 {

                    System.out.print("the type of this cell is==="+type);

                    if(valuestr!=null)

                    {

                      cellV[cellflag]=valuestr.trim();

                      System.out.println("the value of this cell is==="+valuestr);

                    }

                    else  

                    {

                      cellV[cellflag]=Double.toString(valuenum);

                      System.out.println("the value of this cell is==="+valuenum);

                    }

                 }

                 else

                 {

                      cellV[cellflag]="null";

                      System.out.println("erro in the file ,there is a null type cell :"+cellflag);

                 }

              }

              //here i will manipulate the 'insert' directive of database

              StringBuffer sql=new StringBuffer("insert into "+table+"  values (");

              for(int j=1;j<cells;j++)

              {

                   sql.append("'"+cellV[j-1]+"',");

             }

                      sql.append("'"+cellV[cells-1]+"')");

                      System.out.println("sql  "+"="+sql.toString());

             

             

        }

恩看来excel支持的数据类型有限,它支持四种,FORMULA ,NUMERIC,STRING,date。  我们只用到了三种,很显然我常常会在粘切时莫名其妙的在excel插入空列,很讨厌阿,还是加上type验证吧!

问题:jsp中javabean的方法命名疑惑(未解决)

    应朋友的建议,今天加上用户帐号管理的功能。在之中,出现了一个关于javabean方法命名规范的问题。

formbean中有一个变量名为rNewPsd,意为新密码验证,其中的get方法为public getRNewPsd();

在jsp容器中运行报错说属性rNewPsd没有get方法。明明有啊,怪事。

    后来,把变量名改为rewPsd(可以不改),get方法名改为getRewPsd();一切ok。

    我翻了一下<<thinking in java,3rd>>,没有看到javabean的命名有这个限制,不知是怎么一回事?也许跟jsp容器有关。

前四章节小结

这本书的第一章节主要介绍安装jdk,tomcat,ant和书的示例,有点“废话”的嫌疑。

第二章节,大概性的介绍了servlet,作者的意图很明显,给读者介绍一下jsp的历史缘由。

第三章节,主要介绍JSP的机制,但不深入,提到转译时期和请求时期欲言又止,让我看着着急。此外比较了jsp和asp以及asp.net的各种性能,对asp的了解不多,所以大致看了一下。

第四章节,转入正题,主要讲JSP语法,比较全面讲述了jsp的构成,对Elements的四中类型做了甚为详细的介绍。对批注和特殊字符转意解释清楚,不过charset的介绍,它采用MS950,不知道是不是繁体中文的原因,在以后的例子当中它都采用gb2312。Driective Elements讲到三种指令(1.2),其中我在使用taglib指令时遇到问题,对URI属性不清楚,导致第一个JSP没有运行成功。对于Scripting Elements的三部分的区分还只是停留在书本上。在学到Action Elements时,看到一共20个元素,头大了。

计划用一个星期的时间来看第五章隐含对象这部分。

这个周末要考试数据库,或多或少会影响进度。

日记001

最近做了两个jsp+struts+ojb的小项目,感觉还不错,不过页面用了太多的script,项目开始时还特别强调了这个问题,初期还控制得不错,但越到后面越难以控制,最后的报表已经是几乎完全要依赖客户端脚本了。现在对这此script挺担心的,不知道你们是怎么处理这个问题的?

开山篇-工欲善必先利其器

要想“hello, world”还真是不简单。

lomboz的插件是早先安装好的,到今天才算基本会用,至于随心所欲,还没有到那个地步。

在调试过程发现lomboz有个不尽人意的地方,一旦我在视图中点击选择deploy,那么如果我想再继续添加诸如servlet,即使我再选deploy,已经不能更新tomcat   webapp下相应的文件。

或许是我还没有掌握lomboz。

调试第一个servlet成功,但是对内在的机制还没有明白。

万丈高楼平地起,希望我的高楼可以慢慢升起到最高点。

cvs --help-options,The Repository(部分不懂)

-f,-z,-a,-s的选项不懂。

利用-d 选项可以绕过CVSROOT环境变量的限制,在下列的项目中会使用到。

2.1 Telling CVS where your repository is

如:cvs -d g:\cvsroot checkout test

2.5 Multiple repositories    

如:cvs -d server1:/cvs co dir1

    cd dir1

    cvs -d server2:/root co sdir

    cvs update

2.6 Creating a repository

cvs -d g:\cvsroot init

cvs大小写敏感,commit,release,$VISUAL

13:53 2004-11-10

cvs命令是大小写敏感的,checkout!=CHECKOUT

21:21 2004-11-10

今天学习了:

cvs commit 提交操作

cvs commit -m "Added an optimization pass" driver.c  不会出现specify the log message的编辑器;

否则就会调出$CVSEDITOR或$EDITOR环境变量指定的编辑器要求输入log message;

cvs release -d tc  可以删除用户目录tc

但是cvs release tc  我却看不出有什么特殊的用途

Viewing differences :cvs diff driver.c查看库里的文件和本地文件的区别

疑问:

1、环境变量:$VISUAL

2、cvs release的用途