返回目录
一个javabean轻松实现对数据库的各种操作
  作者:未知 更新人:Legume 来源:未知 更新时间:2004.08.24  投稿信箱: tg*ddvip.com


1.调用实例
<%@ page contentType="text/html" import="operate_db.*,java.sql.*"%>
<jsp:useBean id="querybean" scope="request" class="operate_db.QueryBean" />
<%
String prm1,prm2,prm3;
ResultSet rst=null;
prm1 = ...;
prm2 = ...;
prm3 = ...;
querybean.setConnection("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:数据源名","sa","");

try{

String[] param = {prm1,prm2,prm3};
/*设置select语句*/
querybean.setQuerystatement("Select * from table where field1 = ? and field2=? and field3=?");
querybean.setParam(param);
rst=querybean.getResult();
}catch(Exception e){System.out.println(e); }

while (rst.next()){

%><%=rst.getString(1)%>

<%}%>
2.javabean QueryBean

QueryBean.java:

pacakage operate_db;
/**
* Title: select insert update and delete
* Description: select insert update and delete
* Copyright: Copyright (c) 2001
* @author Lci21
* @version 1.0
*/

import java.sql.*;
public class QueryBean
{
public String query_statement; /*定义sql语句*/
public String param[]; /*查询条件,或者是新的记录*/
public ResultSet result=null;
public Connection conn;

public void setParam(String[] param)
{
this.param=param;
}
public void setQuerystatement(String query_statement)
{
this.query_statement=query_statement;
}
public void setConnection(String driverName,String jdbcURL,String username,String passwd) throws Exception
{
Connection conn1;
Class.forName(driverName);
conn1=DriverManager.getConnection(jdbcURL,username,passwd);
conn1.setAutoCommit(false);
this.conn = conn1;
}
/*获取查询结果*/
public ResultSet getResult()
{
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() 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();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}

/*对数据记录进行更改操作*/
public void updateRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
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();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
/*删除数据记录*/
public void deleteRecord() throws SQLException,java.io.UnsupportedEncodingException
{
try
{
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();
}
catch(Exception e)
{
System.out.println(e);
conn.rollback();
}
}
}

希望大家尊重作者的劳动,使用时保留作者信息以及版本信息等等,谢谢!

如果以后不是很忙的话,我会逐步写一些文章,把自己的一些开发经验与大家分享。

初步的文章标题:

**轻松建立并管理数据库连接池!

**利用javabean轻松实现页面导航及数据分页显示!

**利用最终类的静态方法轻松实现对中文乱码的处理!

**利用javabean轻松实现服务器端对数据类型的检测!

**利用javabean轻松实现对服务器端日期格式的检测与处理!

......

......

......

希望大家尊重作者的劳动,使用时保留作者信息以及版本信息等等,谢谢!