I want to create an API using Java Maven, with Spark and H2 database and Sql2o library.
This is the full Error message:
Exceptionorg.sql2o.Sql2oException: Could not acquire a connection from DataSource - IO Exception: "java.io.IOException: The filename, directory name, or volume label syntax is incorrect"; "C:\Users\Claudiu/reviews.db:INIT=RUNSCRIPT from 'classpath:db/init.sql.mv.db" [90031-191] [qtp1133151800-16] ERROR spark.webserver.MatcherFilter - ro.sparkmaven.exc.DaoException: Problem adding Course at ro.sparkmaven.dao.Sql2oCourseDao.add(Sql2oCourseDao.java:28) at ro.sparkmaven.App.lambda$0(App.java:37) at ro.sparkmaven.App$$Lambda$1/1607460018.handle(Unknown Source)
This is the main class:
public class App
{
public static void main(String[] args) {
String connectionString= "jdbc:h2:~/reviews.db:INIT=RUNSCRIPT from 'classpath:db/init.sql";
Sql2o sql2o = new Sql2o(connectionString, "", "");
CourseDao courseDao = new Sql2oCourseDao(sql2o);
Gson gson = new Gson();
post("/courses", "application/json", (req, res) -> {
Course course = gson.fromJson(req.body(), Course.class);
courseDao.add(course);
res.status(201);
res.type("application/json");
return null;
} , gson::toJson);
get("/courses", "application/json", (req, res) -> courseDao.findAll(), gson::toJson);
get("/courses/:id", "application/json", (req, res) -> {
int id;
id = Integer.parseInt(req.params("id"));
//TO DO: What if this not found
Course course = courseDao.findById(id);
return null;
});
after((req,res)->{
res.type("application/json");
});
}
}
This is init.sql:
CREATE TABLE IF NOT EXISTS courses (
id int PRIMARY KEY auto_increment,
name VARCHAR,
url VARCHAR
);
CREATE TABLE IF NOT EXISTS reviews (
id INTEGER PRIMARY KEY auto_increment,
course_id INTEGER,
rating INTEGER,
comment VARCHAR,
FOREIGN KEY(course_id) REFERENCES public.courses(id)
);
Thanks in advance!
As the exception message says, the URL is formatted incorrectly. Use a semicolon instead of a colon before
INIT
. The final'
is missing as well. Try: