- 论坛徽章:
- 0
|
我在Redhat Linux 8.0上安装 Informix IDS 7.3 (or 9.3) ,online成功, 但是jdbc连接不上? 在Windows 2000 jdbc 访问Informix IDS 7.3 成功。
在Linux上:
用的JDBC驱动是最新的:IBM Informix JDBC Driver 2.21.JC3.
用的JDK: Java(TM) 2 SDK, Standard Edition Version 1.4.1
运行程序,提示:Com .informix.asf.IfxASFExcepion: .......
哪位高手可以赐教! 谢谢了
程序如下:ScrollableCursor.java
********************************************************************************
** Overview:
** This JAVA sample program shows the usage of Scrollable ResultSet in
** JDBC 2.0 with Informix JDBC driver v2.21JC2
**
** Software Requirements:
** 1. Informix IDS v7.31 or v9.30
** 2. Informix JDBC Driver v2.21JC2
** 3. Java Development Kit
** 4a. PATH (environment variable) needs to include the bin directory of
** where JDK is installed
** e.g. /<JAVA_HOME>;/bin (On UNIX)
** d:\<JAVA_HOME>;\bin (On Windows).
** 4b. CLASSPATH should include ifxjdbc.jar
** e.g. /informix/jdbc/lib/ifxjdbc.jar (On UNIX)
** d:\informix\jdbc\lib\ifxjdbc.jar (On Windows).
**
** Instructions:
** 1. Compile - javac ScrollableCursor.java
** 2. Execute - java ScrollableCursor host port database server user password
**
** For more information, please see the associated README.txt file.
** ************************************************************************** */
import java.io.*;
import java.sql.*;
public class ScrollableCursor {
static String connURL; // URL to establish JDBC connection
Connection conn; // database connection
ResultSet srs; // result set returned from query
String qry; // string used in query statement(s)
public static void main(String args[]) {
if (args.length == 6) {
// read all arguments from the command line
String hostname = args[0];
String portnum = args[1];
String database = args[2];
String IfxServer = args[3];
String userid = args[4];
String passwd = args[5];
// Informix JDBC driver requires database URL in the following format -
// "jdbc:informix-sqli://hostname:port-number/dbname:INFORMIXSERVER=server-name;user=userid;password=passwd
// construct the database URL using the format shown above
connURL = "jdbc:informix-sqli://" + hostname + ":" + portnum +
"/" + database + ":INFORMIXSERVER=" + IfxServer +
";user=" + userid + ";password=" + passwd;
} else {
System.out.println("\nUSAGE: java ScrollableCursor host port database server user password\n" ;
return;
}
ScrollableCursor sc = new ScrollableCursor();
} // endMain
// Default constructor
ScrollableCursor() {
getDBConnection();
scrollableCursorWithStatement();
scrollableCursorWithPreparedStatement();
closeDBConnection();
} // endConstructor
// Load the driver and establish a coonection with database server
public void getDBConnection() {
try {
// load Informix driver
Class.forName("com.informix.jdbc.IfxDriver" ;
// get a connection to Informix database server
System.out.println("\nOpenning Database Connection ..." ;
conn = DriverManager.getConnection(connURL);
System.out.println("Connection Established." ;
// query statement to be used for testing scrollable cursor
// any simple query can be used for this purpose
qry = "SELECT fname, lname, phone FROM customer";
} catch (ClassNotFoundException cnfe) {
System.out.println("\nERROR: failed to load Informix JDBC Driver - class not found." ;
} catch (SQLException se) {
System.out.println("\nERROR: " + se.getMessage());
se.printStackTrace();
}
} // endFunction
// Close database connection created earlier
public void closeDBConnection() {
try {
System.out.println("\nClosing Database Connection ..." ;
if (conn != null)
conn.close();
System.out.println("Connection Closed." ;
} catch (SQLException se) {
System.out.println("ERROR: " + se.getMessage());
se.printStackTrace();
}
} // endFunction
// Scrollable Cursor with Statement
public void scrollableCursorWithStatement() {
try {
System.out.println("\nScrollable Cursor functionality with 'Statement'" ;
// define a scrollable cursor here
// Note: check with JDBC documentation for more information on
// ResultSet and Concurrency types
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// get the result set by executing the query created earlier
srs = stmt.executeQuery(qry);
// move through the result set and display data at random
testScrollableCursor();
// Cleanup resource(s)
srs.close();
stmt.close();
} catch (SQLException se) {
System.out.println("ERROR: " + se.getMessage());
se.printStackTrace();
}
} // endFunction
// Scrollable Cursor with Prepared Statement
public void scrollableCursorWithPreparedStatement() {
try {
System.out.println("\nScrollable Cursor functionality with 'PreparedStatement'" ;
// define a scrollable cursor here
// Note: check with JDBC documentation for more information on
// ResultSet and Concurrency types
PreparedStatement pstmt = conn.prepareStatement(qry,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// get the result set by executing the query created earlier
srs = pstmt.executeQuery();
// move through the result set and display data at random
testScrollableCursor();
// Cleanup resource(s)
srs.close();
pstmt.close();
} catch (SQLException se) {
System.out.println("ERROR: " + se.getMessage());
se.printStackTrace();
}
} // endFunction
// Traverse through result set with Scrollable Cursor
public void testScrollableCursor() {
try {
srs.last(); // moving to last row
System.out.println("\nTotal record(s) selected: " + srs.getRow());
srs.beforeFirst(); // moving to position before first row
System.out.println("\n** Accessing result set sequentially (normal) **\n" ;
System.out.println(" Customer Name Phone No.");
System.out.println(" ----------------------------- ------------");
while (srs.next()) {
System.out.println(formatRow());
} // endwhile
// pause for user input
waitForUserInput();
srs.beforeFirst(); // moving to position before first row
System.out.println("\n** Accessing result set randomly (feature: scroll cursor) **\n");
System.out.println(" Customer Name Phone No.");
System.out.println(" ----------------------------- ------------");
// Assuming there are 28 rows in the table (customer); the row references here
// and the output may be different based on total number of rows in the table
srs.first(); // moving to first row
System.out.println(formatRow());
srs.absolute(7); // moving to row #7
System.out.println(formatRow());
srs.next(); // moving to row #8
System.out.println(formatRow());
srs.relative(12); // moving to row #20
System.out.println(formatRow());
srs.absolute(-4); // moving to row #4 from last (#25)
System.out.println(formatRow());
if (!srs.isLast()) { // check for last row
srs.last(); // moving to last row
System.out.println(formatRow());
}
if (!srs.isAfterLast()) {
srs.afterLast(); // moving to position after last row
//srs.relative(2); // WRONG: there is no current row
srs.previous(); // moving to last row
System.out.println(formatRow());
}
srs.previous(); // moving to row #27
System.out.println(formatRow());
srs.relative(-17); // moving back 17 rows, relatively (#10)
System.out.println(formatRow());
if (!srs.isFirst()) { // check for first row
srs.absolute(1); // moving to first row
System.out.println(formatRow());
}
// pause for user input
waitForUserInput();
} catch (SQLException se) {
System.out.println("ERROR: " + se.getMessage());
se.printStackTrace();
}
} // endFunction
// Utility Function: formatting the row
public String formatRow() throws SQLException {
StringBuffer sb = new StringBuffer("Row ");
StringBuffer tmp = new StringBuffer(srs.getString(1).trim() + ", " +
srs.getString(2).trim());
for (;tmp.length() < 30;) {
tmp.append(" ");
} // endfor
sb.append((srs.getRow() < 10 ? "#0" : "#") + srs.getRow());
sb.append(": " + tmp);
sb.append(srs.getString(3).trim());
tmp = null;
return sb.toString();
} // endFunction
// Utiltity Function: wait for the user input
public void waitForUserInput() {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
try {
System.out.print("\nPress <enter>; to continue ... ");
br.read();
} catch (Exception e) {}
} // endFunction
}
------------------------完----------------------- |
|