Trying to execute an insert then get the last id value but I get that the select statement fails, however it works when I throw it into mysql workbench it works fine. What is the java code failing?
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
import java.sql.Statement
import java.sql.ResultSet
String url = "jdbc:mysql://localhost:3306/";
String user = "root"
String password = "password"
Connection connection = null;
try {
System.out.println("Connecting database...");
connection = DriverManager.getConnection(url,user,password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeUpdate("INSERT INTO table_listnames (name) VALUES ('alex'); SELECT LAST_INSERT_ID() as lastId;");
while (rs.next()) {
String lastId = rs.getString("lastId");
println(lastId)
}
println("closing now")
stmt.close()
connection.close()
} catch (SQLException e) {
throw new RuntimeException("Cannot connect the database!", e);
} finally {
System.out.println("Closing the connection.");
if (connection != null) try {
connection.close();
} catch (SQLException ignore) {}
}