-1

I'm working on a project that wants me to integrate a Microsoft Access database into a Java program. I have successfully connected to the database however my SQL statements are not updating the DB. I have defined variables that read user input and use that information as the conditional for the WHERE statement. I have taken parts of the first query out and have gotten positive results but the whole statement refuses to cooperate. What do I need to change about the first query to make it run?

    result = statement.executeQuery("SELECT slipNumber FROM Slip WHERE (slipOpen = -1 & slipLength >= "  + boatLengthdub + "& slipDepth >= " + boatDepthdub + ")"+ "LIMIT 1" );

    statement.executeQuery("INSERT INTO Slip (slipOpen, boatID) VALUES (0," + boatIDdub + ")");

    System.out.println("Have a" + result);
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
ace7
  • 71
  • 1
  • 2
  • 6

2 Answers2

1

You appear to be trying to use the ampersand character (&) where you should be using the SQL keyword AND. You also should be using PreparedStatement objects to perform parameterized queries, e.g.,

String sql = 
        "SELECT slipNumber FROM Slip " + 
        "WHERE slipOpen = -1 AND slipLength >= ? AND slipDepth >= ? " +
        "LIMIT 1";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDouble(1, boatLengthdub);  // assuming that they are Double values
ps.setDouble(2, boatDepthdub);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
    System.out.println(rs.getInt("slipNumber"));
} else {
    System.out.println("Not found.");
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
1

I fully agree. And when you perform an update, I'd suggest to use 'statement.executeUpdate(query)'.

Fredy Fischer
  • 458
  • 3
  • 12