I am fairly new to Eclipse & Java and currently work on a project where I need to implement my first database.
Therefore, I tried to connect Eclipse and the H2 Database. While the H2 database part works just fine by itself, I can't figure out how to connect it to Eclipse.
I created the following class and tried to do everything as stated on the website:
package srpTracking;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.tools.DeleteDbFiles;
public class DatabaseConnector {
public static void main(String... args) throws Exception {
DeleteDbFiles.execute("~", "test", true);
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test");
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar(255))");
stat.execute("insert into test values(1, 'Hello')");
ResultSet rs;
rs = stat.executeQuery("select * from test");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
stat.close();
conn.close();
}
I get identifier errors for the last two lines of code, but it also doesn't connect anything.
I copied the H2.jar file into the project folder in a subfolder called lib.
Unfortunately I can't for some reason install the DTP plugin, because I am apparently missing a 'org.eclipse.core.runtime' file.
What do I need to change about my code connect Java and H2? Also, do I need to copy and H2 files into specific folders?
stat.close()andcon.close()statements are outside themainmethod and therefore syntax errors: your code doesn't compile. The problem with the DTP plugin is unrelated and an Eclipse configuration/installation problem. Please edit your question to restrict it to only one problem, and ask a new, separate question for the other problem.