- Managing databases with Eclipse and the Database Tools - Tutorial
- Eclipse DTP: Configure H2 Datasource using Data Source Explorer
- H2 Frequently Asked Questions
- H2 Tutorial
- Screenshots on adding a database connection
Download the H2 Database Engine
- Open the pom.xml of your project >
- add H2 database engine as a dependency.
Maven repository: http://mvnrepository.com/artifact/com.h2database/h2
To download it, right-click on the Project Name in Eclipse Project Explorer > Maven > Download Sources
Download Eclipse DTP (Data Tools Platform)
- In Eclipse, go to Help > Eclipse Marketplace... >
- search Data Tools Platform >
- click Install >
- select both Extender SDK and Enablement Extender SDK >
- accept licence agreement >
- wait patiently until a popup informs you that the installation was successful and Eclipse needs to be restarted.
Open the "Database Development" perspective
This perspective was installed with the DTP.- In Eclipse, go to Window > Open Perspective > Other... > Database Development
Add Database Connection
- In the Database Development perspective, in the Data Source Explorere, right-click on Database Connections > New... >
- select Generic JDBC and enter a Name for the connection then click Next >
- select H2 Driver from the list of drivers.
- if the list is empty, click New Driver Definition button >
- on the Name/Type tab, click Generic JDBC Driver and give it a unique Driver Name.
- on the JAR list tab, click Add JAR/Zip... > select H2 JAR file from Maven repository > click Open.
- on the Properties tab, enter a Database Name for the database, and the following:
- Connection URL - jdbc:h2:~/test
- Driver Class - org.h2.Driver
- User ID - sa
- click OK.
- click Test Connection to see that the connection works.
- click Finish.
Now you can connect to the database by right-click on the name > Connect.
Once you connected you can edit the tables by right-click on the table name > Data > Edit
Once you connected you can edit the tables by right-click on the table name > Data > Edit
Connect to the Database
To connect to a database, a Java application first needs to load the database driver, and then get a connection. A simple way to do that is using the following code:
import java.sql.*;
public class Test {
public static void main(String[] a)
throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:~/test", "sa", "");
// add application code here
conn.close();
}
}
Create a new Database
By default, H2 creates a new database on the first connection, if the one with the specified name does not exist.
After the jdbc:h2: prefix a path to the database can be given. The ~/test creates a new test database in the user's home directory.
After the jdbc:h2: prefix a path to the database can be given. The ~/test creates a new test database in the user's home directory.
No comments:
Post a Comment