JDNI的学习

在tomcat根目录下conf目录下Context.xml  Context节点里面添加Resource 节点

<!--配置MySQL数据库的JNDI数据源-->
40 <Resource 
41         name="jdbc/mysql"
42         auth="Container" 
43         type="javax.sql.DataSource"
44         maxActive="100" 
45         maxIdle="30" 
46         maxWait="10000"
47         username="root" 
48         password="root"
49         driverClassName="com.mysql.jdbc.Driver"
50         url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&amp;characterEncoding=utf-8"/>

配置好后创建dao中代码入下,添加 增删改方法,和通用的查方法,

public class DatabaseDao {
	static DataSource ds = null;
	static {
		try {
			Context context = new InitialContext();
			ds = (DataSource) context.lookup("java:comp/env/jdbc/jamison");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Connection GET() throws Exception {

		return ds.getConnection();
	}

	public static Result getSqlse(String sql, String... pent) {
		Connection conn = null;
		Result rt = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = GET();
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < pent.length; i++) {
				ps.setObject((i + 1), pent[i]);
			}
			rs = ps.executeQuery();
			rt = ResultSupport.toResult(rs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Setclos(conn, ps, rs);
		}

		return rt;
	}
	public  static int IUD(String sql, String... pent){  
		Connection conn = null;
		int count=0;
		PreparedStatement ps = null;
		try {
			conn = GET();
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < pent.length; i++) {
				ps.setObject((i + 1), pent[i]);
			}
			count=ps.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//Setclos(conn, ps, null);
		}
		return count;
	}
	public static void Setclos(Connection conn, PreparedStatement ps, ResultSet rs) {
		if (rs == null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (ps == null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (conn == null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

2.使用DatabaseDao类使用的方法;

public class NewsDao {
	List<News> list=new ArrayList<News>();
	public List<News> GetNews(String sql,String...pent){

		
		Result rs=DatabaseDao.getSqlse(sql, pent);
		Map[] map=rs.getRows();
			for (Map map2 : map) {
				News news=new News();
				news.setUid((Integer) map2.get(("uid")));
				news.setUname(map2.get("uname").toString());
				news.setType(map2.get("type").toString());
				
				news.setTid((Integer)map2.get("nid"));
				news.setTitle(map2.get("title").toString());
				news.setCountnews(map2.get("counten").toString());
				news.setItme(map2.get("time").toString());
				
				news.setZid((Integer)map2.get("zid"));
				news.setZname(map2.get("zname").toString());
				list.add(news);
			}
				
			
		
		return list;
	}
	public List<News> GetNewsupdate(String sql,String...pent){
		Result rt=DatabaseDao.getSqlse(sql, pent);
				News news=new News();
				Map[] map=rt.getRows();
				for (Map map2 : map) {
					news.setTid((Integer)map2.get("nid"));
					news.setTitle(map2.get("title").toString());
					news.setCountnews(map2.get("counten").toString());
					news.setItme(map2.get("time").toString());
					list.add(news);
				}
	
		return list;
	}	
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值