学习 JavaWeb项目开发案例精粹14(新闻发布系统)之九

这里就介绍SqlDAO接口和SqlOperator接口的实现。

SQLDAO接口源码,写了增删改查和关闭以及返回一个连接。


package com.ppcms.common;

import java.sql.Connection;
import java.sql.ResultSet;

public abstract class SqlDAO {
	public SqlDAO() {
    }
	
	public abstract void Close();

    public abstract void prepareStatement(String s);

    public abstract void executeUpdate();

    public abstract ResultSet executeQuery();

    public abstract void executeUpdate(String s);

    public abstract ResultSet executeQuery(String s);

    public abstract Connection getConnection();

}


SqlOperator.java

package com.ppcms.common;

import java.sql.*;

import com.ppcms.common.Log;


public class SqlOperator extends SqlDAO {
	public PreparedStatement prepstmt;
    public Connection conn;
    public DataBaseConnection dbc;
    
	public SqlOperator() {
		prepstmt = null;
		dbc = new DataBaseConnection();
		conn = dbc.getConnection();
	}

	@Override
	public void Close() {
		// TODO Auto-generated method stub
		dbc.close();
	}

	@Override
	public void prepareStatement(String strSql) {
		// TODO Auto-generated method stub
		try {
            prepstmt = conn.prepareStatement(strSql);
        }catch(Exception e) {
            e.printStackTrace(System.out); Debug.writeLog("In prepareStatement(String strSql), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
	}

	@Override
	public void executeUpdate() {
		// TODO Auto-generated method stub
		try
        {
            if(prepstmt != null)
                prepstmt.executeUpdate();
        }
        catch(Exception e)
        {
            e.printStackTrace(System.out);
            Log.writeLog("In executeUpdate(String), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
	}

	@Override
	public ResultSet executeQuery() {
		// TODO Auto-generated method stub
		// return null;
		ResultSet rs = null;
        try
        {
            if(prepstmt != null)
                rs = prepstmt.executeQuery();
        }
        catch(Exception e)
        {
            e.printStackTrace(System.out);
            Log.writeLog("In executeQuery(String), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
        return rs;
	}

	@Override
	public void executeUpdate(String strSql) {
		// TODO Auto-generated method stub
		try {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(strSql);
        } catch(Exception e) {        	
            e.printStackTrace(System.out);
            Log.writeLog("In executeUpdate(String), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
	}

	@Override
	public ResultSet executeQuery(String strSql) {
		// TODO Auto-generated method stub
		// return null;
		ResultSet rs = null;
        try
        {
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(strSql);
        }
        catch(Exception e)
        {
            e.printStackTrace(System.out);
            Log.writeLog("In executeQuery(String), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
        return rs;
	}

	@Override
	public Connection getConnection() {
		// TODO Auto-generated method stub
		return dbc.getConnection();
	}
	
	
	
	public void setString(int index, String value)
    {
        try
        {
            prepstmt.setString(index, value);
        }
        catch(Exception e)
        {
            e.printStackTrace(System.out);
            Log.writeLog("In setString(int index,String value), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
    }

    public void setInt(int index, int value)
    {
        try
        {
            prepstmt.setInt(index, value);
        }
        catch(Exception e)
        {
            e.printStackTrace(System.out);
            Log.writeLog("In setInt(int index,int value), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
    }

    public void clearParameters()
    {
        try
        {
            prepstmt.clearParameters();
            prepstmt = null;
        }
        catch(Exception e)
        {
            e.printStackTrace(System.out);
            Log.writeLog("In clearParameters(), Exception Occured ! Info :" + e.getLocalizedMessage());
        }
    }

    public PreparedStatement getPreparedStatement()
    {
        return prepstmt;
    }

}
增删改查的实现,另外还把prepstmt.setInt和prepstmtsetString给重新封装一下。这里全是JDBC的内容,也都很简单,可以先了解一下。


你可能感兴趣的:(学习 JavaWeb项目开发案例精粹14(新闻发布系统)之九)