001    package railo.commons.db;
002    
003    import java.sql.Connection;
004    import java.sql.DriverManager;
005    import java.sql.ResultSet;
006    import java.sql.SQLException;
007    import java.sql.Statement;
008    
009    /**
010     * Utility for db
011     */
012    public final class DBUtil {
013    
014            // TODO impl. this class, not used at the moment
015            /**
016             * returns label matching className
017             * @param className
018             * @return label
019             */
020            public static String getLabelForDriverClass(String className) {
021                    if("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(className))
022                            return "MSSQL DataBase";
023                    // TODO connect WS from railo. ch to get more
024                    return className;
025            }
026    
027            public static void setAutoCommitEL(Connection conn, boolean b) {
028                    /*try {
029                            if(conn!=null){
030                                    if(conn.getAutoCommit()==b) return;
031                            }
032                    } 
033                    catch (Throwable e) {}*/
034                    
035                    
036                    try {
037                            
038                            if(conn!=null)conn.setAutoCommit(b);
039            } 
040            catch (Throwable e) {}
041            }
042    
043            public static void setReadOnlyEL(Connection conn, boolean b) {
044                    try {
045                            if(conn!=null)conn.setReadOnly(b);
046                    } 
047                    catch (Throwable e) {}
048            }
049    
050            public static void commitEL(Connection conn) {
051                    try {
052                            if(conn!=null)conn.commit();
053                    } 
054                    catch (Throwable e) {}
055            }
056    
057            public static void setTransactionIsolationEL(Connection conn,int level) {
058                    try {
059                            if(conn!=null)conn.setTransactionIsolation(level);
060                    } 
061                    catch (Throwable e) {}
062            }
063    
064            public static void closeEL(Statement stat) {
065                    if(stat!=null) {
066                try {
067                    stat.close();
068                } catch (Throwable t) {}
069            }
070            }
071    
072            public static void closeEL(ResultSet rs) {
073                    if(rs!=null) {
074                try {
075                    rs.close();
076                } catch (Throwable t) {}
077            }
078            }
079    
080            public static Connection getConnection(String dsn, String user, String pass) throws SQLException {
081                    try {
082                    return DriverManager.getConnection(dsn, user, pass);
083            } 
084            catch (SQLException e) {
085                    if(dsn.indexOf('?')!=-1) {
086                    String connStr=dsn+"&user="+user+"&password="+pass;
087                    return DriverManager.getConnection(connStr);
088                }
089                    else throw e;
090            }
091            }
092    
093    }