package com.save4j.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.save4j.Constants;
import com.save4j.model.database.Field;
import com.save4j.model.database.Table;
public class DatabaseUtil {
public static void createTable(Table table) throws SQLException {
Connection conn = DriverManager.getConnection(
Constants.DATABASE_URL,
Constants.DATABASE_USERNAME,
Constants.DATABASE_PASSWORD
);
Statement stmt = conn.createStatement();
StringBuffer buff = new StringBuffer();
buff.append("CREATE TABLE " + table.getName() + "(");
for (String key : table.getFields().keySet()) {
Field field = table.getFields().get(key);
buff.append(field.getName() + " char(50),");
}
// Remove the extra comma at the end
if (buff.charAt(buff.length() - 1) == ',') {
buff.substring(0, buff.length() - 1);
}
buff.append(")");
stmt.executeUpdate(buff.toString());
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}