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 :

[JDBC] String => Date Oracle


Sujet :

JDBC Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut [JDBC] String => Date Oracle
    Bonjour,

    J'ai une variable de type String à convertir en type Date Oracle. J'ai essayé la méthode public static Date valueOf(String s) de java.sql.Date mais le résultat n'est pas concluant. Peut-etre que je l'utilise mal...

    Voici mon code:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    String dateCde=EcouteurNewCde.getDateCde().getText();
    Date d1=Date.valueOf(dateCde.substring(6,9)+"-"+dateCde.substring(3,4)+"-"+dateCde.substring(0,1));
    La date présente dans mon TextField (d'où le getText()) est de format JJ/MM/AAAA.

    Merci de votre aide!

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2004
    Messages : 48
    Points : 58
    Points
    58
    Par défaut
    Utilise SimpleDateFormat.parse() plutôt !

    - Colargol

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2004
    Messages : 48
    Points : 58
    Points
    58
    Par défaut
    Par exemple:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    String aDateString = "16.02.2005";
    SimpleDateFormat aFormatter = new SimpleDateFormat("dd.MM.yyyy");
     
    try {
        Date aDate = aFormatter.parse(aDateString);
     
        // aDate = Wed Feb 16 00:00:00 CET 2005
    } catch (ParseException e) {
        e.printStackTrace();
    }
    - Colargol

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    Il y a un problème avec ta méthode, voici l'erreur renvoyée par eclipse pour la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Date aDate = aFormatter.parse(aDateString);
    Type mismatch: cannot convert from Date to Date
    Une idée???

  5. #5
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    aDate doit être de type java.sql.Date alors qu'il faut un java.util.Date.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    java.util.Date aDate = aFormatter.parse(aDateString);

  6. #6
    Membre régulier
    Inscrit en
    Mars 2002
    Messages
    84
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 84
    Points : 98
    Points
    98
    Par défaut
    en effet, c'est ça le problème.

    ma solution :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    sqlDate = new java.sql.Date( ( (java.util.Date) utilDate).getTime());
    Est-ce que quelqu'un connaît une meilleure solution?

  7. #7
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    Non pas à ma connaissance ...

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    J'ai toujours une erreur...

    Voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    String d1="12-12-2005";
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MMM.yy");
    		try{
    			date1 = formatter.parse(d1);
    		}
    		catch (ParseException pe) {pe.printStackTrace();}
    et l'erreur:
    java.text.ParseException: Unparseable date: "12-12-2005"

  9. #9
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    essaie ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    mais pour Oracle, j'ai besoin d'avoir la date sous cette forme: dd-MMM-yy

    Exemple: 10-APR-85

    Alors comment faire?

  11. #11
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    String d1="10-APR-85"; 
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MMM.yy"); 
    try{ 
       date1 = formatter.parse(d1); 
    } 
    catch (ParseException pe) {
       pe.printStackTrace();
    }

  12. #12
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2004
    Messages : 48
    Points : 58
    Points
    58
    Par défaut
    Citation Envoyé par colargol70
    Par exemple:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    String aDateString = "16.02.2005";
    SimpleDateFormat aFormatter = new SimpleDateFormat("dd.MM.yyyy");
     
    try {
        Date aDate = aFormatter.parse(aDateString);
     
        // aDate = Wed Feb 16 00:00:00 CET 2005
    } catch (ParseException e) {
        e.printStackTrace();
    }
    - Colargol
    C'était un exemple !!! Il ne fallait pas le prendre tel quel, mais l'adapter à ton format de date !!!

    - Colargol

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    je vais essayer de reprendre depuis le début!

    Le problème est que j'ai en entrée une chaine du type dd/MM/yyyy.

    Exemple: 10/04/1985

    Et je souhaiterais avoir en sortie une chaine ou une date au format dd-MMM-yy.

    Exemple: 10-APR-85

    Comment cela est-il réalisable???

    Merci!

  14. #14
    Membre éprouvé
    Profil pro
    Architecte technique
    Inscrit en
    Mars 2002
    Messages
    966
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France

    Informations professionnelles :
    Activité : Architecte technique

    Informations forums :
    Inscription : Mars 2002
    Messages : 966
    Points : 1 085
    Points
    1 085
    Par défaut
    Est-ce que ta date en base est de type DATE ou VARCHAR ???

  15. #15
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    Ma date dans la base est de type DATE.

  16. #16
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2004
    Messages : 48
    Points : 58
    Points
    58
    Par défaut
    Alors utilise SimpleDateFormat, tel que décrit plus haut pour créer une date à partir d'une string. Utilise pour ça le format "dd/MM/yyyy". Si tu as une date dans ta BD, pas besoin de reformatter ta date en "dd-MMM-yy", tu peux la passer telle quelle. Utilise un PreparedStatement et fais un setDate() dans ton INSERT, UPDATE ou DELETE.

    - Colargol

  17. #17
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    Pourrais avoir un petit exemple, si ce n'est pas trop demandé???

    Merci!

  18. #18
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2004
    Messages : 48
    Points : 58
    Points
    58
    Par défaut
    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
    try {
        String aDateString = "16/02/2005";
        SimpleDateFormat aFormatter = new SimpleDateFormat("dd/MM/yyyy");
        Date aDate = aFormatter.parse(aDateString);
     
        // [...]
     
        Class.forName("ton.driver.Oracle");
        Connection aConnection = DriverManager.getConnection("ton.URL", "tonUsername", "tonPassword");
        PreparedStatement aPreparedStatement = aConnection.prepareStatement("INSERT INTO taTable(col1, col2, ...) values (?, ?, ...)");
        aPreparedStatement.setDate(1, new java.sql.Date(aDate.getTime()));
        aPreparedStatement.setString(2, "blabla");
     
        // [...]
     
        int aNrOfRows = aPreparedStatement.executeUpdate();
     
        // [...]
     
        aPreparedStatement.close();
        aConnection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    - Colargol

  19. #19
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    92
    Détails du profil
    Informations personnelles :
    Localisation : France, Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2004
    Messages : 92
    Points : 60
    Points
    60
    Par défaut
    J'ai toujours une erreur:

    Voici le code:
    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
    try { 
    		    String aDateString = "16/02/2005"; 
    		    SimpleDateFormat aFormatter = new SimpleDateFormat("dd/MM/yyyy"); 
    		    Date aDate = aFormatter.parse(aDateString); 
    		    Connection base=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:bd1","system","manager");
    		    PreparedStatement aPreparedStatement = base.prepareStatement("INSERT INTO commande(no_cde,date_cde,adr_livr,cp_liv,ville_livr,date_deb_prod,date_livr_prev,client) values (?,?,?,?,?,?,?,?,?)"); 
    		    aPreparedStatement.setString(1,"seqcde.NEXTVAL");
    		    aPreparedStatement.setDate(2, new java.sql.Date(aDate.getTime())); 
    		    aPreparedStatement.setString(3,"");
    		    aPreparedStatement.setString(4,"");
    		    aPreparedStatement.setString(5,"");
    		    aPreparedStatement.setDate(6, new java.sql.Date(aDate.getTime()));
    		    aPreparedStatement.setDate(7, new java.sql.Date(aDate.getTime()));
    		    aPreparedStatement.setInt(8,1);
    		    int aNrOfRows = aPreparedStatement.executeUpdate(); 
    		    aPreparedStatement.close(); 
    		    base.close(); 
    		} catch (Exception sqle) { 
    		    sqle.printStackTrace(); 
    		}
    et l'erreur:
    java.sql.SQLException: Paramètre IN ou OUT absent dans l'index :: 9
    De plus c'est bizarre car j'ai essayé de faire une requete en dur avec java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    INSERT INTO commande VALUES (seqcde.NEXTVAL,'02/16/2005','','     ','','11/12/2004','01/01/2005',1)
    et il me renvoie une erreur
    java.sql.SQLException: ORA-01843: not a valid month
    alors que cette requête passe parfaitement bien sur le SQL+ de Oracle...

  20. #20
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Décembre 2004
    Messages : 48
    Points : 58
    Points
    58
    Par défaut
    Dans ton INSERT, tu spécifies 8 colonnes, mais passes 9 paramètres ("?")!

    En ce qui concerne la date, aucune idée. Je ne sais pas si ta BD travaille en mode mois/jour ou jour/mois, il semblerait que ça bloque à ce niveau-là...

    - Colargol

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Conversion string to date
    Par Fanny23 dans le forum Collection et Stream
    Réponses: 7
    Dernier message: 27/01/2012, 00h24
  2. [Firebird] Convertir une String en date
    Par laffreuxthomas dans le forum SQL
    Réponses: 1
    Dernier message: 04/05/2005, 19h42
  3. conversion String en Date (parse trop permissif)
    Par ciloulou dans le forum Collection et Stream
    Réponses: 2
    Dernier message: 13/09/2004, 18h36
  4. [JDBC] manipulation des dates/heures en Java
    Par kurkLord dans le forum JDBC
    Réponses: 6
    Dernier message: 09/08/2004, 12h49
  5. [JDBC] inserer une date
    Par zozolh2 dans le forum JDBC
    Réponses: 9
    Dernier message: 01/06/2004, 10h28

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