编写步骤
1.util包中就是连接数据库sqlserver
2.entity包中主要写的是属性
(属性必须和数据的一致)
dao/biz包中 分别里面都有一个接口类和class类
然后我们就要到SorlDao中开始写方法
模糊查询
select *from 表名 where XXX like ‘%"+类名.getXXX+"%’
@Override
public List<Solr> getAll(Solr s) {
// TODO Auto-generated method stub
List<Solr> sols=new ArrayList<Solr>();
try {
con=DBHelper.getCon();
String sql="select *from tb_solr where sjob like '%"+s.getSjob()+"%'";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
s=new Solr();
s.setSid(rs.getInt(1));
s.setSjob(rs.getString(2));
s.setScompany(rs.getString(3));
s.setSaddress(rs.getString(4));
s.setSalary(rs.getString(5));
s.setSurl(rs.getString(6));
s.setSlimit(rs.getString(7));
s.setStime(rs.getString(8));
s.setSdesc(rs.getString(9));
s.setSjobHandle(rs.getString(10));
s.setSaddressHandle(rs.getString(11));
sols.add(s);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return sols;
}
根据id查询
“select *from 表 where sid=”+s.getSid()
@Override
public Solr getAllId(Solr s) {
try {
con=DBHelper.getCon();
String sql="select *from tb_solr where sid="+s.getSid();
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
s.setSid(rs.getInt(1));
s.setSjob(rs.getString(2));
s.setScompany(rs.getString(3));
s.setSaddress(rs.getString(4));
s.setSalary(rs.getString(5));
s.setSurl(rs.getString(6));
s.setSlimit(rs.getString(7));
s.setStime(rs.getString(8));
s.setSdesc(rs.getString(9));
s.setSjobHandle(rs.getString(10));
s.setSaddressHandle(rs.getString(11));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return s;
}
增加数据
insert into 表 (XX,XX,XX,XX) values(?,?,?,?)
@Override
public int AddSolr(Solr s) {
// TODO Auto-generated method stub
int n=0;
try {
con=DBHelper.getCon();
String sql="insert into tb_solr(sjob,scompany,saddress,salary,surl,slimit,stime,sdesc,sjobHandle,saddressHandle) values(?,?,?,?,?,?,?,?,?,?)";
ps=con.prepareStatement(sql);
ps.setString(1, s.getSjob());
ps.setString(2, s.getScompany());
ps.setString(3, s.getSaddress());
ps.setString(4, s.getSalary());
ps.setString(5, s.getSurl());
ps.setString(6, s.getSlimit());
ps.setString(7, s.getStime());
ps.setString(8, s.getSdesc());
ps.setString(9, s.getSjobHandle());
ps.setString(10, s.getSaddressHandle());
n=ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
删除数据
delete from 表名 where sid=?
@Override
public int DelSolr(Solr s) {
int n=0;
try {
con=DBHelper.getCon();
String sql="delete from tb_solr where sid=?";
ps=con.prepareStatement(sql);
ps.setInt(1, s.getSid());
n=ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
根据id进行查询修改数据
update tb_solr set XX=?,XX=?,XX=?,XX=? where sid=XX"
@Override
public int EditSolr(Solr s) {
int n=0;
try {
con=DBHelper.getCon();
String sql="update tb_solr set sjob=?,scompany=?,saddress=?,salary=?,surl=?,slimit=?,stime=?,sdesc=?,sjobHandle=?,saddressHandle=? where sid=?";
ps=con.prepareStatement(sql);
ps.setString(1, s.getSjob());
ps.setString(2, s.getScompany());
ps.setString(3, s.getSaddress());
ps.setString(4, s.getSalary());
ps.setString(5, s.getSurl());
ps.setString(6, s.getSlimit());
ps.setString(7, s.getStime());
ps.setString(8, s.getSdesc());
ps.setString(9, s.getSjobHandle());
ps.setString(10, s.getSaddressHandle());
ps.setInt(11, s.getSid());
n=ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
其实servlet中的代码都差不多,只要稍微根据条件修改一下就好了
例如增加和修改的servlet:
增加:
//设置字符编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=UTF-8");
//获取out
PrintWriter out=resp.getWriter();
//获取session
HttpSession session=req.getSession();
//获取值
//String sid=req.getParameter("sid");
String sjob=req.getParameter("sjob");
String scompany=req.getParameter("scompany");
String saddress=req.getParameter("saddress");
String salary=req.getParameter("salary");
String surl=req.getParameter("surl");
String stime=req.getParameter("stime");
//调用biz
ISolrBiz isd=new SolrBiz();
Solr s=new Solr();
//s.setSid(Integer.parseInt(sid));
s.setSjob(sjob);
s.setScompany(scompany);
s.setSaddress(saddress);
s.setSalary(salary);
s.setSurl(surl);
s.setStime(stime);
int n=isd.AddSolr(s);
//跳转界面
if(n>0) {
req.getRequestDispatcher("solr.do").forward(req, resp);
}
else {
out.print("");
}
}
删除:
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=UTF-8");
//获取out
PrintWriter out=resp.getWriter();
//获取session
HttpSession session=req.getSession();
//获取sid
String sid=req.getParameter("sid");
//调用biz
ISolrBiz isb=new SolrBiz();
Solr s=new Solr();
s.setSid(Integer.parseInt(sid));
int n=isb.DelSolr(s);
//跳转界面
if(n>0) {
req.getRequestDispatcher("solr.do").forward(req, resp);
}
else {
out.print("");
}
}
注:写servlet类是一定要继承HttpServlet
继承HttpServlet后,要使用两个方法doGet和doPost方法
写完servlet类之后,需要在xml中配置
但是有一个更加简化的代码,是以下次行代码,以上的配置代码和下一代码的作用一毛一样
@WebServlet(name="demo", urlPatterns="/demo")