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

📄 example5.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;
import java.sql.Statement;
import java.sql.ResultSet;

/**
 * This class demonstrates a Scrollable result set
 *
 * @version 1.0
 */
public class Example5 {

	/**
 	* 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) {
		Example5 obj = new Example5();
		obj.testScrollableResultset();
	}
	
	/**
	 * 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 testScrollableResultset()
	{
		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 !" );

		String anSQLStmt = " UPDATE XYZCONF.ITEM SET " +
						   " STOCK_QTY = STOCK_QTY + ? " + 
						   " WHERE ITEM_ID = ? "  ;
		try
		{
			Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
													ResultSet.CONCUR_READ_ONLY);
			
			ResultSet rs = stmt.executeQuery( " SELECT * FROM XYZCONF.ITEM " );													
			
			System.out.println("printing descriptions in regular order ..." );
			rs.beforeFirst();
			System.out.println(" isBeforeFirst ? " + rs.isBeforeFirst() );
			while (rs.next())
			{
				System.out.println("row id = " + rs.getRow() );
				System.out.println( " is first row = " + rs.isFirst() );	
				System.out.println( " is last row = " + rs.isLast() );	
				System.out.println(rs.getString(2));
			}

			System.out.println("printing descriptions in reverse order ..." );
			rs.afterLast();
			System.out.println(" isAfterlast? " + rs.isAfterLast() );
			int rowCount = 0;
			while (rs.previous())
			{
				System.out.println(rs.getString(2));
				rowCount ++ ;
			}
			
			if (rowCount > 3)
			{
				System.out.println("Seeking row = " + (rowCount - 2));
				rs.absolute(-3);
				System.out.println(" getRow = " + rs.getRow());
				System.out.println(" desc = " + rs.getString(2));
					
				System.out.println("moving up one row " );
				rs.relative(-1);
				System.out.println(" getRow = " + rs.getRow());
				System.out.println(" desc = " + rs.getString(2));

				System.out.println("moving down two rows " );
				rs.relative(2);
				System.out.println(" getRow = " + rs.getRow());
				System.out.println(" desc = " + rs.getString(2));

				
			}
			
			stmt.close();
			conn.close();
		}
		catch (SQLException t)
		{
			t.printStackTrace();	
		}
	}
	
	
	/**
	 * 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 ;
	}
}

⌨️ 快捷键说明

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