001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.jdbc.datasource;
017    
018    import java.sql.CallableStatement;
019    import java.sql.Connection;
020    
021    import javax.sql.DataSource;
022    
023    import org.kuali.common.jdbc.JdbcUtils;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    public class DriverManagerDataSourceTest {
028    
029            private static final Logger logger = LoggerFactory.getLogger(DriverManagerDataSourceTest.class);
030    
031            protected DataSource getMySQLDataSource(String url, String username, String password) {
032                    DriverManagerDataSource dmsd = new DriverManagerDataSource();
033                    dmsd.setDriverClassName("com.mysql.jdbc.Driver");
034                    dmsd.setUrl(url);
035                    dmsd.setUsername(username);
036                    dmsd.setPassword(password);
037                    return dmsd;
038            }
039    
040            protected DataSource getOracleDataSource(String url, String username, String password) {
041                    DriverManagerDataSource dmsd = new DriverManagerDataSource();
042                    dmsd.setDriverClassName("oracle.jdbc.driver.OracleDriver");
043                    dmsd.setUrl(url);
044                    dmsd.setUsername(username);
045                    dmsd.setPassword(password);
046                    return dmsd;
047            }
048    
049            // @Test
050            public void testOracle() {
051                    try {
052                            DataSource dataSource = getOracleDataSource("jdbc:oracle:thin:@oracle.ks.kuali.org:1521:ORACLE", "master", "gw570229");
053                            Connection conn = DataSourceUtils.doGetConnection(dataSource);
054                            logger.info(conn + "");
055                            // String sql = "{ call rdsadmin.rdsadmin_util.restricted_session(false) }";
056                            // KSENV4 jeffcaddel administrators-MacBook-Pro-2.local JDBC Thin Client JDBC Thin Client 2013-01-03 15:04:24.0 667 34453
057                            String sql = "{ call rdsadmin.rdsadmin_util.kill(667,34457) }";
058                            String nativeSql = conn.nativeSQL(sql);
059                            logger.info("native sql=" + nativeSql);
060                            CallableStatement cstmt = conn.prepareCall(sql);
061                            // cstmt.setString(1, "667");
062                            // cstmt.setString(2, "34455");
063                            cstmt.execute();
064                            conn.commit();
065                            cstmt.close();
066                            JdbcUtils.closeQuietly(dataSource, conn);
067                    } catch (Exception e) {
068                            e.printStackTrace();
069                    }
070    
071            }
072    
073            // @Test
074            public void test() {
075                    try {
076                            DataSource dataSource = getMySQLDataSource("jdbc:mysql://localhost", "root", null);
077                            Connection conn = DataSourceUtils.doGetConnection(dataSource);
078                            logger.info(conn + "");
079                            JdbcUtils.closeQuietly(dataSource, conn);
080                    } catch (Exception e) {
081                            e.printStackTrace();
082                    }
083            }
084    
085    }