测试的那些事

HSF服务探测工具总结2

上一篇 / 下一篇  2012-05-30 16:12:53 / 个人分类:JAVA

1.   JDBC操作

ResultSet使用

   publicList<ApiInfo> doQuaryApiInfo(String sql)throwsSQLException{

      Connection conn=null;

      ResultSet queryData=null;

      Statement stmt =null;

      List<ApiInfo> apiInfoList =newArrayList<ApiInfo>();

      Class.forName("com.mysql.jdbc.Driver");

      conn=DriverManager.getConnection(url,user,password);

          stmt = conn.createStatement();

          queryData = stmt.executeQuery(sql);

          if(queryData !=null){

             while(queryData.next()){

                 ApiInfo apiInfo =newApiInfo();                apiInfo.setApiName(queryData.getString("api_name"));             apiInfo.setCatName(queryData.getString("cat_name").trim());

                 apiInfo.setStatus(queryData.getInt("status"));

                 apiInfoList.add(apiInfo);

             }

          }

          stmt.close();

          conn.close();

      }

      

      returnapiInfoList;

}

数据库返回为ResultSet类型,通过转化得到返回数据,ResultSet不能直接返回,因为数据库链接断开后,无法访问ResultSet数据。

数据库链接未正常关闭问题

The driver was unable to create a connection due to an inability to establis

java类大批量操作数据库时会报错,由于链接非正常关闭,数据库链接用完。需要及时关闭数据库链接

      conn=DriverManager.getConnection(url,user,password);

          String sql="insert into cat_data(cat_name) values(?)";

          pstmt = conn.prepareStatement(sql);

          pstmt.setString(1, cat_name);

          pstmt.executeUpdate();

          pstmt.close();                    

      }

      catch(SQLException se){        

      se.printStackTrace();

      }

      finally{

          pstmt =null;

          conn=null;

      }

 

遇到的问题:

java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/hsftest

由于未加载驱动Class.forName()

          Class.forName("com.mysql.jdbc.Driver");

          }catch(ClassNotFoundException e) {

             e.printStackTrace();

          }

          conn=DriverManager.getConnection(url,user,password);

          stmt = conn.createStatement();

          queryData = stmt.executeQuery(sql);

 

一.Java

1.定时任务

通过任务类MyTask、监听类MyListenerSpring配置来实现

MyTask

publicclassMyTaskextendsTimerTask {

 publicvoidrun() {

// System.out.println("call at " + (new Date()));    

// TODO 此处添加具体任务代码 }

MyListener

publicclassMyListener {

   privateTimertimer=null;

   publicvoidinit(){

      timer=newTimer();

      timer.schedule(newMyTask(), 1000, 360000);//1秒后执行此任务,每次间隔360,就可以在某个固定的时间执行这个任务.

   }

}

 

配置Spring

<beans>

<bean id="myListener" class="com.api.hsf.detect.web.screen.MyListener" init-method="init"/>

</beans>

 

-----------------------------------------------------------------

2.小数位截取:

http://hi.baidu.com/aideyoulin/blog/item/88523fd29a46d03e970a16d5.html

方法:java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");

double d=3.14159;

df.format(d);

 

3.空格字符处理

对于数据中存在/r空格的情况,取出的数据需要做前后空格处理

apiInfo.setCatName(queryData.getString("cat_name").trim());

  

二.HTML

1.   基本操作

字体上色

<font color=red></font>

表格属性

<table border="1"> 

居中属性

align="center" 

如何在原页面打开连接,如何打开一个新页面

<a href="http://www.w3school.com.cn/"target="_blank">连接</a>

target属性设置为"_blank",该链接会在新窗口中打开。不设置target属性,则在原页面打开 

学习网站:http://www.w3school.com.cn/html/


TAG: java JAVA

 

评分:0

我来说两句

Open Toolbar