IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

problème c/s


Sujet :

JDBC Java

  1. #1
    Membre habitué
    Inscrit en
    Janvier 2009
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Janvier 2009
    Messages : 12
    Par défaut problème c/s
    j'ai créer un pgme(client/serveur) le serveur
    j'ai entrain de le programme de bdd en mon code mais c'est trés defficile de faire aide moi
    mon code java et
    le serveur:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    import java.io.*; // for input output
    import java.lang.*; // for Threads
    import java.net.*; // sockets
     
     
    public class serverChat {
     
      public static void main(String args[]) throws IOException {
     
        /*******Declarations**********/
        // Ports and Hosts
        int port = 3000; // port to the PDA client
     
        // Sockets
        ServerSocket serverSocket = null; // ServerSocket listening to the Client
        Socket clientSocket = null; // Socket with Client
     
        // Inout/Output for PDA Client
        BufferedReader isClient = null; // BufferedReader from PDA Client
        PrintWriter osClient = null; // OutputStream to PDA Client
     
        // Strings
        String msg_String = null;
        int msg_hash = 0;
     
        int i = 0;
        int Clientconnected = 0; // Connection marker
        /********End declarations*********/
     
        /****************Server Socket Creation**********/
        System.out.print(
            "\nCreation of the ServerSocket\nlistening to port 3000\nfor client.");
        try {
          System.out.print(".");
          serverSocket = new ServerSocket(port); // Creation of the server Socket listening on port 3000
          System.out.print("..started.\n\n");
        }
        catch (IOException e) {
          System.out.print(
              "...failed.\nProblem in the creation of the ServerSocket : " + e);
          System.exit(1);
        }
        try {
          clientSocket = serverSocket.accept(); // now we are connected
          Clientconnected = 1; // we just mark here that we are connected
        }
        catch (IOException e) {
          System.out.println(
              "Unable to deal with BufferedReader and PrintWriter for the clientSocket : " +
              e);
          System.exit(1);
        }
        /***************End of creation of Sockets**********/
     
        try {
          isClient = new BufferedReader(new InputStreamReader(clientSocket.
              getInputStream())); // InputStream from Client
          osClient = new PrintWriter(clientSocket.getOutputStream()); // prepare an OutputStream for the Client
        }
        catch (IOException e) {
          System.out.println("Cant't deal with the streams : " + e);
        }
     
        while (true) {
          try {
            msg_String = isClient.readLine(); // taking the String from it
            System.out.println("String from the client : " + msg_String);
          }
          catch (IOException e) {
            System.out.println("Couldn't get I/O for the connection to: ");
            System.exit(1);
          }
     
          if (msg_String.trim().equals("/bye")) {
            Clientconnected = 0; // the server is now disconnected
            clientSocket.close();
            serverSocket.close();
            System.exit(0);
          }
     
          if (Clientconnected == 1) { // if the client is connected
            try {
              System.out.print("\nComputing...");
              msg_hash = msg_String.hashCode();
              System.out.print("done");
              osClient.println(msg_hash);
              osClient.flush(); // put it onto the network
              System.out.print("...and sent.\n");
            } //try
            catch (Exception e) {
              System.out.println(e);
              System.exit(1);
            } // catch
          } // if Clientconnected
        } // while
        // System.exit(0);
      } // void main
    } // PDAServer class


    et le code pour integri la bdd :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    import
    java.sql.*;
     
    public
    class acces {
     
    public static void main(String[] args) {
     
    try {
     
    Class.forName(
    "sun.jdbc.odbc.JdbcOdbcDriver");
     
    Connection c = DriverManager.getConnection(
    "jdbc:odbc:morph");
     
    Statement s = c.createStatement();
     
    s.executeUpdate(
    " INSERT INTO sac_mot (N°,mot,frequence) VALUES('11','bonjour','20')");
     
    System.
    out.println("OK...");
     
    }
    catch (Exception e) {
     
    System.
    out.println("Errrrreur..."+e.getMessage());
     
    }
     
    }
     
    }
    comment placer le dernier code dande le premier j'ai essai mais c'est trés defficile

  2. #2
    in
    in est déconnecté
    Membre Expert Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Par défaut
    Et bien, c'est un peu la base de la programmation objet ... je te conseille d'aller consulter quelques cours ou tutos en abondance sur le site.

    En gros, il faut que ta class ServerChat ait une référence vers la class acces ...

    Quelque chose comme :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Class ServerChat {
     
       private acces monAcces = null;  // ici un membre de la classe est une référence vers acces
     
       // du coup, on utilise un constructeur
       ServerChat() {
           acces = new acces(...); // tu va surement devoir passer les login/pass en paramètre
     
           // il faut aussi ajouter toute la partie de ton main que tu utilises pour initialiser
       }
     
     
       // et enfin dans ton main
       public static void main(String[] args) {
            ServerChat monServer = new ServerChat(...);
            // là tu appelles les différentes méthodes au lieu de tout coder dans le main
       }
     
    }
    Sinon pour faire trsè court mais très crade, et bien tu ajoutes simplement la référence à "acces" dans ton main. Mais dans tu les cas tu es obligé de créer des méthodes dans la classe acces. Tu ne dois pas appeler le main d'une autre classe ...

    Style :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    public class serverChat {
     
      public static void main(String args[]) throws IOException {
     
        /*******Declarations**********/
        acces monAcces = new acces();
     
       // ...
     
       // et au moment où tu en as besoin tu appelles les méthodes de la classe acces
     
       monAcces.faitUnTruc();
     
    }

    NB : les noms de classe doivent commencer par une majuscule par convention.

Discussions similaires

  1. Problème d'installation oracle 8.1.7 sous NT
    Par Anonymous dans le forum Installation
    Réponses: 7
    Dernier message: 02/08/2002, 15h18
  2. Problème d'impression
    Par IngBen dans le forum C++Builder
    Réponses: 7
    Dernier message: 22/05/2002, 12h37
  3. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 17h10
  4. Réponses: 6
    Dernier message: 25/03/2002, 22h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo