create Data Source - Java java.sql

Java examples for java.sql:DataSource

Description

create Data Source

Demo Code


//package com.java2s;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Main {
    public static void main(String[] argv) throws Exception {
        createDataSource();/*from   ww w .  j a  v  a 2s  .c  o m*/
    }

    private static DataSource ds;

    public static void createDataSource() throws Exception {
        try {
            Context context = new InitialContext();
            if (context == null) {
                throw new Exception("create context failed!");
            }
            ds = (DataSource) context.lookup("java:comp/env/jdbc/movie");
            if (ds == null) {
                Thread.sleep(2000L);
                ds = (DataSource) context
                        .lookup("java:comp/env/jdbc/movie");
                if (ds == null)
                    throw new Exception("get datasource failed!");
            }
        } catch (NamingException ne) {
            throw ne;
        } catch (Exception e) {
            throw e;
        }
    }
}

Related Tutorials