科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道关于javabean的代码

关于javabean的代码

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

我的代码是借鉴和摘抄别人的,但是运行通过没有问题。

作者:软件工人 来源:CSDN 2008年3月18日

关键字: 代码 JavaBean java

  • 评论
  • 分享微博
  • 分享邮件

数据库连接  和  增删改  

QueryBean.java

package weblistbean;
import java.sql.*;
import java.io.*;
public class QueryBean
{
  //public String query_statement; /*定义sql语句*/
  public String param[]; /*查询条件*/
  public ResultSet result=null; /*查询结果*/
  public Connection conn;
//设置构造函数
 
///数据库连接
  public QueryBean() throws Exception{
       
        this.setConnection("com.microsoft.jdbc.sqlserver.SQLServerDriver","jdbc:microsoft:sqlserver://10.0.0.21:1433;DatabaseName=lngprs;SelectMethod=Cursor","sa","sa");
         System.out.println("---------设置构造函数--------------");
   }
  //设置查询参数
  public void setParam(String[] param)
  {
    this.param=param;
  }
  //设置SQL查询语句
  //public void setQuerystatement(String query_statement)
  //{
   //System.out.println(query_statement);
   // this.query_statement=query_statement;
   // System.out.println("---------传入sql语句--------------");
  //}
  //设置连接参数
  public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
  {
    Connection conn1;
    Class.forName(driverName);
   // conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+p.getProperty("dbserver")+":1433;DatabaseName="+p.getProperty("dbname")+";charset=GB2312",p.getProperty("user"),p.getProperty("password"));        
    conn1=DriverManager.getConnection(jdbcURL,username,passwd);
    ////
   // Stm = conn.createStatement();             
    ////
    conn1.setAutoCommit(false);
    this.conn = conn1;
    System.out.println("---------数据连接成功--------------");
  }
  /*获取查询结果*/
  public ResultSet getResult(String query_statement)
  {
    try
    {
      PreparedStatement select_stm=conn.prepareStatement(query_statement,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
      if (param!=null)
        for(int i=0;i<param.length;i++)
         select_stm.setString(i+1,param[i]);
      result=select_stm.executeQuery();
    }catch(Exception e){System.out.println(e);}
    return result;
  }
  /*对数据库进行增加记录操作*/
  public void insertRecord(String query_statement) throws SQLException,java.io.UnsupportedEncodingException
  {
    try
    {
     
      PreparedStatement insert_stm=conn.prepareStatement(query_statement);
      if (param!=null)
        for(int i=0;i<param.length;i++)
           insert_stm.setString(i+1,param[i]);
      insert_stm.executeUpdate();
      insert_stm.close();
      conn.commit();
      System.out.println("数据插入成功");
    }
    catch(Exception e)
    {
      System.out.println(e);
      conn.rollback();
    }
  }
  /*对数据记录进行更新操作*/
  public void updateRecord(String query_statement) throws SQLException,java.io.UnsupportedEncodingException
  {
    try
    {
     //System.out.println(query_statement);
      PreparedStatement update_stm=conn.prepareStatement(query_statement);
      if (param!=null)
        for (int i=0;i<param.length;i++)
          update_stm.setString(i+1,param[i]);
      update_stm.executeUpdate();
      update_stm.close();
      conn.commit();
      System.out.println("数据修改成功");
     }
     catch(Exception e)
     {
       System.out.println(e);
       conn.rollback();
     }
  }
  /*删除数据记录*/
  public void deleteRecord(String query_statement) throws SQLException,java.io.UnsupportedEncodingException
  {
    try
    {
     //System.out.println(query_statement);
      PreparedStatement delete_stm=conn.prepareStatement(query_statement);
      if (param!=null)
        for (int i=0;i<param.length;i++)
          delete_stm.setString(i+1,param[i]);
      delete_stm.executeUpdate();
      delete_stm.close();
      conn.commit();
      System.out.println("数据删除成功");
    }
    catch(Exception e)
    {
      System.out.println(e);
      conn.rollback();
    }
   }
}

searchtjreport.jsp代码
<jsp:useBean id="query" scope="session" class="weblistbean.QueryBean" />
<%@ page language="java" contentType="text/html;charset=gb2312"
    import="java.sql.*"
   import="java.io.*"
   import="java.util.*"
   import="javax.sql.*"
   import="javax.naming.*"
   import="java.text.*"
   import="java.lang.*"
%>
<%
  String stringSqlString,sumSqlString;  
  String  scontionstar,scontionend;
    scontionstar=request.getParameter("startDate");
    scontionend=request.getParameter("endDate");
 %>
  
<% //2006-08-10
 //scontionstar="2006-08-10";
 //scontionend="2006-09-17";
 
   stringSqlString="SELECT a.ALARM_TIME, b.SITE_NAME, a.INFECTANT_ID, a.STANDARD_VALUE,a.ACTUAL_VALUE";
 
   stringSqlString=stringSqlString+" FROM ALARM a LEFT OUTER JOIN T_DIC_SUBSITE b ON a.SITE_ID = b.SITE_ID WHERE (CONVERT(char(10), a.ALARM_TIME, 120) >= '"+scontionstar+"') AND (CONVERT(char(10), a.ALARM_TIME, 120) <= '"+scontionend+"')";
 
     ResultSet rs=query.getResult(stringSqlString);
   SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //////格式化日期
     DecimalFormat df = new DecimalFormat("##.00");
  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="Scripts/Global.css" type="text/css" rel="stylesheet">
<LINK href="Scripts/Web_Catalog.css" type="text/css" rel="stylesheet">
<title>统计报表</title>
</head>

<body>
<table align="center" id="tabList" bordercolordark="#EBEFFC" bordercolor=#ffffff bordercolorlight="#CCD7F7" class="itemList" border="4" cellpadding="3" cellspacing="2" width="100%">
  <tr align="center" valign="middle">
    <td bgcolor="#1F9CFA" nowrap>月份</td>
    <td bgcolor="#1F9CFA" nowrap>故障数</td>
    <td bgcolor="#1F9CFA" nowrap>超标数</td>
    <td bgcolor="#1F9CFA" nowrap>超标最大值</td>
    <td  bgcolor="#1F9CFA" nowrap colspan="2">有效小时数</td>
  </tr>
  <%             
            while(rs.next()) {
      String curdate=dateformat.format(rs.getTimestamp("ALARM_TIME"));     
     // STANDARD_VALUE=rs.getDouble("STANDARD_VALUE");
      double STANDARD_VALUE=rs.getDouble("STANDARD_VALUE");
               STANDARD_VALUE = Double.parseDouble(df.format(STANDARD_VALUE));

      double ACTUAL_VALUE=rs.getDouble("ACTUAL_VALUE");
                ACTUAL_VALUE = Double.parseDouble(df.format(ACTUAL_VALUE));
   
   
     
         %>
  <tr class='tr1' align="center" valign="middle">
    <td><%=curdate %></td>
    <td><%=rs.getString("SITE_NAME") %></td>
    <td><%=rs.getString("INFECTANT_ID")%></td>
    <td><%=STANDARD_VALUE %></td>
    <td><%=ACTUAL_VALUE %></td>
  </tr>
  <%
             }
   
           %>
  <tr align="center" valign="middle">
    <td>合计</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="2">&nbsp;</td>
  </tr>
</table>
</body>
</html>

 

数据库分页

Datapagebean.java

package weblistbean;
public class Datapagebean{
  private int currentPage;//当前页数
  private int countRecord;//总记录条数
  private int countPage;//总页数
  private int sizePage;//每页记录条数
  public void setAll(int _countRecord,int _sizePage)
//设置四个成员变量的值
{
    countRecord=_countRecord;
    sizePage=_sizePage;
    if(countRecord%sizePage==0)
      countPage=countRecord/sizePage;
    else
      countPage=countRecord/sizePage+1;
    currentPage=1;
  }
  public int getCurrentPage() {
    return currentPage;
  }
  public int getCountPage() {
    return countPage;
  }
  public long getCountRecord() {
    return countRecord;
  }
  public int getSizePage() {
    return sizePage;
  }
  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }
}
breakanalyse.jsp  调用分页代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:useBean id="dbbean" scope="session" class="org.modern.dbbean" />
<jsp:useBean id="query" scope="session" class="weblistbean.QueryBean" />
<jsp:useBean id="datapagebean" scope="session" class="weblistbean.Datapagebean">
</jsp:useBean>

<%@ page language="java" contentType="text/html;charset=gb2312"
    import="java.sql.*"
   import="java.io.*"
   import="java.util.*"
   import="javax.sql.*"
   import="javax.naming.*"
   import="java.text.*"
   import="java.lang.*"
%>
<%
  String sql;
  int rowCount;
  //////////////
 //String stringBREAKDOWN_TYPE_ID="gggggggg";
 //String stringMEMO="hhhhhhhhhhh";
// int Goods_id=5;
// sql="update  BREAKDOWN   set BREAKDOWN_TYPE_ID = '"+stringBREAKDOWN_TYPE_ID+"',MEMO = '"+stringMEMO+"' where BREAKDOWN_ID = "+Goods_id+" ";
  
 // dbbean.update(sql); 
  ////////////////////
   // sql="SELECT ALARM_TIME,SITE_ID,INFECTANT_ID,STANDARD_VALUE,ACTUAL_VALUE, MEMO FROM ALARM";
   sql="SELECT a.BREAKDOWN_TIME, b.SITE_NAME, a.INFECTANT_ID, a.BREAKDOWN_TYPE_ID,";
   sql=sql+"a.BREAK_result,a.BREAKDOWN_ID FROM BREAKDOWN a LEFT OUTER JOIN T_DIC_SUBSITE b ON a.SITE_ID = b.SITE_ID WHERE (CONVERT(char(10), a.BREAKDOWN_TIME, 120) >= '2005-08-10') AND (CONVERT(char(10), a.BREAKDOWN_TIME, 120) <= '2006-09-17') order by BREAKDOWN_ID desc";
 
  //dbbean.getvResult(sql); 
  ResultSet rs=query.getResult(sql);
  //rowCount=dbbean.getrow(); 
  long  BREAK_ID;
 SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //////格式化日期
 /////分页代码/////
  String pages=request.getParameter("dipage");
         //设定Bean的属性
         rs.last();
         int countRecord=rs.getRow();//得到记录的条数
         int countPageRecord=5;//每页5条记录,要设置每页记录条数就更改这个变量的值
         if(pages==null||(pages.trim()).length()==0)
           datapagebean.setAll(countRecord,countPageRecord);
         else
         {
           try
           {
             datapagebean.setCurrentPage(Integer.parseInt(pages));
           }
           catch(Exception e)
           {
             out.println("参数不正确!");
            }
          }
         //当前记录号
         int currentRecord=(datapagebean.getCurrentPage()-1)*datapagebean.getSizePage();
         if(currentRecord==0)
           rs.beforeFirst();
         else
           rs.absolute(currentRecord);
  
     /////分页代码/////
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="Scripts/Global.css" type="text/css" rel="stylesheet">
<LINK href="Scripts/Web_Catalog.css" type="text/css" rel="stylesheet">
<title>故障分析</title>
</head>

<body>
<form name="form1" method="post" action="">
  <table align="center" id="tabList" borderColorDark="#EBEFFC" borderColor=#ffffff borderColorLight="#CCD7F7" class="itemList" border="4" cellpadding="3" cellspacing="2" width="100%">
    <tr align="center" valign="middle">
      <td>故障时间</td>
      <td>站点</td>
      <td>污染物</td>
      <td>故障类型</td>
      <td>故障处理结果</td>
      <td></td>
    </tr>
    <%
  /////分页代码/////
    int i=0;
     /////分页代码/////
          while(rs.next())  {
           // rs.getInt("user_age")
     String curdate=dateformat.format(rs.getTimestamp("BREAKDOWN_TIME"));
    
         %>
    <tr align="center" valign="middle">
      <td><%=curdate %></td>
      <td><%=rs.getString("SITE_NAME") %></td>
      <td><%=rs.getString("INFECTANT_ID") %></td>
      <td><%=rs.getString("BREAKDOWN_TYPE_ID") %></td>
      <td><%=rs.getString("BREAK_result") %></td>
      <td><a href='edit_breakanalyse.jsp?ID=<%=rs.getString("BREAKDOWN_ID")%> '><img src="img/ico_edit.gif" width="15" height="15" alt="" border="0"></a></td>
    </tr>
    <%
             }
           %>
    <tr align="center" valign="middle">
      <td colspan="6">"共"<%=datapagebean.getCountRecord()%>"条记录,共"<%=datapagebean.getCountPage()%>"页,当前第"<%=datapagebean.getCurrentPage()%>"页,每页"<%=datapagebean.getSizePage()%>"条记录,"
 <% if(datapagebean.getCurrentPage()==1)//当前是首页
              ;
           else//当前不是首页
           {
     %>                                   
     <a href=breakanalyse.jsp?dipage=1>首页</a>,<a href='breakanalyse.jsp?dipage=<%=datapagebean.getCurrentPage()-1%>'>上一页</a>,
  <%
   }
  
     if(datapagebean.getCurrentPage()==datapagebean.getCountPage())//当前是末页
              ;
           else//当前不是末页
           {
     %>
     <a href='breakanalyse.jsp?dipage=<%=datapagebean.getCurrentPage()+1%>'>下一页</a>,
     <a href='breakanalyse.jsp?dipage=<%=datapagebean.getCountPage()%>'>末页</a>
    <%}%>
   &nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章