首页>代码>java使用JDBC连接MySQL数据库操作电子图书增删改查项目实例>/jdbc连接数据库-源码/ebookentry-JDBC/src/dao/BaseDao.java
package dao;

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

public class BaseDao {

	/***
	 * 
	 * @author 数据库连接类
	 *
	 */
		private String driver ="com.mysql.jdbc.Driver";
		private String url="jdbc:mysql://localhost:3306/ebookentry?characterEncoding=utf-8";
		private String name="root";
		private String pwd="123456789";
	      Connection conn=null;
	     
	      /***
	       * 
	       * @return 打开连接
	       */
	    /*  public Connection getconn(){
	  		Connection conn=null;
	  		Context ctx;
	  		try {
	  			ctx = new InitialContext();
	  			DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/news");	
	  	    	conn=ds.getConnection();
	  		}catch (Exception e) {
	  			e.printStackTrace();
	  		}
	  		return conn;
	  	}     */
		protected  Connection getconn(){
			conn=null;	
			try {
				Class.forName(driver);
				conn=DriverManager.getConnection(url,name,pwd);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			return conn;
		}
		
		/****
		 * 
		 * @param 关闭数据库连接
		 */
		protected void closeAll(Connection conn ,PreparedStatement ps,ResultSet rs){		
			if(rs!=null)
				try {
					if(rs!=null)
					rs.close();
					if(ps!=null)
					ps.close();
					if(conn!=null)
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
		}
	
		/***
		 * 
		 * @param 增删改方法
		 * @param 接受 参数为 SQL语句 和 对象数组
		 * @return 返回受影响行数
		 */
		public int executeUpdate(String sql ,Object []ob){
			conn=getconn();
			PreparedStatement ps=null;
			try {
				ps=prepareStatement(conn,sql,ob);
				int i=ps.executeUpdate();
				return i;	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
			    //	e.printStackTrace();
				return 0;
			}finally{			
				closeAll(conn, ps, null);
			}
		
		}	
	
		/***
		 * 查询方法
		 */
		protected PreparedStatement prepareStatement(Connection conn,String sql,Object []ob){		
			PreparedStatement ps=null;
					try {
						int index=1;
						ps = conn.prepareStatement(sql);
							if(ps!=null&&ob!=null){
								for (int i = 0; i < ob.length; i++) {			
										ps.setObject(index, ob[i]);	
										index++; 
								}
							}
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
			 return ps;
		}
	
}
最近下载更多
y1214435276  LV9 2024年4月11日
微信网友_6906962132258816  LV7 2024年3月16日
微信网友_6829521472425984  LV5 2024年1月20日
asddwh  LV13 2023年12月26日
321170193  LV6 2023年11月12日
jiemomo  LV12 2023年10月19日
liuliuyl  LV3 2023年6月17日
哇呜呜  LV1 2023年6月13日
微信网友_6260196253601792  LV7 2022年12月16日
wwb521  LV7 2022年11月28日
最近浏览更多
赵鑫cdsaljkdfsa  LV11 2024年7月2日
liyan54188  LV2 2024年6月25日
暂无贡献等级
Dominick  LV14 2024年5月8日
一起加油  LV5 2024年4月19日
y1214435276  LV9 2024年4月11日
微信网友_6906962132258816  LV7 2024年3月16日
微信网友_6829521472425984  LV5 2024年1月20日
Kaiaahh  LV2 2024年1月4日
放鞭炮  LV1 2023年12月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友