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

📄 cachedrowsetexample.java

📁 此程序都是企业级 的数据库开发程序 全面揭示了JAVA对数据库的操作
💻 JAVA
字号:
//  com/wrox/rowset/CachedRowSetExample.java
package com.wrox.rowset;

import java.io.*;
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;

public class CachedRowSetExample {
  CachedRowSet cachedRs;

  public static void main(String[] args) {
    CachedRowSetExample crse = new CachedRowSetExample();
    try {
      crse.populateRowSet();
      boolean done = false;
      BufferedReader in = 
	new BufferedReader(new InputStreamReader(System.in));
      while (!done) {
	System.out.print("Enter a row number (0 to exit) : ");
	String s = in.readLine();
	int result = new Integer(s).intValue();
	if (result == 0) {
	  done = true;
	} else {
	  crse.showRow(result);
	}
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  void populateRowSet() throws ClassNotFoundException, SQLException {
    Class.forName("COM.cloudscape.core.JDBCDriver");
    String url = "jdbc:cloudscape:c:/Code/wrox/database/Wrox4370.db";
    String username = "APP";
    String password = "";
    Connection connection = 
      DriverManager.getConnection(url, username, password);

    cachedRs = new CachedRowSet();
    String sql = "select * from Stores";
    cachedRs.setCommand(sql);
    cachedRs.execute(connection);
    connection.close();
  }

  void showRow(int row) throws SQLException {
    try {
      cachedRs.absolute(row);
    } catch (SQLException e) {
      System.out.println("Caught exception. row=[" + row +
			 "] message=[" + e.getMessage() + "]");
      String message = e.getMessage().toLowerCase();
      if (message.indexOf("invalid cursor position") == -1) {
	//if it's not an invalid position, rethrow the exception
	throw e;
      }
    }

    if (cachedRs.isBeforeFirst()) {
      System.out.println("Index " + row + " is before first row");
    } else if (cachedRs.isAfterLast()) {
      System.out.println("Index " + row + " is after last row");
    } else {
      System.out.println("StoreId : " + cachedRs.getInt("StoreID") +
			 " :: StoreDescription : " + 
			 cachedRs.getString("StoreDescription"));
    }
  }
}

⌨️ 快捷键说明

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