⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example8.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
/* 
 * WebLogic Server Unleashed
 * 
 */


package com.wlsunleashed.jdbc;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


/**
 * This class demonstrates Batch transactions in a database.
 * 
 * @version 1.0
 */
public class Example8 {
    /**
     * The main method - the point of entry into this class
     * 
     * @param args : all run-time args are passed in this String array
     */
    public static void main(String[] args) {
        Example8 obj = new Example8();
        double[] stockArray = new double[] { 5, 7, 6.9, 4, 20 };
        obj.incrementStock(stockArray);
    }

    /**
     * Returns a connection to a pointbase database, whose server is running at
     * the given host and port, and the database being called dbName. The
     * credentials supplied while connecting is also accepted as paramter
     * 
     * @param host : The host where the server is running
     * @param port : The port where the server is listening to
     * @param dbName : the database name
     * @param userid : The user id to use while connecting
     * @param password The password of the specified user
     * 
     * @return an object of type java.sql.Connection
     */
    private Connection getConnection(String host, int port, String dbName, 
                                     String userid, String password) {
        Connection conn = null;

        try {
            Class.forName("com.pointbase.jdbc.jdbcUniversalDriver");

            String url = "jdbc:pointbase:server://" + host + ":" + port + "/"
                         + dbName;
            System.out.println("Using url string " + url);
            conn = DriverManager.getConnection(url, userid, password);
            System.out.println("Obtained Connection");
        } catch (Throwable t) {
            t.printStackTrace();
            conn = null;
        }

        return conn;
    }

    /**
     * increments the stock of the items in the ITEM table based on the
     * stockArray passed as a parameter. The stock array is assumed to have as
     * many entries as there are items in the table
     * 
     * @param stockArray : a list of increments to the stock.
     */
    private void incrementStock(double[] stockArray) {
        System.out.println("Trying to obtain a connection ... ");

        Connection conn = getConnection("localhost", 9092, "demo", "PBSYSADMIN", 
                                        "PBSYSADMIN");

        if (conn == null) {
            return;
        }

        System.out.println("Connection obtained !");

        try {
            conn.setAutoCommit(false);

            Statement stmt = conn.createStatement();

            for (int i = 0; i < stockArray.length; i++) {
                String anSQLStmt = "";
                anSQLStmt = " UPDATE XYZCONF.ITEM SET "
                            + " STOCK_QTY = STOCK_QTY + " + stockArray[i]
                            + " WHERE ITEM_ID = " + (i + 1);
                stmt.addBatch(anSQLStmt);
            }

            int[] updateCount = stmt.executeBatch();


            //time to commit our transactions
            conn.commit();

            for (int i = 0; i < updateCount.length; i++) {
                System.out.println(" op " + (i + 1) + " # Rows affected = "
                                   + updateCount[i]);
            }

            stmt.close();
            conn.close();
        } catch (BatchUpdateException b) {
            System.err.println("SQLException: " + b.getMessage());
            System.err.println("SQLState:  " + b.getSQLState());
            System.err.println("Message:  " + b.getMessage());
            System.err.println("Vendor:  " + b.getErrorCode());
            System.out.print("Update counts:  ");

            int[] updateCounts = b.getUpdateCounts();

            for (int i = 0; i < updateCounts.length; i++) {
                System.err.print(updateCounts[i] + "   ");
            }

            rollback(conn);
        } catch (SQLException t) {
            t.printStackTrace();
            rollback(conn);
        }
    }

    /**
     * Method rollback. rolls back a transaction on a connection
     * 
     * @param conn The Connection object.
     */
    private void rollback(Connection conn) {
        try {
            conn.rollback();
        } catch (Throwable t) {
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -