欢迎您来到GIS动力

加入收藏 免费注册 用户登陆 帮助中心
首页 新闻动态 技术专栏 银杏树下 学习考研 软件下载 求职招聘 许愿瓶 节日祝福 用户中心 精彩推荐 资源搜索 地图
专栏导航: AO开发 | SO开发 | ArcGIS桌面 | 超图桌面 | 开发语言 | 数据库 | WebGIS | 银杏文学 | 研究生考题 | FreeMap FreeTalk
   您现在位于: 首页技术专栏开发语言 → 正文
Java程序性能和速度优化实例
07-12-24 00:00:00 作者: 出处:赛迪网技术社区

例一:应用具有I/O Buffer功能Class

  import java.io.*;
public class IoTest
  {
   public static void main(String args[])
   {
    try
    {
     FileReader fr = new FileReader(args[0]);
     BufferedReader br = new BufferedReader(fr);
     while ( br.readLine() != null )
     {
      System.out.println(" The file content are :" + br.readLine());
     }
     fis.close();
    }
    catch ( IOException ioe )
    {
     System.out.println("The I/O exception is " + ioe);
    }
   }
  }
   
  在上例中,程序使用了具有Buffer功能的Class,使得Disk I/O的读取速度大大提高。BufferedReader 是取代DataInputStream 而提高读写速度的Java Class。在新的Java版本中,已不建议使用DataInputStream,因为其读写是基于字符为单位的。
   
  例二:字符串运算处理
   
  public class StringOperation
  {
   public static void main(String args[])
   {
    String sqlQuery = null;
    String sqlCondition = " conditionC = conditionD ");
    StringBuffer sb = new StringBuffer();
    sb.append("select * from database table where ");
    sb.append(" conditionA = conditionB and ");
    if ( ! sqlCondition.equals(null)
    {
     sb.append(sqlCondition);
    }
    else
    {
     sb.append(" conditionE = conditionF ");
    }
    sqlQuery = sb.toString();
    // Then connect to the database then excute the database query
    // .......
   }
  }
   
  在上例中,使用StingBuffer class来完成数据库查询建立,避免使用String class的"+="操作,以减少JVM在内存中创建新的对象,占用资源,增加JVM回收资源负担。读者可以使用Java Proflier功能来具体比较使用不同的String操作,JVM需要完成多少资源回收和运行时间。因此在JVM中对String直接进行"+="是非常昂贵的运算。

  例三:处理昂贵的数据库初始化

  目前许多网站可以透过Web服务器查询数据库,如何提高数据库查询速度成为许多程序员关注的问题。在Java Servlets或JSP中可以通过init() 或Jspinit()来实现,以下是一具体Java Servlet与数据库对话实例。
    
  import java.io.*;
  import java.sql.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
   
  public class DatabaseServlet extends HttpServlet
  {
   public void init( ServletConfig conf) throws ServletException
   {
    super.init(conf);
    Connection conn = null;
    try
    {
     Class.forName("sun.jdbc.odbc.JdcOdbcDriver");
     Conn = DriverManager.getConnection("jdbc:odbc:yourDSN,"","");
    }
    catch ( SQLException sqle )
    {
     System.err.println("your error exception is " + sqle);
    }
    catch ( ClassNotFoundException cnfe )
    {
     System.err.println("your error exception is " + cnfe);
    }
   }
   public void doGet( HttpServletRequest req, HttpServletResponse res) throws
   ServletException, IOException
   {
    res.setContentType("text/html");
    ServletOutputStream out = null;
    // Your HTML formatter
    out.println(" Your HTML");
    try
    {
     Statement stmt = conn.creatStatement();
     ResultSet rs = stmt.excuteQuery("select * from yourDatabasetable ");
     while ( rs.next() )
     {
      // Processing your data
     }
    }
    catch ( SQLException sqle )
    {
     out.println("The SQL error is " + sqle);
    }
    // output your processing result to HTML page
    out.println(" your HTML");
    rs.close();
    stmt.close();
   }
   public void destroy()
   {
    try
    {
     conn.close();
    }
    catch ( SQLException sqle )
    {
     System.err.println("your SQL error is " + sqle);
    }
   }
  }
   
  在上例中,由于Java Servlet运行机制的特点,将昂贵的数据库初始化运算在整个Servlet运行中仅只调用一次的init()中完成,以减少不必要的重复性数据库运算。读者可以根据应用的具体情况,甚至将数据库的Statement和ResultSet部分移至init()中完成,或者调用PreparedStatement与CallableStatement来优化数据库的运算。同时,对数据库的连接的关闭由destroy()一次性完成。


(本文已被浏览 次)
发布人:admin
推荐给好友:发送给好友
上篇新闻:
下篇新闻:
相关评论
发表我的评论
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  •   相关文章  
    无相关新闻

    关于我们友情链接 ┋ 与我在线 ┋ 管理 ┋ TOP
     
    网站当前版本:GisPower CMS V3.0
    『GIS 动力』- http://www.gispower.org/
    联系我们:webmaster#gispower.org
    Copyright (c) 2003-2007 GisPOwer.Org. All Rights Reserved.
     

                   滇ICP备05006901号