在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&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;
}