-1

I am getting this error when running a login screen. I'm really not sure what the issue is as I followed a tutorial, and for him it worked fine. Here is my code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
Connection con = null;
    PreparedStatement pst = null ; 
    ResultSet rs = null;

try{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Morgan\\Documents\\Database1.accdb");
String sq1 = "select * from user_account where username = '"+ tfusr.getText() +"' and password = '"+ tfpwsd.getText() +"' ";
pst = con.prepareStatement(sq1);
rs = pst.executeQuery();
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Login successfull");
}
else {
JOptionPane.showMessageDialog(null , "Login Failed");
}
}
catch(Exception e){
    System.out.println(e);
}

Here are my dependencies:

dependencies

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • Are you using a **JTextField** for your Password entry or is it a **JPasswordField**? – DevilsHnd - 退職した Aug 11 '23 at 22:14
  • Its a jtext field and in the databse its a short text field – Morgan Joffe Aug 11 '23 at 22:18
  • You're not using the `PreparedStatement` properly. Call `setString` on it, after getting rid of those quotes. Also use try-with-resources for your dB objects. As has been said, use a `JPasswordField` so your code would be `pst.setString(1, new String(pField.getPassword()):` – g00se Aug 12 '23 at 05:47
  • You should post the stack trace to see, where the Exception is produced. Could be a lib/db version mismatch, https://jackcess.sourceforge.io/apidocs/com/healthmarketscience/jackcess/DataType.html#EXT_DATE_TIME . – PeterMmm Aug 12 '23 at 07:17
  • Update your question with a link to the tutorial you followed. – skomisa Aug 13 '23 at 16:50
  • Since you are using JDK 1.8, what version of NetBeans are you using? If you are using any version more recent than 12.5 then that is the cause of your problem, because none of them support JDK 1.8. – skomisa Aug 13 '23 at 17:00

1 Answers1

0

It is probably caused by password being a reserved word.

So, try:

String sq1 = "select * from user_account where username = '" + tfusr.getText() + "' and [password] = '" + tfpwsd.getText() + "'";
Gustav
  • 53,498
  • 7
  • 29
  • 55
  • Tried this , still getting the same error – Morgan Joffe Aug 12 '23 at 19:47
  • _password_ isn't a reserved word. See [Access reserved words](https://support.microsoft.com/en-us/office/learn-about-access-reserved-words-and-symbols-ae9d9ada-3255-4b12-91a9-f855bdd9c5a2#__toc262648753) – skomisa Aug 13 '23 at 16:54
  • @skomisa: Browse for many examples, ie [validate username and password in java and ms access](https://stackoverflow.com/a/36531993/3527297). – Gustav Aug 13 '23 at 18:01
  • If you think the question here is a duplicate of the one you linked to then you shouldn't have answered this question at all. Instead you should have voted to close it as a duplicate. – skomisa Aug 15 '23 at 22:25