2015-10-29 29 views
0

我有一個嚴重的問題,我的應用程序。當我登錄一切順利從數據庫連接檢索information.but當我想修改我的登錄密碼我can't.i不要爲什麼,當我連接到phpmyadmin一切都很棒!請幫忙 !SQL服務器更新查詢與Java NetBeans

try{ 
     Connection con=null; 
      Statement stmt=null; 
      ResultSet rs=null; 
      int m=0; 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=fiche_de_poste;user=sa;password=admin"); 
      stmt = con.createStatement(); 

      rs=stmt.executeQuery("select * from compte"); 

    while(rs.next()){ 
     String hh=jPasswordField2.getText(); 
     String hh2=jPasswordField3.getText(); 
    if(jTextField1.getText().equals(rs.getString("Ncompte"))&&jPasswordField1.getText().equals(rs.getString("Cle"))&&(jPasswordField2.getText().length()>0)&&(jPasswordField3.getText().length()>0)&&(hh.equals(hh2))){ 
     m=1; 
    try{Connection conMOD2=null; 
      Statement stmtMOD2=null; 
      ResultSet rsMOD2=null; 

Class.forName(                                                  
"com.microsoft.sqlserver.jdbc.SQLServerDriver");System.out.println("1"); 




conMOD2=DriverManager.getConnection("same as the previous one"); 
System.out.println("1"); 
stmtMOD2 = conMOD2.createStatement(); 
System.out.println("1"); 
String reqq="Update compte set Cle='"+jPasswordField3.getText()+"' WHERE  
Ncompte='"+jTextField1.getText()+"'"; 
System.out.println("2"); 
System.out.println(reqq); 
PSUpdate = conMOD2.prepareStatement(reqq); 
PSUpdate.executeUpdate(); 
//rsMOD2=stmtMOD2.executeQuery(reqq); 
JOptionPane.showMessageDialog(null, " Mot de passe modifié avec succès   

!!"); 

    }catch(Exception ex){}  



     } 

    } 
    if (m==0) 
     JOptionPane.showMessageDialog(null, "ID ou mot de passe non valide,ou bien un des champs es vide"); 
    }catch(Exception ex){ 
    JOptionPane.showMessageDialog(null, "Ops , Impossible de se connecter à la base de données !"); 
    } 
+0

美化(格式),然後再發布的代碼。什麼是「conMOD2 = DriverManager.getConnection(與前一個相同」);「你真的在你的代碼中有嗎?你得到什麼錯誤信息? – Willmore

+0

不,我有一個像以前one.i有問題發佈這就是爲什麼我刪除它;) – jhon

+0

在SQL UPDATE中有一個錯誤。字段Ncompte是文本類型。文本字段不能使用運算符「=」進行比較。將sql查詢更改爲'UPDATE compte SET Cle =?在哪裏Ncompte喜歡?'。請參閱下面的答案,說明您的問題的工作解決方案。 – Willmore

回答

0

以下是針對您的問題的解決方案。如果你的數據存放在數據庫:fiche_de_poste和fiche_de_poste2:

在數據庫fiche_de_poste是表

CREATE TABLE [dbo].[compte](
    [Cle] [text] , 
    [NCompte] [text] 
) 

在數據庫fiche_de_poste2創建表

CREATE TABLE [dbo].[compte](
    [Cle] [text], 
    [NCompte] [text] 
) 

並且在表fiche_de_poste.dbo.compte

數據

| Cle | NCompte |

cle1 compte1

cle2 compte2表

數據fiche_de_poste2.dbo.compte

| CLE | NCompte |

cle1 compte1

cle2 compte2

編譯並運行應用程序。當應用程序窗口出現輸入數據字段:

在現場「孔特」輸入文字「compte1」

在現場「CLE先例」輸入文字「cle1」

在現場「CLE暴發戶」輸入文本「cle3」

在現場 「CLE暴發戶」 輸入文字 「cle3」

你應該會看到一個如下圖:

enter image description here

單擊按鈕「Changer cle」。

的溶液本身:

package javaapplication1; 

import java.awt.*; 
import java.sql.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class JavaApplication1 
{ 
    public static void main(String[] args) 
    { 
     Runnable r = new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       JFrame f = new JFrame(); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.setPreferredSize(new Dimension(300, 300)); 
       JPanel contentPane = new JPanel(); 
       BoxLayout bl = new BoxLayout(contentPane, BoxLayout.PAGE_AXIS); 
       contentPane.setLayout(bl); 

       final JTextField jTextField1 = new JTextField(); 
       final JPasswordField jPasswordField1 = new JPasswordField(); 
       final JPasswordField jPasswordField2 = new JPasswordField(); 
       final JPasswordField jPasswordField3 = new JPasswordField(); 

       Box b = new Box(BoxLayout.LINE_AXIS); 
       b.add(new JLabel("Compte")); 
       b.add(jTextField1); 
       contentPane.add(b); 

       b = new Box(BoxLayout.LINE_AXIS); 
       b.add(new JLabel("Cle précédent")); 
       b.add(jPasswordField1); 
       contentPane.add(b); 

       b = new Box(BoxLayout.LINE_AXIS); 
       b.add(new JLabel("Cle nouveau")); 
       b.add(jPasswordField2); 
       contentPane.add(b); 

       b = new Box(BoxLayout.LINE_AXIS); 
       b.add(new JLabel("Cle nouveau (répété)")); 
       b.add(jPasswordField3); 
       contentPane.add(b); 

       JButton but = new JButton("Changer cle"); 
       contentPane.add(but); 
       but.addActionListener(new ActionListener() 
       { 
        @Override 
        public void actionPerformed(ActionEvent ae) 
        { 
         try 
         { 
          Connection con = null; 
          Statement stmt = null; 
          ResultSet rs = null; 
          int m = 0; 
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
          con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=fiche_de_poste"); 
          stmt = con.createStatement(); 

          rs = stmt.executeQuery("select * from compte"); 

          while (rs.next()) 
          { 
           String hh = jPasswordField2.getText(); 
           String hh2 = jPasswordField3.getText(); 
           if (jTextField1.getText().equals(rs.getString("Ncompte")) 
             && jPasswordField1.getText().equals(rs.getString("Cle")) 
             && jPasswordField2.getText().length() > 0 
             && jPasswordField3.getText().length() > 0 
             && hh.equals(hh2)) 
           { 
            m = 1; 
            try 
            { 
             Connection con2 = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=fiche_de_poste2"); 

             String reqq = "UPDATE compte SET Cle = ? WHERE Ncompte LIKE ?"; 
             PreparedStatement ps = con2.prepareStatement(reqq); 
             ps.setString(1, jPasswordField3.getText()); 
             ps.setString(2, jTextField1.getText()); 

             int rowsUpdated = ps.executeUpdate(); 
             String msg = String.format("%d rangées mise à jour", rowsUpdated); 
             if (0 < rowsUpdated) 
             { 
              msg = msg + " Mot de passe modifié avec succès."; 
             } 
             else 
             { 
              msg = msg + " Mot de passe ne modifié pas."; 
             } 
             JOptionPane.showMessageDialog(null, msg); 
            } 
            catch (SQLException sqlex) 
            { 
             System.out.println("SQL exception message " + sqlex.getMessage()); 
            } 
           } 
          } 
          if (m == 0) 
          { 
           JOptionPane.showMessageDialog(null, "ID ou mot de passe non valide,ou bien un des champs es vide"); 
          } 
         } 
         catch (SQLException es) 
         { 
          es.printStackTrace(); 
         } 
         catch (Exception ex) 
         { 
          JOptionPane.showMessageDialog(null, "Ops , Impossible de se connecter à la base de données !"); 
         } 
        } 
       }); 

       f.setContentPane(contentPane); 

       f.pack(); 
       f.setVisible(true); 
      } 
     }; 
     EventQueue.invokeLater(r); 
    } 
} 
+0

謝謝你兄弟,但它不是相同的connection.i不知道真的wxhat正在發生 – jhon

+0

okkk okk sppokiii我現在得到了你用rowsupdated作爲int ......我會盡量讓你知道 – jhon

+0

好的,rowsupdated來自executeUpdate的結果(請參閱http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate()) 。 – Willmore