Reputation: 11
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.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$000(StaffLogin.java:13)
at StaffLogin$1.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.
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
}
Upvotes: 1
Views: 21168
Reputation: 70939
It is not central to your problem, but this line
String sql = "SELECT * FROM Staff_Table WHERE Firstname='" + u+"' and Password='"+ p+"'";
is a major security hole. Even though you are "using" PreparedStatement
s, as you are "text" building strings with parameter passed values, you are setting yourself up for a SQL injection attack.
Instead do
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);
}
That way users with names or passwords like "Robert'; DROP TABLE Staff_Table; --" won't become a nightmare in the future.
If you don't understand why this is an issue, consider the scenario where the above "Robert..." user exists.
String u = "Robert'; DROP TABLE Staff_Table; --";
String p = "haha";
String sql = "SELECT * FROM Staff_Table WHERE Firstname='" + u+"' and Password='"+ p+"'";
becomes
String sql = "SELECT * FROM Staff_Table WHERE Firstname='"
+ "Robert'; DROP TABLE Staff_Table; --"
+ "' and Password='"+ p+"'";
or simply
String sql = "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
SELECT * FROM Staff_Table WHERE Firstname='Robert';
DROP TABLE Staff_Table;
--' and Password='haha';
(note the last line is a SQL comment, as it starts with --
).
Upvotes: 2
Reputation: 12737
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()) {
if ("value1".equals(rs.getString("mycolumn")) || "value2".equals(rs.getString("mycolumn"))) {
This way it works fine:
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
String value = rs.getString("mycolumn");
if ("value1".equals(value) || "value2".equals(value)) {
Upvotes: 5
Reputation: 6947
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.
Go here:
Control Panel -> System & Security -> Administrative Tools -> Data Sources (ODBC)
Upvotes: 0