Java SQL Table createTable(Connection dbCon, String tableName, String fields)

Here you can find the source of createTable(Connection dbCon, String tableName, String fields)

Description

create Table

License

Open Source License

Declaration


public static void createTable(Connection dbCon, String tableName, String fields) throws Exception 

Method Source Code


//package com.java2s;
//  it under the terms of the GNU General Public License as published by

import java.sql.Connection;
import java.sql.DatabaseMetaData;

import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

    public static void createTable(Connection dbCon, String tableName, String fields) throws Exception {
        if (!testIfTableExist(dbCon, tableName)) {
            Statement aStmt = dbCon.createStatement();
            String sql = "CREATE TABLE " + tableName + " " + fields;
            aStmt.execute(sql);/*from   w w  w  . ja v a 2  s .  c  om*/
        }
    }

    public static boolean testIfTableExist(Connection dbCon, String tableName) throws Exception {
        DatabaseMetaData meta = dbCon.getMetaData();
        String[] types = { "TABLE" };
        ResultSet aRs = meta.getTables(null, null, tableName, types);
        if (aRs.next())
            return true;
        return false;
    }
}

Related

  1. countTables(Connection connection)
  2. createBagValuesTables(Connection con)
  3. createFreqTable()
  4. createHSQLTables(LineNumberReader reader, Statement statement)
  5. createSourceTable(Connection con)
  6. createTable(Statement statement, String spaceName, String minSeeders, String capacity, String replicationCount)
  7. createTable(String createTblSql, Connection conn)
  8. createTableAndInsertData(Connection c)
  9. dropTable(String tableName, Connection con)