h2 Database Connectivity

Vardyschon18 1 Reputation point
2022-02-06T19:53:58.027+00:00

I am using a H2 Database for my Java project, it has lots of Data stored already, is there any way to connect my H2 Database to a .NET Application in C#? I don't want to change the host of my Database, can someone help? I can't find any tutorials on how to connect my H2 Database

public RegisterDatabase() {
try {
connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
} catch (SQLException e) {
e.printStackTrace();
}
}

public RegisterEntry select(int id) {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = connection.prepareStatement("SELECT * FROM REGISTERENTRY WHERE ENTRYID = ?");
        stmt.setInt(1, id);
        rs = stmt.executeQuery();
        rs.next();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return parsetoObj(rs);
}

public ArrayList<RegisterEntry> selectall() {
    ArrayList<RegisterEntry> entries = new ArrayList<RegisterEntry>();
    PreparedStatement stmt = null;
    try {
        stmt = connection.prepareStatement("SELECT * FROM REGISTERENTRY");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            entries.add(parsetoObj(rs));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return entries;
}

public RegisterEntry parsetoObj(ResultSet rs) {
    int id;
    String teacher;
    String entryType;
    String entryText;
    LocalDateTime date;
    try {
        id = (rs.getInt(1));
        date = rs.getTimestamp(2).toLocalDateTime();
        entryType = rs.getString(3);
        entryText = rs.getString(4);
        teacher = rs.getString(5);
        RegisterEntry entry = new RegisterEntry(id, entryType, teacher, entryText, date);
        return entry;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
public void insert(String line){
    String ar[] = line.split(";");
    int id = (Integer.parseInt(ar[1]));
    LocalDateTime date = LocalDateTime.now();
    String entryType = ar[2];
    String entryText = ar[4];
    String teacher = ar[3];

    try {
        PreparedStatement stmt= connection.prepareStatement("INSERT INTO REGISTERENTRY VALUES(?,?,?,?,?);");
        stmt.setInt(1,id);
        stmt.setTimestamp(2,Timestamp.valueOf(date));
        stmt.setString(3,entryType);
        stmt.setString(4,entryText);
        stmt.setString(5,teacher);
        stmt.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    }
Community Center Not monitored
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Morillo 34,671 Reputation points MVP Volunteer Moderator
    2022-02-06T21:37:24.027+00:00

    Please ask your question on this Google Group intended to give support to H2 database.

    You may get a faster response there.

    Hope this helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.