controllerimpl.java

来自「JSP开发的完整的网络商店.包含源代码和开发文档等」· Java 代码 · 共 109 行

JAVA
109
字号
/*
 * Created on 2005-11-11
 * Author 曹汕
 * Version 1.0
 * Copyright by CS.SSPKU Inc. All rights reserved. 
 */
package com.struts.controller.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.struts.utils.DBConnectionManager;

/**
 * @author cs
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract class ControllerImpl {
	private Log log=LogFactory.getLog(this.getClass().getName());
	
	
	//获得数据库中sequence的值,作为所插入表的ID
	protected int getId(String seq){
		String sql="select "+seq+".nextval from dual";
		ResultSet rs=selectRecord(sql);
		try {
			if(rs.next())return rs.getInt("nextval");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.info("SEQ字段查询出错,请联系管理员"+e.getMessage());
		}
		return 0;
		}
	/**
	 * 数据库记录插入
	 * @param sql
	 * @return true or false
	 */
	protected boolean insertRecord(String sql){
		DBConnectionManager dbconnection=DBConnectionManager.getInstance();
		Connection conn=dbconnection.getConnection();
		System.out.println("SQL <<<< " + sql);
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
			return true;
		} catch (SQLException e) {
			System.out.print("Insert:" + e.getMessage());
			return false;
		}finally{
			dbconnection.freeConnection(conn);
		}
	}
	/**
	 * 数据库记录更新
	 * @param sql
	 * @return true or false
	 */
	protected boolean updateRecord(String sql){
		DBConnectionManager dbconnection=DBConnectionManager.getInstance();
		Connection conn=dbconnection.getConnection();
		System.out.println("SQL <<<< " + sql);
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
			return true;
		} catch (SQLException e) {
			System.out.print("Update:" + e.getMessage());
			return false;
		}finally{
			dbconnection.freeConnection(conn);
		}
	}
	
	/**
	 * 数据库记录查询
	 * @param sql
	 * @return ResultSet
	 */
	public ResultSet selectRecord(String sql){
		DBConnectionManager dbconnection=DBConnectionManager.getInstance();
		Connection conn=dbconnection.getConnection();
		System.out.println("SQL <<<< " + sql);
		ResultSet rs = null;
		try {
			Statement stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			System.out.print("Query:" + e.getMessage());
		}finally{
			dbconnection.freeConnection(conn);
		}
		return rs;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?