---
title: "jdbc DEMO （sql语句的批处理，事务处理，数据绑定prepare）"
author: "jackson_jar"
date: "2008-10-30T12:42:54Z"
tags: ["JDBC", "MySQL", "Oracle", "SQL"]
slug: "jdbc-demo-sql-prepare"
draft: false
lastmod: "2008-10-30T12:42:54Z"
id: 260003
url: "https://jackson-jar.iteye.com/blog/260003"
---

**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());**  
    **}**  
**}**