⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbutils.java

📁 宠物诊所的JAVA-WEB实现
💻 JAVA
字号:
package com.hellofdeath.tool;

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

import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.hellofdeath.owner.PetownerDao;
import com.hellofdeath.pet.PetDao;

/*
 * 数据库工具类
 */
public class DBUtils {
	public DBUtils() {
	}

	public Connection getConnection() { // 建立连接池的连接
		try {
			Context initCtx = new javax.naming.InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			ds = (DataSource) envCtx.lookup("jdbc/PetHospital");
			conn = ds.getConnection();
		} catch (SQLException ex) {
			System.out.print("SQL异常,getConnection出现问题!正关闭连接!");
			System.out.println(ex.getMessage());
		} catch (NamingException e) {
			System.out.print("命名目录出现问题");
			System.out.println(e.getMessage());
		}
		return conn;
	}

	public ResultSet getResultSet(String sql) {// 数据库的查询操作
		this.getConnection();
		System.out.println("执行数据库操作:" + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/*
	 * public boolean insertEmployee(EmpDao empDao) { //
	 * 数据库的插入操作,所向插入职员的账号的密码,如果需要写注册的话
	 * success = true; 
	 * try { 
	 * 		pstmt = conn.prepareStatement("insert into emplyee
	 *		 values (?,?)"); 
	 *		 pstmt.setString(1, empDao.getEmp_name());
	 *			pstmt.setString(2, empDao.getEmp_password()); pstmt.execute(); 
	 *		}catch(SQLException e) 
	 *		{
	 *			 e.printStackTrace(); 
	 *			 success = false; 
	 *      } 
	 *      return  success; 
	 * }
	 */

	public boolean insertOwner(PetownerDao petOwner) {// 插入所有人的信息
		success = true;
		this.getConnection();
		try {
			pstmt = conn
					.prepareStatement("insert into owners(name,address,city,telephone)"
							+ " values(?,?,?,?)");
			pstmt.setString(1, petOwner.getPetOwnerName());
			pstmt.setString(2, petOwner.getPetOwnerAddress());
			pstmt.setString(3, petOwner.getPetOwnerCity());
			pstmt.setString(4, petOwner.getPetOwnerTelNo());
			pstmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			success = false;
		}
		return success;
	}

	public boolean insertPet(PetDao pet) {// 插入宠物的信息
		success = true;
		this.getConnection();
		try {
			pstmt = conn
					.prepareStatement("insert into pets(name,birth_date,type_id,owner_id)"
							+ " values(?,?,?,?)");
			pstmt.setString(1, pet.getPetName());
			pstmt.setString(2, pet.getPetBirthDate());
			pstmt.setInt(3, pet.getPetTypeId());
			pstmt.setInt(4, pet.getPetOwnerId());
			pstmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			success = false;
		}
		return success;
	}

	public boolean insert(String strSql) {// 插入的通用信息,我也不知道后来用没用
		success=true;
		this.getConnection();
		try {
			pstmt = conn.prepareStatement(strSql);
			pstmt.execute();
		} catch (SQLException e) {
			success=false;
			e.printStackTrace();
		}
		return success;
	}

	public boolean updateOwner(PetownerDao owner) {// 根据ID更新所有人的信息
		success = true;
		this.getConnection();
		String strSql = "update owners set name='" + owner.getPetOwnerName()
				+ "',address='" + owner.getPetOwnerAddress() + "',city='"
				+ owner.getPetOwnerCity() + "',telephone='"
				+ owner.getPetOwnerTelNo() + "' where id='"
				+ owner.getPetOwnerId() + "'";
		System.out.println(strSql);
		try {
			pstmt = conn.prepareStatement(strSql);
			pstmt.execute();
		} catch (SQLException e) {
			success = false;
			e.printStackTrace();
		}
		return success;
	}

	public boolean updatePet(PetDao pet) {// 根据ID更新宠物的信息
		this.getConnection();
		success = true;
		try {
			String strSql = "update pets set name='" + pet.getPetName()
					+ "',birth_date='" + pet.getPetBirthDate() + "',type_id="
					+ pet.getPetTypeId() + ",owner_id=" + pet.getPetOwnerId()
					+ " where id=" + pet.getPetId();
			System.out.println(strSql);
			pstmt = conn.prepareStatement(strSql);
			pstmt.execute();
			strSql = "update types set name='" + pet.getPetTypeName()
					+ "' where id=" + pet.getPetTypeId();
			pstmt = conn.prepareStatement(strSql);
			pstmt.execute();
			strSql = "update owners set name='" + pet.getPetOwnerName()
					+ "' where id=" + pet.getPetOwnerId();
			pstmt = conn.prepareStatement(strSql);
			pstmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			success = false;
		}
		return success;
	}

	public void close() {// 关闭资源
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private boolean success;

	private Connection conn = null;

	private DataSource ds = null;

	private ResultSet rs = null;

	private PreparedStatement pstmt = null;
}

⌨️ 快捷键说明

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