I'm developing some small Client-Server program in Java, using MySQL for DB and java swing for the client, and I would like to know what is the best way to transfer data from client to server and back ? I read about Socket but is it can transfer the whole table ? or I need to transfer the data one by one ? there is better tool ? or maybe Socket can transfer the whole table ? And another question is: Is it better to use one frame instead of many ? cause it's more convinience to use many. Thanks a lot.
-
google "mysql data replication" – ScaisEdge Oct 19 '16 at 09:50
-
Welcome to SO :-) What do you mean with "transfer the db table"? display the data? Use a client db connection on the client side and have sql access? – Stefan Hegny Oct 19 '16 at 09:51
-
Thanks :) Yes, I'm trying to display a table as it is on a window, I build a new frame with JTable, and I manage to connect with the server to MySQL, and now I just want to pass the result of the query from the server to the client – Rotem Uzan Oct 19 '16 at 11:40
-
*"Is it better to use one frame instead of many ?"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) Voting to close the question(s) as 'too broad' given that SO is a Q&A site that works best when each thread has a single clearly defined inquiry, described in the title. It is not a help desk. – Andrew Thompson Oct 19 '16 at 13:53
1 Answers
You are developing a Client-server architecture.
Simply send your query to server using PreparedStatement
, execute it usingexecuteQuery()
and store the table in Resultset object
.
it's simple, easy to use and less complicated method.
The detailed steps are:
Register Driver
Class.forName("com.mysql.jdbc.Driver");
//This One's For MySQL. For other DB, you have to change the driver nameEstablish Connection
Connection con = DriverManager.getConnection("jdbc:mysql://your database ip address:port Number", "Database Username", "Database Password");
PreparedStatement
PreparedStatement ps = con.prepareStatement("Your SQL Query Here");
Execute and get results
In case of DML:
ps.executeUpdate();
In case of DQL:
ResultSet rs = ps.executeQuery();
//This one will be used in your case
Now you have the result of the query executed in your rs
object. deal with it as you wish.
If satisfied, please Accept!!!

- 273
- 3
- 14
-
Thanks for your answer, the dificault part for me is the communication between the server and the client, i manage to send query from the server to the DB and get the result, but to send query from the client to the server or to send the table (result) to the client it's the part where I wonder if there is an option to send the whole table to the client in one variable ? like ResultSet ? – Rotem Uzan Oct 20 '16 at 07:50