what is META-INF/persistence.xml in netbeans?
I am building a semester Java project that relies on DB, so I read tutorials about JDBC and I built simple app that reads from a MySQL DB. I tried Netbeans desktop database application to read the generated code, but I found that Netbeans takes a开发者_开发问答 very different approach to connect to the database and I can't understand why it uses a persistence.xml
file and what is the persistence package used for?
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost/mysql?" +
"user=root&password=123456";
Connection con = DriverManager.getConnection(connectionUrl);
Can anyone guide me to tutorials to understand this approach ? And why Netbeans uses this code instead of the usual code I posted ?
It is part of JPA (Java Persistence API). It's roughly said an abstract layer over basic JDBC so that you can interact with the DB on a more object oriented manner without the need to write raw SQL statements and to repeat all the JDBC boilerplate (connection, statement, resultset, etc) over and over.
Here are some resources about JPA:
- Java EE 6 tutorial - JPA
- JPA API javadocs
- Wikipedia - JPA
Probably you have configured the Netbeans project to use persistence which would then by default use JPA. If you don't want to use this, but rather want "plain vanilla" JDBC, then you should create a normal/standard Java project. You however have to write all the JDBC and SQL code yourself.
See also:
- JDBC tutorial
- Java connectivity with MySQL
精彩评论