欢迎光临
我们一直在努力

一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare,)-JSP教程,资料/其它

建站超值云服务器,限时71元/月

/*
* created by intellij idea.
* user: administrator
* date: mar 26, 2002
* time: 3:24:12 pm
* to change template for new class use
* code style | class templates options (tools | ide options).
*/

package com.chinacountry.databases;
import java.sql.*;
import java.util.stringtokenizer;
public class conndb
{
    string sdbdriver = "org.gjt.mm.mysql.driver";
    string sconnstr = "jdbc:mysql://10.100.27.13:3306/db1";
    connection cn = null;
    statement stmt;
    boolean autocommit;
    private string dbtype="mysql";
    //private string dbtype="oracle";
    private conndb()
    {
       init();
    }
    private void init()
    {
        try
        {
            class.forname(sdbdriver).newinstance();
            cn = drivermanager.getconnection(sconnstr,"naxweb","naxweb");

        }
        catch(exception e)
        {
            system.err.println("conndb(): " + e.getmessage());
        }
    }
    public static conndb getnewinstance()
    {
        return new conndb();
    }
    //数据绑定的资料好像很少,有空给大家一个例子。在这里只能返回preparedstatement。
    public preparedstatement getpreparedstmt(string sql) throws sqlexception
    {
        preparedstatement prestmt=null;
        try
        {
            prestmt = cn.preparestatement(sql);
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            throw ex;
        }
        return prestmt;
    }
    public void begintrans()  throws sqlexception
    {   try
        {
        autocommit=cn.getautocommit();
        cn.setautocommit(false);
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.print("begintrans errors");
            throw ex;
        }
    }
    public void commit()  throws sqlexception
    {
        try
        {
            cn.commit();
            cn.setautocommit(autocommit);
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.print("commit errors");
            throw ex;
        }
    }
    public void rollback()
    {
        try
        {
            cn.rollback();
            cn.setautocommit(autocommit);
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.print("rollback errors");
            //throw ex;
        }
    }
    public boolean getautocommit()  throws sqlexception
    {
        boolean result=false;
        try
        {
            result=cn.getautocommit();
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.println("getautocommit fail"+ex.getmessage());
            throw ex;
        }
        return result;
    }
    //默认的情况下一次executequery(string sql)是一次事务。
    //但是可以调用begintrans(),然后多次executequery(string sql),最后commit()实现多sql的事务处理(注意在这种情况下如果发生违例,千万不要忘了在catch(){调用rollback()})。
    //
    public resultset executequery(string sql) throws sqlexception
    {
        resultset rs = null;
        try
        {
            stmt=cn.createstatement();
            rs = stmt.executequery(sql);
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.println("conndb.executequery:"+ex.getmessage());
            throw ex;
        }
        return rs;
    }
    public void executeupdate(string sql) throws sqlexception
    {
        try
        {
            stmt=cn.createstatement();
            stmt.executeupdate(sql);
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.println("conndb.executeupdate:"+ex.getmessage());
            throw ex;
        }
    }
    //method dobatch 的参数sql,是由一些sql语句拼起来的,用;隔开。可以将许多的sql放在一个事物中,一次执行。
    public int[] dobatch(string sql)  throws sqlexception
    {
        int[] rowresult=null;
        string a;
        try
        {
            //boolean autocommit=cn.getautocommit();
            //cn.setautocommit(false);
            stmt=cn.createstatement();
            stringtokenizer st = new stringtokenizer(sql,";");
             while (st.hasmoretokens())
             {
                 a=st.nexttoken();
                 stmt.addbatch(a);
             }
             rowresult=stmt.executebatch();
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.println("conndb.dobatch:"+ex.getmessage());
            throw ex;
        }
        return rowresult;
    }
    public string getdbtype()
    {
        return dbtype;
    }
    public void close() throws sqlexception
    {
        try
        {
            stmt.close();
            stmt=null;
            cn.close();
            cn=null;
        }
        catch(sqlexception ex)
        {
            ex.printstacktrace();
            system.out.println("closeing connection fail"+ex.getmessage());
            throw ex;

        }
    }
    public static void main(string[] args) throws exception
    {
            conndb con=conndb.getnewinstance();
            system.out.print(con.getdbtype());
    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare,)-JSP教程,资料/其它
分享到: 更多 (0)