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

📄 example1.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.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * This class demonstrates the use of the Connection interface.
 * 
 * @version 1.0
 */
public class Example1 {
    /**
     * 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) {
        Example1 obj = new Example1();
        System.out.println("Trying to obtain a connection ... ");

        Connection conn =
            obj.getConnection(
                "localhost",
                9092,
                "demo",
                "PBPUBLIC",
                "PBPUBLIC");

        if (conn == null) {
            return;
        }

        System.out.println("Connection obtained !");
        obj.printBasicConnectionInfo(conn);
    }

    /**
     * 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;
    }

    /**
     * printBasicConnectionInfo prints some basic information from the passed
     * Connection object.
     * 
     * @param aConnection : Object of type java.sql.Connection
     */
    private void printBasicConnectionInfo(Connection aConnection) {
        try {
            System.out.println(" Catalog name = " + aConnection.getCatalog());
            System.out.println(
                " Transaction isolation = "
                    + aConnection.getTransactionIsolation());
            System.out.println(
                " Is it read only ? " + aConnection.isReadOnly());
            System.out.println(" About to close the connection .. ");
            aConnection.close();
            System.out.println(" Operation succeeded. Let us make sure .. ");

            if (aConnection.isClosed()) {
                System.out.println(" Connection closed ! ");
            } else {
                System.out.println(" Connection is still open! ");
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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