hi all,
i install java application with socket server that connect to access database inside the server and i create a client application that connect to the server and send sql statement to be executed by the server . i install the client application on 5 computers the first entry is inserted correctly but when i move to another computer then access to the old computer it does not allow insertion

Recommended Answers

All 17 Replies

Without seeing the relveant code it's impossible for anyone to know what's wrong with your application.

the code is very long but i will send the server code

  public static final int serverport=44444;
     ResultSet test;
     Statement stmt;
         DataOutputStream out;
         DataInputStream in;
            String k;
    public ServerFastLink(){
        try{
            // SERVERIP = getLocalIpAddress();
                              System.out.println("hhhhhhhhhhhhh");

            ServerSocket server=new ServerSocket(serverport);
            while(true){
                Socket client=server.accept();
                System.out.println("client has been accepted");
              ServerFastLink.ClientThread cli=new ServerFastLink.ClientThread(client);
                cli.start();



            }
        }
        catch(Exception e){

        }
    }

    public static void main(String[] args)   {
     ServerFastLink ser=new ServerFastLink();
 } 
   class ClientThread extends Thread{
   Socket csocket;
   ClientThread(Socket s){
       csocket=s;
   }

   public void run(){
       try{

         String url="jdbc:odbc:fastlink";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
System.out.println("connection");
Properties prop = new Properties (); 
prop.put ("charSet", "Cp1256");
prop.put ("user", ""); 
prop.put ("password", "");
Connection con=DriverManager.getConnection(url,prop);
System.out.println("connection1");
 stmt=con.createStatement();
System.out.println("connection2");
 in=new DataInputStream(csocket.getInputStream());
         out=new DataOutputStream(csocket.getOutputStream());
                System.out.println("you have create the connection");
           k=in.readUTF().toString();

           System.out.println(k);
           k=in.readUTF().toString();
           test=stmt.executeQuery(k);
          String reply="";
          while (test.next()){
              reply=reply+test.getObject("loc").toString()+",";
          }
          reply=reply.substring(0, reply.length()-1);
          System.out.println(reply);
           out.writeUTF(reply);
            k=in.readUTF().toString();
            while (k.equals("finish")==false){
            System.out.println(k);
        test=stmt.executeQuery(k);
       System.out.println("hfjghdkg");

          reply="";
          while (test.next()){
              System.out.println("hfjghdkg");
              reply=reply+test.getObject("employee_name").toString()+",";
          }
          reply=reply.substring(0, reply.length()-1);
          System.out.println(reply);
           out.writeUTF(reply);
            k=in.readUTF().toString();
            }
            k=in.readUTF().toString();
             System.out.println(k);
             test=stmt.executeQuery(k);
             test.next();
             reply=test.getObject("employee_password").toString();
             out.writeUTF(reply);
           System.out.println("finished");
           int ii=0;

       while (ii<=7)
         {
   k=in.readUTF().toString();
          if (k.startsWith("exit")==true){
         break;
          }
          else{
               test=stmt.executeQuery(k);
             if (test.next()){
                 out.writeUTF("Sim Card already exists");
             }               
             else{
                 out.writeUTF("exists");
                  k=in.readUTF().toString();
             System.out.println(k);
             stmt.executeUpdate(k);
             System.out.println("Successful");
           out.writeUTF("Successful");


           }


           }
           }
           Thread    thread =ClientThread.currentThread();
        System.out.println(thread.getName());

        thread.stop();

       }
       catch(Exception e){

       }
   }
   } 
}

the client program consisit of two interface
the first interface establish the connection and the second interface send sql statement to the server here is part of the second interface

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   //get current date time with Date()
   Date insertiondate = new Date();
   //System.out.println(dateFormat.format(date)); don't print it, but save it!
   String yourDate = dateFormat.format(insertiondate);
   System.out.println(yourDate);
String check="";

String temp=text1.getText().toString();
if ( text1.getText().toString().trim().equals("")==true){

     JOptionPane.showMessageDialog(null,"Full Name field can not be empty","ERROR",JOptionPane.INFORMATION_MESSAGE);   
}
else if ( text10.getText().toString().trim().equals("")==true){

     JOptionPane.showMessageDialog(null,"Sim card number field can not be empty","ERROR",JOptionPane.INFORMATION_MESSAGE);   
}
else if ( text11.getText().toString().trim().equals("")==true){

     JOptionPane.showMessageDialog(null,"MSISDN field can not be empty","ERROR",JOptionPane.INFORMATION_MESSAGE);   
}

else{
    ii++;
if(check1.isSelected()==true){
    check=check1.getText();
}
else if (check2.isSelected()==true){
    check=check2.getText();
}
String prerequest="select * from contract where msisdn="+text11.getText();
first_interface.out.writeUTF(prerequest);
   System.out.println(prerequest);
String preanswer=First_Interface.in.readUTF().toString();
   System.out.println(preanswer);
if (preanswer.startsWith("exists")==true){
   System.out.println("***************************");
String name=text1.getText().toString();
 Date dateFromDateChooser = datechooser1.getDate();
String birthdate=String.format("%1$td-%1$tm-%1$tY", dateFromDateChooser);
dateFromDateChooser = datechooser2.getDate();
String contractdate=String.format("%1$td-%1$tm-%1$tY", dateFromDateChooser);
String sql="insert into contract ( full_name , document_type , document_number , profession , dob , nationality , address , phone_number , email , simcard_number , msisdn , imei_number , device_type , device_price , device_model , sim_price , employee_name , date1 ,insertion_date ,location )";
String values="('"+name+"' , '"+check+"' , '"+text2.getText()+"' , '"+text18.getText()+"' , '"+birthdate+"' , '"+text4.getText()+"' , 'street/"+text5.getText()+"-area/"+text6.getText()+"-City/"+text7.getText()+"' , '"
        +text8.getText()+"' , '"+text9.getText()+"' , '"+text10.getText()+"' , "+text11.getText()+" , '"+text12.getText()+"' , '"+text13.getText()+"' , '"+text14.getText()+"' , '"+text15.getText()+"' , '"+text16.getText()+"' , '"+First_Interface.employeename+"' , '"+contractdate+"' , '"+yourDate+"' , '"+First_Interface.location+"')";
System.out.println(sql);
System.out.println(values);
String fin=sql+" values "+values;
first_interface.out.writeUTF(fin);
String answer=first_interface.in.readUTF().toString();
 JOptionPane.showMessageDialog(null,answer,"complete",JOptionPane.INFORMATION_MESSAGE);
 text1.setText("");
 bg1.clearSelection();
 text2.setText("");

 text4.setText("");
 text5.setText("");
 text6.setText("");
 text7.setText("");
 text8.setText("");
 text9.setText("");
 text10.setText("");
 text11.setText("");
 text12.setText("");
 text13.setText("");
  text14.setText("");  text15.setText("");  text16.setText("");/*  text17.setText("");*/ text18.setText("");  
  datechooser1.cleanup();
  datechooser2.cleanup();
System.out.println("###################");
}
else{
  JOptionPane.showMessageDialog(null,preanswer,"ERROR",JOptionPane.INFORMATION_MESSAGE);  
}
} }
      catch (Exception e){

      }
        if (ii>=8){
            this.hide();
        }

How many times have we said this in the Java forum...
Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

There's no point anyone spending time on that code when there could be a perfectly good explanation that you have supressed.

commented: the most reliable reply to "no exceptions are thrown" that always seems to fit ... +14

i did not understand what you want from me to do ??

put ane.printStackTrace(); in all your catch blocks (that's at least 3 different places), and run the application again, so you can see if Java is detecting any errors.

when i put e.printStackTrace() no error displayed

OK, so all three catch blocks now have printStackTraces.
When you say " it does not allow insertion" exactly what do you see? You have lots of println in your code to trace what's happening (which is a very good thing to do!), so what do they tell you when the insert is "not allowed"?

i send sql statement but the server didnot see it. i mean the server didnot recieve sql statement from the client

Is this what you are saying and/or implying?:
the client is sucessfully connected to the server
the client has definitly sent a query
the server has not received a query
you have a printStackTrace in every catch block, but no exceptions are reported

the client send query to the server and the server recieve it and execute it
when another client connect to server the old client can not connect to the server any more

" the old client can not connect to the server any more"
" i send sql statement but the server didnot see it. "

These two quotes are taken from your last 2 posts. If the client cannot connect, how do you send the sql statement?
When you say "cannot connect" exactly what error message do you get that tells you it cannot connect?

I mean it cannot connect if another client program connect to the server from other desktop when it moves from one thread to another it cannot return to the old thread

That makes little sense - each connection is running in its own thread. I have no idea what the "it" is in "it moves from one thread to another..."
What about "I send sql statement" - is that true or not?
What is the answer to the last question in my previous post?

You seem totally confused about what is or isn't happening - your explanations are incomplete and inconsistent. Until you can give a complete consistent description of the problem, including all the relevant messages from the program, you and I are just talking in the dark.

Now you Start to understand me each client Program establish Thread with the server That handle multiple threads I think the problem Is the server receive messages only from the last client (Last thread) only and does not deal with the remaining threads

" I think the problem Is ..."
Maybe it is, maybe it isn't. If your guess about what the problem is was right you would probably have fixed it by now. You have to take this methoidically, starting from a clear statement of the exact problem symptoms.

I didn't spot anything in the code that would stop one thread when another is started. You may have a problem with opening multiple connections to the same database, but (a) that should give you an exception and (b) it wouldn't stop an existing thread from receiving messages (unless there was an exception that terminated the try), but you assure me all your catches will print the exception.
Without proper indentation and structuring the code is far too hard to follow - especially what's inside or outside which loop. There's also stuff that's downright baffling, eg first post lines 54-58 where you read from the connection, display the result, but then ignore that and read another line (which you don't display) to use for the query. Why? Maybe the problem is in executing multiple queries from any one connection?

I've asked repeatedly about the messages that the code produces around the error, and you are obviously unwilling to do that. YOu also keep saying what you think the problem is rather than responding to questions about the exact symptoms.
Anyway, it matters little because I have to go now, and won't be back on DaniWeb until tomorrow. Maybe in the meantime you may consider tidying up the code and concentrating on getting an exact complete problem description.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.