I know that the database connectivity from Java is something that many people want the answer for. Two possible ways to connect a java applet to a database are:(Submitted by Cliff Berg)1. Using a combination of CGI scripts and Java
In this solution, the java applet sends a request to the HTTP server. The java applet creates a URL object using a CGI token of the form "http://ServerName/cgiScriptName?Params...". The HTTP server thinks that the request is being sent by a browser and executes the cgi scripts returning the results to the "browser" (the server thinks it is returning to the browser but in reality it is returning the results to the URL object). The java applet can then interpret the results and show them in a nicely formatted fashion. This method can be used to return results from any cgi script.I have used this method successfully to get results from Sybase. It is simple. The cgi script that I execute on the server actually runs a stored procedure and returns the results to my applet. Fancy things like, passing parameters, formatting results, using CGI scripts that expect POST/GET requests can all be achieved using this method (and a little work!!)
Below is the code for my sample application. The code works for me but will not work as-is for others because of the cgi script name, sybase server location etc etc. But it can be used by anyone to get the basic idea. I would be more than willing to answer ANY questions regarding this or any related problems. I have used unix shell scripts in this sample. But you can use perl/tcl/c/c++ or anything else on the face of this earth that will run as a cgi script on a HTTP server. After receiving the results, it is upto you and the capabilities of Java to persent them in any which way you choose!
2. Using Java with sockets to talk to the database
This is an exciting but extremelyu laborious, error-prone and sometimes complex implementation that will most probably not run with Netscape or any SSL compliant browser. Netscape 2.02 Beta was supporting java applets that were communicating with a server by opening sockets on the client (browser side). I have been told that they do not support it now in 2.03Beta. So there is no point in writing applets rught now using this method. Hopefully this situation will change.
A similar technique which I have seen used, although less flexible than the approach used by Vivek Pabby, is to call the stored procedure directly from an html page, instead of from an applet. The stored procedure must then generate a correct text/html response, which the browser then displays. This is no different from older ways of accessing databases from html, except that now the generated html can include an applet tag, which could, e.g. draw a graph, or do anything that an applet can do. I myself prefer Pabby's method, since the applet is in control from the beginning.
Native methods can be called in Navigator 2.0 on NT. To use native methods via an applet the applet must load the class with the native methods from a local CLASSPATH directory. Additionally, the dll library for the native method class needs to be in the Navigator\Programs\java\bin subdirectory with the awt dll and others. Finally, the nsjava32.dll library implements the functions such as "makeJavaString", etc. Linking your native library dll with javai.lib does not work. You need to link with the import library for nsjava32.dll instead. Netscape does not seem to ship this. You can construct one by using dumpbin, link, and a lot of editing. But it works if you do this. Long term Netscape needs to conform to the javai.dll exports list and module name; likewise the Java SDK needs to ship a version of javai.lib for javai.dll. The javai.lib in the Java SDK 1.0 seems to be a static library only.Here is an example:
ftp://usfs2as.us.ohio-state.edu/pub/decorum/java/GreetClient/This actually does a DCE RPC from the applet, which is probably not what you need. But it can easily be modified to do something "useful".
execute_java_constructor(...)creates a new Java object from within a native method.execute_java_dynamic_method(...) execute_java_static_method(...)call a method from a native method
To pass in an array object first declare the native method in the java file. Using javah and javah -stubs create the *.h and the *.c file. See the java tutorial at javasoft for an example. In the c implementation file copy the function over from the header file and put in the names associated with the formal parameters. I am using String[] as the object being passed into the c funtion.In the c function unhand() the array object to get access to the java string array and then use makeJavaString() to insert a java string into the array. When the function returns, the java strings are in the array.
NOTE: Pass in a variable so you know what the size of the array is. Here is the code from the c implementation side.
long HelloWorld_getActivity(struct HHelloWorld *javaThis,struct HArrayOfString *jstrArray,long count) { // We Passed in the string array and now we need to fill it int index = 0; for( index = 0; index < count; index++) { // Get string data from anywhere and insert into the array // the body contains an array of characters, this is where // the individual strings are put.. unhand(jstrArray)->body[index] = makeJavaString("Test1", 5); } }To call this from the java side follow the instructions from the javasoft tutorial on native methods to create the java files. The call would look something like this.class Main { public static void main(String[] args) { // HelloWorld is the class that contains the native method HelloWorld HW = new HelloWorld(); // Allocate the string array on the java side. String[] strarr = new String[6]; int tst = 6; // getActivity() is the native method declared in // HelloWorld.java. The c function will insert javastrings // into strarr. HW.getActivity(strarr, tst); // print them out to see if we are succesful. for(int index = 0; index < tst; index++) System.out.println(strarr[index]); } }