All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.drinkjava2.jdialects.DialectJdbcUtils Maven / Gradle / Ivy

Go to download

jDialects is a pagination and DDL tool support ~80 databases, run on JDK8 or above

There is a newer version: 5.0.13.jre8
Show newest version
/*
* jDialects, a tiny SQL dialect tool 
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package com.github.drinkjava2.jdialects;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Some simple JDBC method
 * 
 * @author Yong Zhu
 * @since 1.0.0
 */
public abstract class DialectJdbcUtils {

	public static Long hotQueryForLong(Connection conn, String sql) throws SQLException {
		PreparedStatement pst = null;
		SQLException exception = null;
		try {
			pst = conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			rs.next();
			return rs.getLong(1);
		} catch (SQLException e) {
			exception = e;
			return null;
		} finally {
			closeRSandPST(null, pst, exception, sql);
		}
	}

	public static void hotExecuteSql(Connection conn, String sql) throws SQLException {
		PreparedStatement pst = null;
		SQLException exception = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.execute();
		} catch (SQLException e) {
			exception = e;
		} finally {
			closeRSandPST(null, pst, exception, sql);
		}
	}

	public static void closeRSandPST(ResultSet rs, PreparedStatement pst, SQLException exception, String sql,
			Object... params) throws SQLException {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				if (exception != null)
					e.setNextException(exception);
				exception = e;
			} finally {
				rs = null;
			}
		}
		if (pst != null) {
			try {
				pst.close();
			} catch (SQLException e) {
				if (exception != null)
					e.setNextException(exception);
				exception = e;
			} finally {
				pst = null;
			}
		}
		if (exception != null)
			throw exception;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy