简单页面+java后台+数据库,实现从页面对数据库的增删改查

来源:互联网 发布:虚拟视频软件电脑 编辑:程序博客网 时间:2024/06/13 05:11

在实现简单网页上对数据内容进行增删改查,需要用到三个部分,分别是jsp网页部分+java后台部分+数据库表

我用一个新闻的例子来实现,首先编写java后台程序


java后台程序:

我们用三层的模式进行设计:分别是servlet,service,dao层,并且建立个实体包用来打包数据库和后台要用到的属

性截个图


首先是写功能写的顺序分别是从servlet,service,dao层:

servlet层代码如下:

public class TypeServlet {TypeService ts=new TypeServiceImp();//调用service层/*******添加************************************************************************************/public int addtype(String name){int a=0;a=ts.addtype(name);return a;}/*******查看************************************************************************************/public List<types> selets(){List<types> list=new ArrayList<types>();list=ts.selets(null);return list;}/*******删除************************************************************************************/public int delete(int id){int a=0;types t=new types();t.setId(id);a=ts.delete(t);return a;}/*******修改************************************************************************************/public int update(types t){int a=0;a=ts.update(t);return a;}/*******查找一个************************************************************************************/public types selectone(int id){types t=new types();t.setId(id);types nt=ts.selectone(t);return  nt;}}

Service层分为两层分别为接口层和实现层



接口程序如下:

public interface TypeService {public int addtype(String name);public List<types> selets(types t);public int delete(types t);public int update(types t);public types selectone(types t);}

接口实现程序:

public class TypeServiceImp implements TypeService{TypeDao td= new TypeDaoImp();public int addtype(String name) {    //注意返回数据不要忘记修改int a=0;a=td.addtype(name);return a;}public List<types> selets(types t) {List<types> list=new ArrayList<types>();list=td.selets(t);return list;}/*******删除************************************************************************************/public int delete(types t) {int a=0;a=td.delete(t);return a;}/*******修改************************************************************************************/public int update(types t) {int a=0;a=td.update(t);return a;}/*******查找单个************************************************************************************/public types selectone(types t){types tp=new types();tp=td.selectone(t);return tp;}}

Dao层程序同样分为接口层和实现层

接口层程序:

public interface TypeDao {public int addtype(String name);public List<types> selets(types t);public int delete(types t);public int update(types t);public types selectone(types t);}

实现类程序:

public class TypeDaoImp implements TypeDao{Connection con=null;PreparedStatement ps=null;ResultSet rs=null;public int addtype(String name){int a=0;try {//连接数据库con=Shujuku.conn();String sql="insert into typesname values(?)";   //设置id自增ps=con.prepareStatement(sql);ps.setString(1, name);a=ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return a;}public List<types> selets(types t) {List<types> list=new ArrayList<types>();try {//连接数据库con=Shujuku.conn();String sql="select*from typesname";  ps=con.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()){types ty=new types();ty.setId(rs.getInt("id"));ty.setTypename(rs.getString("typename"));list.add(ty);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}/*******删除************************************************************************************/public int delete(types t) {int a=0;try {con=Shujuku.conn();String sql="delete from typesname where id="+t.getId();ps=con.prepareStatement(sql);a=ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return a;}/*******修改************************************************************************************/public int update(types t) {int a=0;try {con=Shujuku.conn();String sql="update typesname set typename=? where id=?";ps=con.prepareStatement(sql);ps.setString(1, t.getTypename());ps.setInt(2, t.getId());a=ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return a;}/*******查找一个************************************************************************************/public types selectone(types t) {types tp=new  types();try {con=Shujuku.conn();String sql="select * from typesname where id=?";ps=con.prepareStatement(sql);ps.setInt(1, t.getId());rs=ps.executeQuery();if(rs.next()){tp.setId(rs.getInt("id"));tp.setTypename(rs.getString("typename"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return tp;}}

最后就是数据库包,为了方便使用,将数据库的驱动连接信息建立一个包存放:

代码如下:

public class Shujuku {public static Connection conn(){//定义地址String url="jdbc:sqlserver://localhost:1433;DatabaseName=test;";    //定义连接初始值Connection connection=null;try {//加载驱动Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//建立连接     connection=DriverManager.getConnection(url, "sa", "DCX5201314");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return connection;}}

属性包,代码如下:

public class types {private int id;private String typename;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTypename() {return typename;}public void setTypename(String typename) {this.typename = typename;}}

java后台程序就这么多;


接下来是数据库部分:

数据库部分主要就是建立一张表,笔者使用的是SQL Server 2008,首先建个数据库test,建立个表typesname,设置两列分别为id typename,id设为主键,int 型,自增为1;typename设置类型为varchar型,不能为空。



好了,数据库和java后台都搭建好了,现在来到前端网页部分,


网页部分

在myeclipse中新建7个jsp文件



index.jsp是一个总的网页

设置代码如下:

</head><frameset rows="20%,80%"><frame src="head.jsp"><frameset cols="20%,80%"><frame src="left.jsp"><frame src="right.jsp" name="rights"></frameset></frameset><body></body>
head.jsp

<body>  <h1>这是头部</h1></body>

left.jsp

<body>    <h1>这是左边</h1>    <ul>       <li><a href="addtype.jsp" target="rights">添加新闻类型</a></li>       <li><a href="showtype.jsp" target="rights">查看新闻类型</a></li>    </ul>  </body>
right.jsp

<body>    <h1>这是右边</h1> </body>
addtype.jsp

</head>  <%    request.setCharacterEncoding("UTF-8");    String name= request.getParameter("typename");    if(name!=null){    TypeServlet tp=new TypeServlet();    int a=tp.addtype(name);    if(a>0){    RequestDispatcher rd = request.getRequestDispatcher("showtype.jsp");    rd.forward(request, response);    }else{    RequestDispatcher rd = request.getRequestDispatcher("addtype.jsp");    rd.forward(request, response);    }    }  %>  <body>    <h1>添加新闻类型</h1><hr/>    <form action="addtype.jsp" method="post">    新闻类型:<input type="text" name="typename"></br>        <input type="submit" value="提交">    </form>  </body>

showtype.jsp

<script type="text/javascript">function deletes_(id){    var f=confirm("是否确定删除?");   if(f){   location.href="showtype.jsp?ids="+id;   }else{   alert("您取消删除");   }}function update(id){location.href="updatetype.jsp?ids="+id;}</script>  </head>  <%  request.setCharacterEncoding("UTF-8");  String id=request.getParameter("ids");  String type=request.getParameter("type");  if(id!=null){      TypeServlet ts=new TypeServlet();  int a = ts.delete(Integer.parseInt(id));  response.sendRedirect("showtype.jsp");         }   %>  <body>    <h1> 展示类型</h1>    <table border="1">    <tr><td>编号</td><td>类型名称</td><td>操作</td></tr>    <%    //直接调用后台数据     TypeServlet ts=new TypeServlet();     List<types> list=ts.selets();     for(int i=0;i<list.size();i++){     types n=list.get(i);     %>     <tr>  <td><%=n.getId() %></td>  <td><%=n.getTypename() %></td>  <td><input type="button" onclick="update(<%=n.getId() %>)" value="修改"/>         <input type="button" onclick="deletes_(<%=n.getId() %>)" value="删除"/></td>  </tr>    <%     }     %>  </body>

updatetype.jsp

<body>    <%     request.setCharacterEncoding("UTF-8");     String id=request.getParameter("ids");    TypeServlet tsl=new TypeServlet();    types ts=new types();    String type= request.getParameter("type");    if(type!=null){    String typename=request.getParameter("newtype");//从下面的输入取值    String id1=request.getParameter("id");    ts.setId(Integer.parseInt(id1));//强转    ts.setTypename(typename);    int a=tsl.update(ts);    response.sendRedirect("showtype.jsp");    }else{      if(id!=null){         ts=tsl.selectone(Integer.parseInt(id));      }      }    %>        <h1>修改新闻类型界面</h1>    <hr/>    <form action="updatetype.jsp" method="post">    <input type="hidden" name="type" value="3">    <input type="hidden" name="id" value="<%=ts.getId() %>">      新闻类型:<input type="text" value="<%=ts.getTypename() %>" name="newtype"><br/>      <input type="submit" value="提交">    </form>  </body>

最终项目在tomcat上发布。

下面的地址积分系统调的太高了,我重新上传了一份内容是一样的地址在这:http://download.csdn.net/download/qq_34178998/10154005

高积分下载打包文件在这:http://download.csdn.net/download/qq_34178998/9920064

也可以参考在这篇基础上的两个表关联操作:http://blog.csdn.net/qq_34178998/article/details/77017760

有问题也希望各位提出,一起进步大笑




阅读全文
2 0
原创粉丝点击