Hallo Forum,
ich möchte ein PostgreSQL-Schema in Oracle migrieren. Dafür brauche ich für jede Tabelle die Anzahl der Spalten. Ich habe verschiedene Methoden im Netz gefunden und ausprobiert, aber kriege nicht das Richtige raus.
Wie kann ich die Anzahl der Spalten am sinnvollsten ermitteln?
Hier mein Code:
=========================================================
public void getSch(ActionEvent event2) {
Statement stmt = null;
try {
stmt = con1.createStatement();
} catch (SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
try {
DatabaseMetaData dbmd = con1.getMetaData();
try (ResultSet tables = dbmd.getTables(null, "public", "%", new String[] { "TABLE" })) {
while (tables.next()) {
// Create Table anfangen
String myCreateTable = "CREATE TABLE IF NOT EXISTS ";
String tName = tables.getString("TABLE_NAME");
ResultSet columns = dbmd.getColumns(null, null, tName, null);
// Create Table fortführen
myCreateTable += tName + "(";
int y=0;
int anzahl Spalten = ?????????????????????????????????????????
while (columns.next()) {
// Spalten
String colName = columns.getString("COLUMN_NAME");
String colType = columns.getString("TYPE_NAME");
String colSize = columns.getString("COLUMN_SIZE"); // String, weil manche Längen Strings sind
// convert to oracle
if ("serial".equals(colType)) { colType = "NUMBER"; }
if ("int4".equals(colType)) { colType = "NUMBER"; }
if ("text".equals(colType)) { colType = "CLOB"; }
if ("varchar".equals(colType)) { colType = "VARCHAR2"; }
// Create Table fortführen
myCreateTable += colName+" "+colType+"("+colSize+") ";
// Create Table fortführen
//Wenn letztes Element des Resultsets erreicht ist, dann Klammer zu, ansonsten ein Komma
if (y < anzahlSpalten) {
myCreateTable += ",";
} else {
myCreateTable += ")";
}
}
// Schema - Migration
System.out.println(myCreateTable);
}
}
} catch (SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
==========================================================
Danke im Voraus
capri
ich möchte ein PostgreSQL-Schema in Oracle migrieren. Dafür brauche ich für jede Tabelle die Anzahl der Spalten. Ich habe verschiedene Methoden im Netz gefunden und ausprobiert, aber kriege nicht das Richtige raus.
Wie kann ich die Anzahl der Spalten am sinnvollsten ermitteln?
Hier mein Code:
=========================================================
public void getSch(ActionEvent event2) {
Statement stmt = null;
try {
stmt = con1.createStatement();
} catch (SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
try {
DatabaseMetaData dbmd = con1.getMetaData();
try (ResultSet tables = dbmd.getTables(null, "public", "%", new String[] { "TABLE" })) {
while (tables.next()) {
// Create Table anfangen
String myCreateTable = "CREATE TABLE IF NOT EXISTS ";
String tName = tables.getString("TABLE_NAME");
ResultSet columns = dbmd.getColumns(null, null, tName, null);
// Create Table fortführen
myCreateTable += tName + "(";
int y=0;
int anzahl Spalten = ?????????????????????????????????????????
while (columns.next()) {
// Spalten
String colName = columns.getString("COLUMN_NAME");
String colType = columns.getString("TYPE_NAME");
String colSize = columns.getString("COLUMN_SIZE"); // String, weil manche Längen Strings sind
// convert to oracle
if ("serial".equals(colType)) { colType = "NUMBER"; }
if ("int4".equals(colType)) { colType = "NUMBER"; }
if ("text".equals(colType)) { colType = "CLOB"; }
if ("varchar".equals(colType)) { colType = "VARCHAR2"; }
// Create Table fortführen
myCreateTable += colName+" "+colType+"("+colSize+") ";
// Create Table fortführen
//Wenn letztes Element des Resultsets erreicht ist, dann Klammer zu, ansonsten ein Komma
if (y < anzahlSpalten) {
myCreateTable += ",";
} else {
myCreateTable += ")";
}
}
// Schema - Migration
System.out.println(myCreateTable);
}
}
} catch (SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
==========================================================
Danke im Voraus
capri