当前位置:首页 > java使用jdbc连接数据库的几种方式
编程技术
package db.util;
import java.sql.CallableStatement; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.HashMap; import java.util.List; import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DbUtil {
public static final String _DefaultJNDI = \
private static Log log = LogFactory.getLog(DbUtil.class); /** *
* Utility function to clean up db usage. *
** @param pResultSet * @param pStmt * @param pConn *
* @exception java.sql.SQLException */
public static void cleanup(ResultSet pResultSet, Statement pStmt, Connection pConn) throws SQLException { try {
if (pResultSet != null) { pResultSet.close(); pResultSet = null; }
// log.info(\ } catch (SQLException e) {
throw e; } finally { try {
if (pStmt != null) { pStmt.close(); pStmt = null; }
} catch (SQLException e) { throw e; } finally { try {
if (pConn != null && !pConn.isClosed()) { pConn.close(); pConn = null; }
} catch (SQLException e) { throw e; } } } } /** * ??????????? *
* @param sql
* String * @return String */
public static String Update(String sql) { return Update(sql, _DefaultJNDI); } /** *
* Utility function to Execute SQL Language. *
** @param sql
* String * @return String */
public static String Update(String strSql, String _JNDI) { Connection conn = null;
Statement stmt = null; String strRtn = \ try {
conn = ConnectionPool.getInstance().getConnection(_JNDI); stmt = conn.createStatement(); stmt.executeUpdate(strSql); strRtn = \ } catch (Exception e) { e.printStackTrace();
System.out.println(\ strRtn = e.getMessage(); } finally { try {
DbUtil.cleanup(null, stmt, conn); } catch (Exception e) { } }
return strRtn; } /** *
* ????MAX ID,??1,????μ?ID
*
** @param fieldname * ID????? * @param tablename * ???? * @return Integer ?μ?ID */
public static long getId(String fieldname, String tablename) { return getId(fieldname, tablename, _DefaultJNDI); }
public static long getId(String fieldname, String tablename, String _JNDI) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = \ long id = 0;
sql = \
try {
conn = ConnectionPool.getInstance().getConnection(_JNDI); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); rs.next();
id = rs.getLong(1); id++;
} catch (Exception e) {
System.out.println(\ log.info(e.getMessage()); } finally { try {
cleanup(null, stmt, conn); } catch (Exception e2) { } }
return id; }
public static String Update(String strSql[]) { return Update(strSql, _DefaultJNDI); } /**
* Execute the number of SQL Language that in SQL Array *
* @param strSql
* String[] SQL ???????????
* @return String Exectute Message 'success' or Error Message */
public static String Update(String strSql[], String _JNDI) { Connection conn = null; Statement stmt = null; String strRtn = \ String sql = null;
try {
conn = ConnectionPool.getInstance().getConnection(_JNDI); conn.setAutoCommit(false); stmt = conn.createStatement();
for (int i = 0; i < strSql.length; i++) {
if ((strSql[i] != null) && (strSql[i].length() > 0)) {
共分享92篇相关文档