【问题标题】:sql server update query with java netbeans使用 java netbeans 的 sql server 更新查询
【发布时间】:2015-10-29 10:46:46
【问题描述】:

我的应用程序出现严重问题。当我登录时,从与数据库的连接到检索信息,一切都很顺利。但是当我想修改我的登录密码时,我不能。我不想要为什么,并且当我连接到 phpmyadmin 时,一切都很好!请帮忙!

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 !");
    }

【问题讨论】:

  • 在发布之前美化(格式化)代码。什么是 "conMOD2=DriverManager.getConnection("same as the previous one"); "你的代码中真的有它吗?你得到什么错误信息?
  • 不,我有一个和上一个一样的。我在发布时遇到了问题,这就是我删除它的原因;)
  • 在 SQL UPDATE 中有一个错误。字段 Ncompte 是文本类型。无法使用运算符“=”比较文本字段。将 sql 查询更改为 UPDATE compte SET Cle = ? WHERE Ncompte LIKE ? 。请参阅下面的答案,该答案说明了您的问题的有效解决方案。

标签: java sql-server netbeans


【解决方案1】:

以下是针对您的问题的有效解决方案。如果您的数据放入数据库: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中有数据

|克莱| NCompte |

cle1 compte1

cle2 compte2

fiche_de_poste2.dbo.compte中的数据

|克莱| NCompte |

cle1 compte1

cle2 compte2

编译并运行应用程序。当应用程序窗口出现时,在字段中输入数据:

在“Compte”字段中输入文本“compte1”

在“Cle 先例”字段中输入文本“cle1”

在“Cle nouveau”字段中输入文本“cle3”

在“Cle nouveau”字段中输入文本“cle3”

您应该会看到以下图像:

单击“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);
    }
}

【讨论】:

  • 谢谢你的兄弟,但它不是同一个连接。我真的不知道发生了什么
  • okkk okk sppokiii 我现在明白了,你使用 rowsupdated 作为 int ......我会试着让你知道
  • 好的,rowsupdated 来自 executeUpdate 的结果(见docs.oracle.com/javase/7/docs/api/java/sql/…)。
  • 噗噗,还是不行!!它不只是在行中工作:int rowsUpdated = ps.executeUpdate();
  • 确定它不起作用。我们有一个准备好的声明,必须为它设置值。 :/ 添加代码 String reqq = "UPDATE compte SET Cle = ? WHERE Ncompte = ?"; PreparedStatement ps = con2.prepareStatement(reqq); ps.setString(1, jPasswordField3.getText()); ps.setInt(2, Integer.valueOf(jTextField1.getText()));。查看更新的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2016-04-25
  • 1970-01-01
相关资源
最近更新 更多