I have a little problem, I’m making project and I’m sending data from client to server, server sends it to the mysql database.
I have client and admin tabs in client application,
In client tab i type Username and connect to the server, this username is beeing send to the server and server saves is into database in table users.
In admin tab (client application side) i connect to modify some data from database and add new data to the database.
My problem is: step by step
1. I type username, press connect, username string is being send to the server and saved in mysql in table users
2. Next, I go to the admin tab, I want to add some data to be inserted to the mysql (it’s question in string) so I type that question in textField and press add and the problem is that server reads it as a username and insert it to users table not to the questions table.
Here is the code from client where i send username:
String name = client.nameField.getText();
out = new PrintWriter (os, true);
out.println(name);
Here is the code from admin tab where I send data:
DataOutputStream os = new DataOutputStream (socket.getOutputStream());
String name = client.nameField.getText();
String title = Configuration.titleField.getText();
String question = Configuration.questionField.getText();
String answer1 = Configuration.answer1Field.getText();
out = new PrintWriter (os, true);
out.println(title);
out.println(question);
out.println(answer1);
And here is the code where are recieve data on the server side:
while(RunThread){
String name;
name = in.readLine();
st.execute("INSERT INTO "+db+" .poll_users (name) VALUES ('" + name + "')");
String title;
title = in.readLine();
if(title!=null)
st.execute("INSERT INTO "+db+" .poll_titles (title) VALUES ('" + title + "')");
String question;
question = in.readLine();
if(question!=null)
st.execute("INSERT INTO "+db+" .poll_questions (question) VALUES ('" + question + "')");
String answer1;
answer1 = in.readLine();
if(answer1!=null)
st.execute("INSERT INTO "+db+" .poll_answers (answer) VALUES ('" + answer1 + "')");
How to make it working?
Thanks for any help
Maybe you should change your client server protocol from relying on the order of the messages to being more explicit? Might make it more robust.
Maybe you should send lines like this:
Then you can receive the fields in any order..