一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare)

43 篇文章 0 订阅
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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值