package de.tutorials;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Hsql {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", "");
Statement createTableStatement = c.createStatement();
createTableStatement.execute("Create table bmi (minAge NUMERIC, maxAge NUMERIC, minThreshold NUMERIC, maxThreshold NUMERIC, range VARCHAR(100))");
PreparedStatement insertStatement = c.prepareStatement("INSERT into bmi (minAge, maxAge, minThreshold, maxThreshold, range) VALUES (?,?,?, ?,?)");
insertStatement.setInt(1,18 );
insertStatement.setInt(2,24 );
insertStatement.setInt(3, 0);
insertStatement.setInt(4, 19);
insertStatement.setString(5, "Sie sind untergewichtig");
insertStatement.execute();
insertStatement.setInt(1,18 );
insertStatement.setInt(2,24 );
insertStatement.setInt(3, 19);
insertStatement.setInt(4, 24);
insertStatement.setString(5, "Sie haben Ihr Normalgewicht");
insertStatement.execute();
// usw.
//Abfrage per select
PreparedStatement abfrageStatement = c.prepareStatement("Select range from bmi where minAge <= ? AND maxAge >= ? AND minThreshold <= ? AND maxThreshold >= ?");
int alter = 23;
int threshold = 20;
abfrageStatement.setInt(1, alter);
abfrageStatement.setInt(2, alter);
abfrageStatement.setInt(3, threshold);
abfrageStatement.setInt(4, threshold);
ResultSet rs = abfrageStatement.executeQuery();
while(rs.next())
System.out.println(rs.getString("range"));
}
}