当前位置:编程学习 > JSP >>

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

答案:/*
* 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)
        {
         &nb

上一个:Java常见问题集锦 (1)--来自SUN中国
下一个:一个可以完成读取、打印输出、保存xml等等功能的java例子(xml新手不可不看呀!)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,