一個javabean輕鬆實現對資料庫的各種操作

來源:互聯網
上載者:User
資料|資料庫


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輕鬆實現對伺服器端日期格式的檢測與處理!

......

......

......

希望大家尊重作者的勞動,使用時保留作者資訊以及版本資訊等等,謝謝!




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.