找不到数据 Java 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10197733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:01:46  来源:igfitidea点击:

No data found Java exception

javasqlms-accessnetbeans

提问by JohnO

I'm new to Java and working on a project. i am trying to retrieve data from MS Access with the use of Netbeans. It was all working fine untill i reinstalled my operating system. Now wen i run the code I get this error. Your help and advice would be much appreciated

我是 Java 新手,正在从事一个项目。我正在尝试使用 Netbeans 从 MS Access 检索数据。一切正常,直到我重新安装操作系统。现在我运行代码我得到这个错误。您的帮助和建议将不胜感激

java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7138)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at connect.ConnectDB(connect.java:24)
    at StaffLogin.formWindowOpened(StaffLogin.java:125)
    at StaffLogin.access
import java.sql.*;
import javax.swing.*;

public class StaffLogin extends javax.swing.JFrame {

Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;

/**
 * Creates new form StaffLogin
 */
public StaffLogin() {
    initComponents();
}
   private void formWindowOpened(java.awt.event.WindowEvent evt)  {                                  
    // TODO add your handling code here:
    conn = connect.ConnectDB();
}                                 

private void cmdloginMouseClicked(java.awt.event.MouseEvent evt)  {                                      
    // TODO add your handling code here:
    conn = connect.ConnectDB();
   String u = txtusername.getText();
   String p = txtpassword.getText();

    String sql = "SELECT * FROM Staff_Table WHERE Firstname='" + u+"' and Password='"+ p+"'";
    try{
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        if (rs.next()){
            JOptionPane.showMessageDialog(null,"Correct Password");
            Interface i = new Interface();
            i.setVisible(true);

        }
        else
            JOptionPane.showMessageDialog(null,"Invalid Username or Password");
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}                                     

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new StaffLogin().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton cmdlogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField txtpassword;
private javax.swing.JTextField txtusername;
// End of variables declaration                   
0(StaffLogin.java:13) at StaffLogin.windowOpened(StaffLogin.java:47) at java.awt.Window.processWindowEvent(Window.java:1859) at javax.swing.JFrame.processWindowEvent(JFrame.java:279) at java.awt.Window.processEvent(Window.java:1820) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Window.dispatchEventImpl(Window.java:2475) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

This is the code, I have left out the 'generated code' and the 'look and feel' also. please let me know if these are needed. Thanks.

这是代码,我也省略了“生成的代码”和“外观”。如果需要这些,请告诉我。谢谢。

ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
    if ("value1".equals(rs.getString("mycolumn")) || "value2".equals(rs.getString("mycolumn"))) {

}

}

回答by André

This typically occurs when you try to read the value of a column multiple times. For example, this may throw "No data found":

当您尝试多次读取列的值时,通常会发生这种情况。例如,这可能会抛出“未找到数据”:

ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
    String value = rs.getString("mycolumn");
    if ("value1".equals(value) || "value2".equals(value)) {

This way it works fine:

这样它工作正常:

String sql = "SELECT * FROM Staff_Table WHERE Firstname='" + u+"' and Password='"+ p+"'";

回答by Edwin Buck

It is not central to your problem, but this line

这不是您问题的核心,但这条线

String sql = "SELECT * FROM Staff_Table WHERE Firstname=? and Password=?"
try{
    pst = conn.prepareStatement(sql);
    pst.setString(1, u);
    pst.setString(2, p);
    rs = pst.executeQuery();
    if (rs.next()){
        JOptionPane.showMessageDialog(null,"Correct Password");
        Interface i = new Interface();
        i.setVisible(true);

    }
    else
        JOptionPane.showMessageDialog(null,"Invalid Username or Password");
}
catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

is a major security hole. Even though you are "using" PreparedStatements, as you are "text" building strings with parameter passed values, you are setting yourself up for a SQL injection attack.

是一个主要的安全漏洞。即使您正在“使用” PreparedStatements,因为您正在“文本”构建带有参数传递值的字符串,您也正在为 SQL 注入攻击做好准备。

Instead do

而是做

String u = "Robert'; DROP TABLE Staff_Table; --";
String p = "haha";
String sql = "SELECT * FROM Staff_Table WHERE Firstname='" + u+"' and Password='"+ p+"'";

That way users with names or passwords like "Robert'; DROP TABLE Staff_Table; --"won't become a nightmare in the future.

这样,具有“Robert'; DROP TABLE Staff_Table; --”之类的名称或密码的用户将来就不会成为噩梦。

If you don't understand why this is an issue, consider the scenario where the above "Robert..." user exists.

如果您不明白为什么会出现这个问题,请考虑上述“Robert...”用户存在的场景。

String sql = "SELECT * FROM Staff_Table WHERE Firstname='" 
  + "Robert'; DROP TABLE Staff_Table; --" 
  + "' and Password='"+ p+"'";

becomes

变成

String sql = "SELECT * FROM Staff_Table WHERE Firstname='Robert'; DROP TABLE Staff_Table; --' and Password='haha';

or simply

或者干脆

SELECT * FROM Staff_Table WHERE Firstname='Robert';
DROP TABLE Staff_Table;
--' and Password='haha';

which gets executed as a compound SQL statement, consisting of the three SQL statements

它作为复合 SQL 语句执行,由三个 SQL 语句组成

##代码##

(note the last line is a SQL comment, as it starts with --).

(注意最后一行是 SQL 注释,因为它以 开头--)。

回答by Steve H.

My best guess is that someone else created the ODBC data source on your old machine, and you haven't created it on your new machine.

我最好的猜测是其他人在您的旧机器上创建了 ODBC 数据源,而您没有在新机器上创建它。

Go here:

到这里:

Control Panel -> System & Security -> Administrative Tools -> Data Sources (ODBC)

控制面板 -> 系统和安全 -> 管理工具 -> 数据源 (ODBC)