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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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();
}
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.