语句 stmt 问题 netbeans

huangapple 未分类评论50阅读模式
英文:

Statement stmt problems netbeans

问题

以下是您提供的源代码的翻译部分:

public class AuthentificationClient extends javax.swing.JFrame {

  connexion maconn; 
  Statement stmt1;  

    public AuthentificationClient() {
        initComponents();
    }

    private void numcompteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Bienvenue ret = new  Bienvenue  () ;
        ret.setVisible(true);
        this.dispose();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String requete ="Select * from Compte where numcompte='"+numcompte.getText()+"' and codec = '"+Arrays.toString(code.getPassword())+"'";
        try {
            Statement stmt1 = maconn.Obtenirconnexion().createStatement();
            ResultSet rs= stmt1.executeQuery(requete);
        } catch (SQLException ex) {
            Logger.getLogger(AuthentificationPharmacien.class.getName()).log(Level.SEVERE, null, ex);
        }
        OpClient oc = new  OpClient  () ;
        oc.setVisible(true);
        this.dispose();
    }                                        

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AuthentificationClient().setVisible(true);
            }
        });
    }

这是您提供的源代码的翻译。如果您需要更多帮助,请随时提问。

英文:

I'am working on this source code :

public class AuthentificationClient extends javax.swing.JFrame {

  connexion maconn; 
  Statement stmt1;  

    public AuthentificationClient() {
        initComponents();
    }

    private void numcompteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Bienvenue ret = new  Bienvenue  () ;
        ret.setVisible(true);
        this.dispose();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String requete ="Select * from Compte where numcompte='"+numcompte.getText()+"' and codec = '"+Arrays.toString(code.getPassword())+"'";
        try {
            Statement stmt1 = maconn.Obtenirconnexion().createStatement();
            ResultSet rs= stmt1.executeQuery(requete);
        } catch (SQLException ex) {
            Logger.getLogger(AuthentificationPharmacien.class.getName()).log(Level.SEVERE, null, ex);
        }
        OpClient oc = new  OpClient  () ;
        oc.setVisible(true);
        this.dispose();
    }                                        

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AuthentificationClient().setVisible(true);
            }
        });
    }

There is no error but when I execute, it doesn't work correctly! This is the error message that I receive :
语句 stmt 问题 netbeans

答案1

得分: 0

变量“maconn”从未被赋值,至少在您提供的代码中是这样,因此您可能在一个空对象上调用方法。

英文:

The variable maconn is never assigned, at least in the code you have provided, so you may be calling a method on a null object.

答案2

得分: 0

  1. 使用PreparedStatement替代Statement,以避免SQL注入攻击,同时简化您的代码,例如在使用PreparedStatement时无需将字符串文字括在''内。

  2. 在访问对象(例如maconn)的任何成员(方法/变量)之前,先执行null检查。

    实施方式如下:

    String requete = "Select * from Compte where numcompte = ? and codec = ?";
    PreparedStatement pstmt;
    ResultSet rs;
    try {
        if (maconn != null && maconn.Obtenirconnexion() != null) {
            pstmt = maconn.Obtenirconnexion().prepareStatement(requete);
            pstmt.setString(1, numcompte.getText());
            pstmt.setString(2, Arrays.toString(code.getPassword()));
            rs = pstmt.executeQuery(requete);
            //...
        }
    }
    
英文:
  1. Use PreparedStatement instead of Statement to avoid SQL Injection attack as well to simplify your code e.g. you won't need to enclose string literals within ' ' when you use PreparedStatement.

  2. Perform null check on an object (e.g. maconn) before accessing any member (method/variable) using it.

    Do it as follows:

    String requete ="Select * from Compte where numcompte = ? and codec = ?";
    PreparedStatement pstmt;
    ResultSet rs;
    try {
        if(maconn!=null && maconn.Obtenirconnexion()!=null) {
            pstmt = maconn.Obtenirconnexion().prepareStatement(requete);
            pstmt.setString(1, numcompte.getText());
            pstmt.setString(2, Arrays.toString(code.getPassword()));
            rs = pstmt.executeQuery(requete);
            //...
        }
    }
    

huangapple
  • 本文由 发表于 2020年5月29日 06:55:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/62075876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定