KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestClient


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) GIE Dyade (Groupe BULL / INRIA Research Center) 1997
5  * (C) ExperLog 1999-2000
6  *
7  * @version 1.0
8  * @author Pierre-Yves Gibello (Pierre-Yves.Gibello@experlog.com)
9  */

10
11 import java.sql.*;
12 import java.net.InetAddress JavaDoc;
13
14 /**
15  * This is a sample program for RmiJdbc client/server jdbc Driver
16  * RmiJdbc relies on Java RMI for jdbc objects distribution
17  */

18 public class TestClient {
19
20   public static void main(String JavaDoc args[]) {
21
22     try {
23
24       // Register RmiJdbc Driver in jdbc DriverManager
25
// The call to newInstance() is necessary on some platforms
26
// (with some java VM implementations)
27
Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
28
29       // Test with InstantDB free java database engine
30
// See http://www.lutris.com/products/instantDB/index.html
31
// for info & download
32
String JavaDoc url = "jdbc:idb:sample.prp";
33
34       // RMI host will point to local host
35
// A port number may be specified as 1st argument to the program
36

37       String JavaDoc portSpec = "";
38       if(args.length > 0) {
39         Integer.parseInt(args[0]); // Check port number is an integer
40
portSpec = new String JavaDoc(":" + args[0]);
41       }
42
43       String JavaDoc rmiHost = new String JavaDoc(
44        "//" + InetAddress.getLocalHost().getHostName() + portSpec);
45
46       // RmiJdbc URL is of the form:
47
// jdbc:rmi://<rmiHostName[:port]>/<jdbc-url>
48

49       String JavaDoc jurl = "jdbc:rmi:" + rmiHost + "/" + url;
50       System.out.println("TestClient:" + jurl);
51       Connection c = DriverManager.getConnection(jurl);
52
53       Statement st = c.createStatement();
54       ResultSet rs = st.executeQuery("select * from import1");
55
56       ResultSetMetaData md = rs.getMetaData();
57       while(rs.next()) {
58         System.out.print("\nTUPLE: | ");
59         for(int i=1; i<= md.getColumnCount(); i++) {
60           System.out.print(rs.getString(i) + " | ");
61         }
62       }
63
64       rs.close();
65
66     } catch(Exception JavaDoc e) {
67       e.printStackTrace();
68     }
69   }
70 };
71
72
Popular Tags