A JavaBean easy to implement a variety of operations on the database

Source: Internet
Author: User
Tags commit implement insert odbc query rollback
Data | database


1. Invoke instance
<%@ 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: Data Source name", "sa", "");

try{

string[] param = {PRM1,PRM2,PRM3};
/* SET SELECT statement * *
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; /* Define SQL statement * *
Public String param[]; /* Query conditions, or a new record * *
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;
}
/* Get query Results/
Public ResultSet getresult ()
{
Try
{
PreparedStatement select_stm=conn.pre Parestatement (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;
}

/* Add record operation to the database/
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 ();
}
}

/* Change operations on data records/
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 ();
}
}
/* Delete data record/
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 ();
}
}
}

I hope you respect the work of the author, the use of retained author information and version information, etc., thank you!

If not very busy later, I will gradually write some articles, some of their development experience to share with you.

Preliminary article title:

* * Easy to establish and manage the database connection pool!

* * Using JavaBean easy to achieve page navigation and data paging display!

* * Using the static method of the final class to easily implement the processing of Chinese garbled!

* * Using JavaBean easy to implement server-side data type detection!

* * Using JavaBean easy to implement the server-side date format detection and processing!

......

......

......

I hope you respect the work of the author, the use of retained author information and version information, etc., thank you!




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.