I want to make an applet that can display realtime data, but the data get from another application using Delhpi, from the Delphi Application directly access to SQL database.

I think I'll use socket for communication.
Please provide me the best solution and some code to explain it, and what I need such tutorial or etc.

Who is Participating?
Laminamia063099Connect With a Mentor Commented:
To access any database through java, all you need to use is the JDBC API (Java database connectivity).

Make sure that you've set up ODBC on your computer (do this in the Control Panel).  In the following example, say that your datasource is named: TestDatabase.

Now, in your Java program, import java.sql.*.

The following would make a small query to your datasource and display the results.

//Load the odbc driver...done once per session
//Create a connection
String datasourceURL = "";
String username = "johndoe";
String password = "something";
Connection conn = DriverManager.getConnection(datasourceURL, username, password);
//Create a Statement
Statement st = conn.createStatement();
//Execute SQL Query
ResultSet rs = st.executeQuery("Select colX from TableX");
//Now you can process the results from the Result set object
System.out.println("Processing result set....");
while ({
  System.out.println("ColX Value: " + rs.getString("colX"));

This should do it!  If there are any problems with this code, drop a comment.

Good luck.

Laminamia :)
ovidiucraciunCommented:*; a package that you must study if you want to play with sockets
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Ok, in your Delphi program you must built in the capability to have a listening socket, on some port on the machine its on.  This must be created for the applet to communicate with the program.

In you applet, you must create a socket to connect to the port which the Delphi program is listening on.  Unless you have a signed applet, the Delphi program must be on the same machine that the applet was served from!  

       String host = new String("");
       int port = xxx; //port the delphi program is listening on.
        //Create socket
        socket = new Socket(host, port);
        //Create streams for reading and writing from/to socket
        sIn = new BufferedReader (new      
        sOut = new PrintStream(socket.getOutputStream());

Now you have to initiate the talking between the two programs  by using the input and output streams to the socket that have been created on both ends.

I can't help you with the Delphi half.  I would suggest asking this question on that topic area as well.  I can help you with the Java side.  I'll post more code if you want!

Laminamia :)
JavaBoy060299Author Commented:
Hi Laminamia,

Thanks for your quick response.
but I need more assist from you.

I want to know what is the best model, between the Applet request data from Delphi or Delhpi App send data to the applet if the data have changed.

Would you to redraw your answer ? so others can give their comments.
If none give better answer than you do, then points are yours

Hi JavaBoy...
Since you're using an applet that is connecting through a socket, bidirectional communication is possible.  So in this case, I would suggest that the server will be the one to notify the applet if data have changed.  This is better because it saves the client resources instead of the client spawning a thread which polls for updates from time to time. You would usually implement a client polling for updates if you have a request/response protocol such as HTTP such as if you have a servlet or cgi program in the server-side.


No problem.  I agree with jerch that the Delphi server would notify the client of the data updates.  

When using the socket for communication, whether the client requests or the server just sends the data, the client will still have to read from the socket regularly.  What I would suggest is: if the client does little other than display this data, have it sit it a loop reading from the socket.  When the server sends the data, it will be read from the socket with a call to sIn.readLine() or some such other Input call.  When there is no data in the socket to be read, the client will just wait.  This could be done in one thread, leaving other tasks to the client's main thread.

I can't say this is the best way to do it, only a possible solution.

Another way to set it up would be to have a separate class handle the socket connection and reading.  Whenever data is read, that class could call the class that takes care of the data display, notifying it that the data has arrived and passing it the data as needed.  If this is a way you would like to go, I can help you out here.  Again, it's just one possible solution.

Good luck :)

Laminamia :)
JavaBoy060299Author Commented:
Hi Laminamia,

I have changed my mind, Now I want both client/server site will be build by Java.

Can you give me some simple working examples, using MSSQL/Access db ?


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.