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 :

Transformer le temps-SQL en autre format


Sujet :

JDBC Java

  1. #1
    Membre du Club
    Inscrit en
    Mai 2008
    Messages
    112
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 112
    Points : 44
    Points
    44
    Par défaut Transformer le temps-SQL en autre format
    Salut, chers developpeurs,
    avec votre aide j'acquiers certains infos qui me fon avancer dans mon programme.
    Actuellement j'aimerais formatter le temps-SQL (JJJJ-MM-DD) en un format europeen, c'est à dire un format JJ. MM. AAAA.

    Comment dois-je me prendre.
    quelqu'un a-t-il une idée??

    Voici en fait mon code (voire lignes 160-162)
    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
     
    package gui.dialog;
     
     
    import java.sql.ResultSet;
     
    import gui.MainFrame;
    import gui.panel.CPProjectList;
     
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
     
     
     
    import com.jgoodies.forms.builder.PanelBuilder;
    import com.jgoodies.forms.layout.CellConstraints;
    import com.jgoodies.forms.layout.FormLayout;
     
    import model.ProjectListTableModel;
     
    import utilities.DatabaseConnection;
     
    public class DialogProjectEdit extends JDialog {
     
    	private MainFrame frame = MainFrame.getInstance();
        private DatabaseConnection dbc = frame.getConnectedDB();
     
     
        private int projId = 0;
     
        private JButton btEdit;
        private JButton btCancel;
     
        private JTextField projektName;
     
        private JTextField projektId;
        private JTextField createdDate;
     
     
        public DialogProjectEdit(JFrame frame, boolean modal, int i){
        	super(frame, modal);
        	this.setProjId(i);
     
    		initialize();
     
    		setModal(modal);
    		this.setLocationRelativeTo(null);
        }
     
     
     
    	private void initialize() {
    		this.setTitle("Projekt Ändern");
    		this.setSize(450, 180);
     
    		FormLayout layout = new FormLayout(
    				"right:[40dlu,pref], 3dlu, 40dlu, 7dlu, "
    						+ "right:[40dlu,pref], 3dlu, 60dlu",
    				"p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 14dlu, p");
     
    		PanelBuilder builder = new PanelBuilder(layout);
    		builder.setDefaultDialogBorder();
     
    		CellConstraints cc = new CellConstraints();
    		int row = 1;
     
    		builder.addLabel("Projektname:", cc.xy(1, row));
    		builder.add(getPName(), cc.xyw(3, row++, 5));
    		row++;
     
    		builder.addLabel("ProjektID:", cc.xy(1, row));
    		builder.add(getPId(), cc.xyw(3, row++, 5));
    		row++;
     
    		builder.addLabel("Erstellt am", cc.xy(1, row));
    		builder.add(getCDate(), cc.xyw(3, row++, 5));
    		row++;
     
    		builder.add(getbtEdit(), cc.xy(5, row));
    		builder.add(getbtCancel(), cc.xy(7, row));
    		this.getValues();
     
     
    		this.setContentPane(builder.getPanel());
     
    	}
     
    	private void getValues(){
    		try{
    	        ResultSet rs = dbc.abfrage("select * from project_table where project_id = "+ this.getProjId() +" ;");
    	        while( rs.next() ) {
    	                this.projektId.setText(rs.getString(1));
    	                this.projektName.setText(rs.getString(2));
    	                this.createdDate.setText(rs.getString(3));}
    	        } catch (Exception e) {
    	               e.printStackTrace();
    	        }
    	}
     
    	private void setProjId(int i){
            this.projId = i + 1;
        }
     
    	private int getProjId(){
            return this.projId;
        }
     
    	/**
             * This method initializes tfName
             * 
             * @return javax.swing.JTextField
             */
    	private JTextField getPName() {
    		if (projektName == null) {
    			projektName = new JTextField();
     
    		}
    		return projektName;
    	}
     
    	/**
             * This method initializes tfName
             * 
             * @return javax.swing.JTextField
             */
    	private JTextField getPId() {
    		if (projektId == null) {
    			projektId = new JTextField();
    		}
    		return projektId;
    	}
     
    	/**
             * This method initializes tfName
             * 
             * @return javax.swing.JTextField
             */
    	private JTextField getCDate() {
    		if (createdDate == null) {
    			createdDate = new JTextField();
    		}
    		return createdDate;
    	}
     
    	/**
             * This method initializes btEdit
             * 
             * @return javax.swing.JButton
             */
    	private JButton getbtEdit() {
    		if (btEdit == null) {
    			btEdit = new JButton();
    			btEdit.setText("Ändern");
    			final int id = this.getProjId();
     
    			btEdit.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
     
    					String query = "UPDATE project_table " +
    							"set created_date = \'"
    							+  createdDate.getText() + "\' WHERE project_id = "+ id +";";
     
     
    					if(dbc.setEintrag(query))
    						System.out.println("Query Successed");
    					DialogProjectEdit.this.setVisible(false);
    					DialogProjectEdit.this.dispose();
    					frame.loadPanel(new CPProjectList());
    				}
    			});
    		}
    		return btEdit;
    	}
     
    	/**
             * This method initializes btCancel
             * 
             * @return javax.swing.JButton
             */
    	private JButton getbtCancel() {
    		if (btCancel == null) {
    			btCancel = new JButton();
    			btCancel.setText("Abbrechen");
    			btCancel.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					DialogProjectEdit.this.setVisible(false);
    					DialogProjectEdit.this.dispose();
    				}
    			});
    		}
    		return btCancel;
    	}
     
     
    }

  2. #2
    Rédacteur
    Avatar de CyberChouan
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    2 752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Janvier 2007
    Messages : 2 752
    Points : 4 314
    Points
    4 314
    Par défaut
    Avec la classe SimpleDateFromat regarde le fonctionnement sur l'API Java.

    Sinon, avec des PreparedStatement bien configurés, c'est encore plus simple (et plus propre) puisque la classe java.sql.Date hérite de java.util.Date

  3. #3
    Membre du Club
    Inscrit en
    Mai 2008
    Messages
    112
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 112
    Points : 44
    Points
    44
    Par défaut
    Citation Envoyé par CyberChouan Voir le message
    Avec la classe SimpleDateFromat regarde le fonctionnement sur l'API Java.

    Sinon, avec des PreparedStatement bien configurés, c'est encore plus simple (et plus propre) puisque la classe java.sql.Date hérite de java.util.Date
    Salut!
    J'ai cherché. Mais je dois avouer ne pas savoir comment l'utiliser dans mon code suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     String query = "UPDATE project_table " +
    							"set created_date = \'"
    							+  createdDate.getText() + "\' WHERE project_id = "+ id +";";
    Puisse quelqu'un m'aider à faire le formatage?

    Merci d'avance

  4. #4
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    Il ne faut pas utiliser de Statement, passe au PreparedStatement.
    Ta requête devient (pour un format de saisie JJ/MM/AAAA)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    PreparedStatement pstmt = connection.prepareStatement("UPDATE project_table set created_date=? where project_id=?");
    pstmt.setDate(1, new SimpleDateFormat("dd/MM/yyyy").parse(createdDate.getText()));
    pstmt.setInt(2, id);
    pstmt.execute();

  5. #5
    Membre du Club
    Inscrit en
    Mai 2008
    Messages
    112
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 112
    Points : 44
    Points
    44
    Par défaut
    Citation Envoyé par OButterlin Voir le message
    Il ne faut pas utiliser de Statement, passe au PreparedStatement.
    Ta requête devient (pour un format de saisie JJ/MM/AAAA)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    PreparedStatement pstmt = connection.prepareStatement("UPDATE project_table set created_date=? where project_id=?");
    pstmt.setDate(1, new SimpleDateFormat("dd/MM/yyyy").parse(createdDate.getText()));
    pstmt.setInt(2, id);
    pstmt.execute();
    Salut!
    Je trouve le PreparedStatement complexe et ne sais comment l'utilisr dans mon code. N'existe-t-il pas une simple methode que je puisse utiliser dans mon code. J'aimerais en fait remplacer le code suivant (voir mon premier post):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    String query = "UPDATE project_table " +
    "set created_date = \'"
    +  createdDate.getText() + "\' WHERE project_id = "+ id +";";
    par celui-ci (exemple):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    update proj3_report_table, proj3_report_log_table 
    set proj3_report_table.created_date='2001-01-01' ,   
    proj3_report_log_table.post_time='2001-01-01'
    where proj3_report_table.report_id=1 and proj3_report_log_table.report_id=T1.report_id;
    ceci en transformant directement les dates en format francais (jj. mm. aaaa)

  6. #6
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    Tu peux le faire avec SimpleDateFormat, le problème si tu n'utilises pas de PreparedStatement viendra de la base elle-même.
    Elle est paramétrée pour récupérer des dates String sous un format donné, s'il change, ton application ne fonctionne plus...
    Avec le PreparedStatement, elle fonctionnera toujours... à toi de choisir

  7. #7
    Membre du Club
    Inscrit en
    Mai 2008
    Messages
    112
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 112
    Points : 44
    Points
    44
    Par défaut
    J'ai pu resoudre ce probleme par la methode du SimpleDateFormat. Cependant, j'aimerais que les horaire ne s'affichent pas. Comment m'y prendre??

    voici mon 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
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    private JButton getbtEdit() 
    {
        if (btEdit == null) 
            {
    	    btEdit = new JButton();
           	    btEdit.setText("Change");   
    	    final int id = this.getProjId();
     
    	    btEdit.addActionListener(new java.awt.event.ActionListener() {
    	        public void actionPerformed(java.awt.event.ActionEvent e) {
    		        java.sql.Timestamp sdate = null;
    			try {
    				DateFormat format = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss"); 
    				java.util.Date jdate = format.parse(createdDate.getText().toString());
    					sdate = new java.sql.Timestamp(jdate.getTime());
     
    			      } 
                                  catch(Exception ex) 
                                  {
    			          ex.printStackTrace();
    			       }
     
    			        String query = "UPDATE project_table " +
    							"set created_date = \'"
    							+ sdate + "\' WHERE project_id = "+ id +";";
     
    					if(dbc.setEintrag(query))
    						System.out.println("Query Successed");
    					DialogProjectEdit.this.setVisible(false);
    					DialogProjectEdit.this.dispose();
    					MainFrame.loadPanel(new CPProjectList());
    				}
    			});
    		}
    		return btEdit;
    	}
    et puis:
    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
    public void addRow(ResultSet rs, int i, int n){
            try {
            Vector row = new Vector();
            int projectId = rs.getInt(1);
            String projectName = rs.getString(2);
            String createdDate = rs.getDate(3).toLocaleString();
            String username = rs.getString(4);
     
            row.add(projectId);
            row.add(projectName);
            row.add(createdDate);
            row.add(username);
     
     
            this.data.add(row);
            } catch(Exception e){
                e.printStackTrace();
            }
     
        }
    Merci de votre aide

  8. #8
    Membre éprouvé Avatar de Jidefix
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    742
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations forums :
    Inscription : Septembre 2006
    Messages : 742
    Points : 1 154
    Points
    1 154
    Par défaut
    Bonjour,
    toujours en passant par un SimpleDateFormat


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
    //String createdDate = rs.getDate(3).toLocaleString();
    //devient:
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
    String  createdDate = sdf.format(rs.getDate());

Discussions similaires

  1. SQL SERVER 2000 -Format date
    Par Billouze dans le forum MS SQL Server
    Réponses: 4
    Dernier message: 04/10/2005, 10h51
  2. connexion d'une bd d'un serveur sql à un autre serveur sql
    Par kayser dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 29/07/2005, 12h43
  3. SQL et les formats date : je n'y arrive pas !
    Par chargy dans le forum Requêtes et SQL.
    Réponses: 5
    Dernier message: 23/06/2005, 19h54
  4. Sauvegarde d'une image sous un autre format
    Par gandf dans le forum C++Builder
    Réponses: 4
    Dernier message: 24/02/2004, 09h50
  5. Transformation de requete sql
    Par Spiderben dans le forum Requêtes
    Réponses: 5
    Dernier message: 11/02/2004, 21h49

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