create Tables via DataSource - Java java.sql

Java examples for java.sql:DataSource

Description

create Tables via DataSource

Demo Code

/*/*from   w  w w.ja  v a  2 s . com*/
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015, 2016 
 */
//package com.java2s;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

public class Main {
    private static void createTables(DataSource ds) throws Exception {
        try (Connection cn = ds.getConnection()) {
            Statement stmt = cn.createStatement();
            stmt.execute("CREATE TABLE persons " + "("
                    + "id INTEGER NOT NULL,"
                    + "firstname VARCHAR(40) NOT NULL,"
                    + "lastname VARCHAR(40) NOT NULL," + "PRIMARY KEY (id)"
                    + ")");
        } catch (SQLException e) {
            if (e.getLocalizedMessage().contains("already exists"))
                return;
            else
                throw e;
        }
    }
}

Related Tutorials