分享
 
 
 

关于javabean的代码

王朝java/jsp·作者佚名  2007-03-12
窄屏简体版  字體: |||超大  

数据库连接 和 增删改

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>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有