首页Java基础Java源码Java框架设计模式JavaEEJava Web开发视频教程

您现在的位置:首页 >

Java源码

关注我们

优就业官方微信
优就业官方微信扫描二维码,即可与小
U亲密互动
优就业官方微博
优就业官方微博扫码关注,更有精彩活动
等你参与
  • 优就业官方QQ号

    优就业官方QQ号

    一键加好友
  • 咨询电话:( 9:00-22:00 )

    400-650-7353

Java完成数据增删改查的源代码

2017-12-13 15:08:39

// 得到所有的记录

public ArrayList getUserInfo()

{

Connection conn = null;

Statement state = null;

ResultSet rs = null;

UserPO upo = null;

ArrayList al = new ArrayList();

try {

conn = DBTools.getConnection();

state = conn.createStatement();

String sql = "select * from notebook_user";

rs = state.executeQuery(sql);

//从resultset对象中将数据取出

while(rs.next())

{

upo = new UserPO();

int id = rs.getInt("id");

String yhm = rs.getString

("yhm");

String email = rs.getString("email");

String content = rs.getString("content");

upo.setId(id);

upo.setYhm(yhm);

upo.setEmail(email);

upo.setContent(content);

//将修改对象放入已经创建好的集合类对象ArrauyList

al.add(upo);

}

} catch (Exception ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}

finally

{

DBTools.closeRs(rs);

DBTools.closeState(state);

DBTools.closeConn(conn);

}

return al;

}

// 删除一条user记录

public void deleteUserInfo(int id)

{

Connection conn = null;

Statement state = null;

try {

conn = DBTools.getConnection();

state = conn.createStatement();

String sql = "delete from notebook_user where id="+id;

//System.out.println(sql);

state.executeUpdate(sql);

} catch (Exception ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}

finally

{

DBTools.closeState(state);

DBTools.closeConn(conn);

}

}

// 根据给定的信息得到记录

public ArrayList getUserInfoByInfo(String name,String email,String content)

{

Connection conn = null;

Statement state = null;

ResultSet rs = null;

UserPO upo = null;

ArrayList al = new ArrayList();

try {

conn = DBTools.getConnection();

state = conn.createStatement();

String sql = "select * from notebook_user where 1=1 ";

if(!"".equals(name) && name != null)

{

sql += " and yhm like '%"+name+"%'";

}

if(!"".equals(email) && email != null)

{

sql += " and email = '"+email+"'";

}

if(!"".equals(content) && content != null)

{

sql += " and content like '%"+content+"%'";

}

sql+=" order by id desc";

rs = state.executeQuery(sql);

//从resultset对象中将数据取出

while(rs.next())

{

upo = new UserPO();

int id = rs.getInt("id");

String yhm = rs.getString("yhm");

String femail = rs.getString("email");

String fcontent = rs.getString("content");

upo.setId(id);

upo.setYhm(yhm);

upo.setEmail(femail);

upo.setContent(fcontent);

//将修改对象放入已经创建好的集合类对象ArrauyList

al.add(upo);

}

} catch (Exception ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}

finally

{

DBTools.closeRs(rs);

DBTools.closeState(state);

DBTools.closeConn(conn);

}

return al;

}

}

免责声明:本文来源于网络,由网友提供或网络搜集,仅供个人交流学习参考使用,不涉及商业盈利目的。如有版权问题,请联系本站管理员予以更改或删除。谢谢合作!

猜你喜欢

 
 
咨询中心
优就业微信扫一扫
微信扫一扫

400-650-7353

加入官方微博