Reputation: 1
I am new to the Play framework.I connected Play with mysql.But the data I am submitting through the form I have created is not adding in the database table.I am getting an exception
[PersistenceException: java.sql.SQLException: Connection is closed!]
The code I used is
public static Result addBar(){
Bar bar= Form.form(Bar.class).bindFromRequest().get();
bar.save();
return redirect(routes.Application.index());
}
Upvotes: 0
Views: 324
Reputation: 11
In play you don't need to add a database driver manually. Just add the MySQL dependency to your "build.sbt" file like described in the links below from the documentation. For example: libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.18"
.
http://www.playframework.com/documentation/2.2.x/JavaDatabase http://www.playframework.com/documentation/2.2.x/SBTDependencies
After you added the dependency you have to add the connection information in the "conf/application.conf" file. Therefore you need to uncomment the following lines and add your specific informations.
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://host/db?characterEncoding=UTF-8"
db.default.user=yourdb
db.default.password="supersecretpw"
If you want to use an ORM you can start with "ebean" by adding libraryDependencies += javaEbean
to the dependencies and uncomment the following line in the "conf/application.conf" file:
ebean.default="models.*"
This tells play that your model classes are stored in the package "models".
To use ebeans methods your classes have to be annotated with @Entity
and need to extend play.db.ebean.Model
.
The documentation at playframework.com on this topic is very detailed.
I hope that helped.
Upvotes: 1