项目中jdbc操作数据库 DButil类

刚学的struts  就试着做了个项目 ,这些是对数据库的操作,希望各位大虾指点

数据库连接 ConnDB.java

package com.bookstore.db;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class ConnDB {
	public Connection conn = null;
	private PreparedStatement pstm;
	public Statement stmt = null;
	private static String propFileName = "/com/connDB.properties";
	private static Properties prop = new Properties();
	private static String dbClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
	private static String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=STRUTS_BOOKSTORE";
	private static String dbUser = "sa";
	private static String dbPwd = "123456";

	public ConnDB() {
		try {
			InputStream in = getClass().getResourceAsStream(propFileName);
			prop.load(in);
			dbClassName = prop.getProperty("DB_CLASS_NAME");
			dbUrl = prop.getProperty("DB_URL", dbUrl);
			dbUser = prop.getProperty("DB_USER", dbUser);
			dbPwd = prop.getProperty("DB_PWD", dbPwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection() {
		try {
			Class.forName(dbClassName).newInstance();
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
		} catch (Exception ee) {
			ee.printStackTrace();
		}
		if (conn == null) {
			System.err
					.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
							+ dbClassName
							+ "\r\n链接位置:"
							+ dbUrl
							+ "\r\n用户/密码"
							+ dbUser + "/" + dbPwd);
		}
		return conn;
	}

	/*
	 * 执行sql 语句
	 */
	public void doPstm(String sql, Object[] params) {
		if (sql != null && !sql.equals("")) {
			if (params == null) {
				params = new Object[0];
			}
			getConnection();
			if (conn != null) {
				try {
					pstm = conn.prepareStatement(sql,
							ResultSet.TYPE_SCROLL_INSENSITIVE,
							ResultSet.CONCUR_READ_ONLY);
					for (int i = 0; i < params.length; i++) {
						pstm.setObject(i + 1, params[i]);
					}
					pstm.execute();

				} catch (Exception e) {
					System.out.println("doPstm()方法出错!");
					e.printStackTrace();
				}
			}
		}

	}

	/*
	 * 关闭数据库连接
	 */
	public void close() {
		try {
			if (pstm != null) {
				pstm.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}

	/**
	 * @return 返回执行sql后的结果集
	 */
	public ResultSet getRs() throws SQLException {
		return pstm.getResultSet();
	}

	/**
	 * @return 返回执行sql后影响记录条数
	 */
	public int getCount() throws SQLException {
		return pstm.getUpdateCount();
	}

}

IBaseDao.java

package com.basedao;

import java.sql.ResultSet;
import java.util.List;

import com.bookstore.util.pager.Pager;

public interface IBasedao {
	public Pager getPager(String currentPage, String pageMethod, String table )
			throws Exception;

	// 添加 删除 修改
	public int saveOrDeleteOrUpdate(String sql, Object[] params)
			throws Exception;

	// 查询
	public List find(String sql, Object[] params) throws Exception;

	// 获取resultSet
	public List getListByRs(ResultSet rs) throws Exception;
}

BaseDao.java实现部分

package com.basedao;

import java.sql.ResultSet;
import java.util.List;

import com.bookstore.db.ConnDB;
import com.bookstore.util.pager.PageDao;
import com.bookstore.util.pager.Pager;

public class BaseDao implements IBasedao {
	
	protected ConnDB db;
	public BaseDao(){
		db= new ConnDB();
	}

	@Override
	public int saveOrDeleteOrUpdate(String sql, Object[] params)
			throws Exception {
		db.doPstm(sql, params);
		int count = db.getCount();
		return count;
	}

	@Override
	public List find(String sql, Object[] params) throws Exception {
		db.doPstm(sql, params);
		List list = this.getListByRs(db.getRs());
		return list;
	}

	@Override
	public Pager getPager(String currentPage, String pageMethod, String table)
			throws Exception {
		Pager page = new Pager();
		PageDao pagedao = new PageDao();
		page = pagedao.getPager(table, currentPage, pageMethod);
		String sql = page.getSQL(table);
		System.out.println("分页SQL语句----"+sql);
		db.doPstm(sql, null);
		List list = this.getListByRs(db.getRs());
		page.setElements(list);
		return page;
	}

	@Override
	public List getListByRs(ResultSet rs) throws Exception {
		return null;
	}

}

通过重写 public List getListByRs(ResultSet rs) 函数可以实现不同表字段的查询

例如:

 

package com.bookstore.vo.admin;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.basedao.BaseDao;
import com.bookstore.db.ConnDB;
import com.bookstore.util.pager.PageDao;
import com.bookstore.util.pager.Pager;

public class AdminDao extends BaseDao implements IAdminDao {

	@Override
	public List getListByRs(ResultSet rs) throws Exception {
		List<Admin> list = new ArrayList<Admin>();
		try {
			while (rs.next()) {
				Admin admin = new Admin();
				admin.setId(rs.getInt("ID"));
				admin.setUsername(rs.getString("username"));
				admin.setPswd(rs.getString("pswd"));
				admin.setTruename(rs.getString("truename"));
				admin.setTelephone(rs.getString("telephone"));
				admin.setIntroduce(rs.getString("introduce"));
				admin.setRegTime(rs.getDate("regtime"));
				admin.setLoginip(rs.getString("loginip"));
				list.add(admin);
			}
			rs.close();
		} catch (Exception e) {
			System.err.println("获取AdminList数据库数据列表失败!");
			e.printStackTrace();
		} finally {
			db.close();
		}
		return list;
	};

	
}

你可能感兴趣的:(jdbc,struts2,DButil实用类)