• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 11次 | 进入软件测试论坛讨论

领测软件测试网 /*
* 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());
    }
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网