java和mysql之间的时间日期类型传递

###mysql(版本:5.1.50)的时间日期类型如下:

datetime 8bytes xxxx-xx-xx xx:xx:xx 1000-01-01 00:00:00到9999-12-31 23:59:59
timestamp 4bytes xxxx-xx-xx xx:xx:xx 1970-01-01 00:00:01到2038
date 3bytes xxxx-xx-xx 1000-01-01到9999-12-31
year 1bytes xxxx 1901到2155
time 3bytes xx:xx:xx -838:59:59到838:59:59(为了满足时间的加减运算)

Read More

MySQL分页查询优化

今天研究了一下MySQL的分页查询,记录并分享如下:

###方式1:

select * from table order by id limit m, n; 

该语句的意思为,查询m+n条记录,去掉前m条,返回后n条记录。无疑该查询能够实现分页功能,但是如果m的值越大,查询的性能会越低(越后面的页数,查询性能越低),因为MySQL同样需要扫描过m+n条记录。

Read More

java获取ftp文件大小

import java.io.IOException;
import java.util.StringTokenizer;

import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

public class FtpGetFileSizeDemo {
  public static int BUFFER_SIZE = 10240;

  private FtpClient m_client;

  private String host = "";

  private String user = "";

  private String password = "";

  private String sDir = "";

  public FtpGetFileSizeDemo() {
    try {
      System.out.println("Connecting to host " + host);
      m_client = new FtpClient(host);
      m_client.login(user, password);
      System.out.println("User " + user + " login OK");
      System.out.println(m_client.welcomeMsg);
      m_client.cd(sDir);
      System.out.println("Directory: " + sDir);
      m_client.binary();
      System.out.println("Success.");
    } catch (Exception ex) {
      System.out.println("Error: " + ex.toString());
    }
  }

  protected void disconnect() {
    if (m_client != null) {
      try {
        m_client.closeServer();
      } catch (IOException ex) {
      }
      m_client = null;
    }
  }

  public static int getFileSize(FtpClient client, String fileName)
      throws IOException {
    TelnetInputStream lst = client.list();
    String str = "";
    fileName = fileName.toLowerCase();
    while (true) {
      int c = lst.read();
      char ch = (char) c;
      if (c < 0 || ch == '\n') {
        str = str.toLowerCase();
        if (str.indexOf(fileName) >= 0) {
          StringTokenizer tk = new StringTokenizer(str);
          int index = 0;
          while (tk.hasMoreTokens()) {
            String token = tk.nextToken();
            if (index == 4)
              try {
                return Integer.parseInt(token);
              } catch (NumberFormatException ex) {
                return -1;
              }
            index++;
          }
        }
        str = "";
      }
      if (c <= 0)
        break;
      str += ch;
    }
    return -1;
  }

}

后来发现不能获得ftp里中文文件的大小,解决办法:
调用getFileSize() 之前先编解码,然后调用getFileSize,就O了,如:
fileName=new String(fileName.getBytes("GBK"),"ISO8859-1");
而不是在第50行(fileName = fileName.toLowerCase(); )之后去调用fileName=new String(fileName.getBytes("GBK"),"ISO8859-1");

log4j中打印Ibatis最终的SQL语句

在项目开发时都大家都希望将SQL在后台打印出来,以帮助开发以及后续的bug修改。如果用JDBC那么可以方便的打印,可使用ibatis就不知道怎么办了,最近在网上找了一段log4j的配置可以很保姆的处理这个问题。这里贴出来给大家参考一下。

Read More

ibatis传递多个参数

不知何时,ibatis3改为mybatis3了,听说mybatis3不用再需要自己手动实现DAO的实现类了,Service层可以直接使用DAO接口中的方法。

ibatis3如何传递多个参数有两个方法:一种是使用Map,另一种是使用JavaBean。

Read More