JDBC Query
The following Java code executes an SQL SELECT * on the MVFILE.PERSON table. In detail, the code does the following:
-
Creates a connection to Caché using java.sql.Connection.
-
Creates a Statement object and uses it to execute the query.
-
Iterates through the result set displaying the data.
-
Closes the result set and the database connection.
//Create the connection
Class.forName("com.intersys.jdbc.CacheDriver");
String url="jdbc:Cache://localhost:1972/MYACCOUNT";
Connection conn = DriverManager.getConnection(url);
//Execute the query
String query = "Select * from MVFile.Person";
Statement stmt = conn.createStatement();
java.sql.ResultSet rs = stmt.executeQuery(query);
//Retrieve and display the results
ResultSetMetaData rsmd = rs.getMetaData();
int colnum = rsmd.getColumnCount();
while (rs.next()) {
for (int i=1; i<=colnum; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
//Close the result set and database connection
rs.close();
conn.close();
The above code is contained in RunQuery.java. This file is in <cachesys>\Dev\tutorials\mv. Executing the Java Examples contains step-by-step instructions for executing this code.