英文:
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 :
答案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
-
使用PreparedStatement替代
Statement
,以避免SQL注入攻击,同时简化您的代码,例如在使用PreparedStatement
时无需将字符串文字括在''
内。 -
在访问对象(例如
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); //... } }
英文:
-
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 usePreparedStatement
. -
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); //... } }
专注分享java语言的经验与见解,让所有开发者获益!
评论