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

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

 

java后台程序:

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

性截个图

页面如何获取数据库数据

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

servlet层代码如下:

 

 
  1. public class TypeServlet {

  2.  
  3. TypeService ts=new TypeServiceImp();//调用service层

  4. /*******添加************************************************************************************/

  5. public int addtype(String name){

  6. int a=0;

  7. a=ts.addtype(name);

  8. return a;

  9. }

  10. /*******查看************************************************************************************/

  11. public List<types> selets(){

  12. List<types> list=new ArrayList<types>();

  13. list=ts.selets(null);

  14. return list;

  15. }

  16. /*******删除************************************************************************************/

  17. public int delete(int id){

  18. int a=0;

  19. types t=new types();

  20. t.setId(id);

  21. a=ts.delete(t);

  22. return a;

  23. }

  24. /*******修改************************************************************************************/

  25. public int update(types t){

  26. int a=0;

  27. a=ts.update(t);

  28. return a;

  29. }

  30. /*******查找一个************************************************************************************/

  31. public types selectone(int id){

  32. types t=new types();

  33. t.setId(id);

  34. types nt=ts.selectone(t);

  35. return nt;

  36. }

  37. }

 

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

页面如何获取数据库数据

 

接口程序如下:

 

 
  1. public interface TypeService {

  2.  
  3. public int addtype(String name);

  4.  
  5. public List<types> selets(types t);

  6.  
  7. public int delete(types t);

  8.  
  9. public int update(types t);

  10.  
  11. public types selectone(types t);

  12.  
  13. }


接口实现程序:

 

 

 
  1. public class TypeServiceImp implements TypeService{

  2.  
  3. TypeDao td= new TypeDaoImp();

  4.  
  5. public int addtype(String name) { //注意返回数据不要忘记修改

  6. int a=0;

  7. a=td.addtype(name);

  8. return a;

  9. }

  10.  
  11. public List<types> selets(types t) {

  12. List<types> list=new ArrayList<types>();

  13. list=td.selets(t);

  14. return list;

  15. }

  16. /*******删除************************************************************************************/

  17. public int delete(types t) {

  18. int a=0;

  19. a=td.delete(t);

  20. return a;

  21. }

  22. /*******修改************************************************************************************/

  23. public int update(types t) {

  24. int a=0;

  25. a=td.update(t);

  26. return a;

  27. }

  28.  
  29. /*******查找单个************************************************************************************/

  30. public types selectone(types t){

  31. types tp=new types();

  32. tp=td.selectone(t);

  33. return tp;

  34.  
  35. }

  36. }

 

 

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

接口层程序:

 

 
  1. public interface TypeDao {

  2.  
  3. public int addtype(String name);

  4.  
  5. public List<types> selets(types t);

  6.  
  7. public int delete(types t);

  8.  
  9. public int update(types t);

  10.  
  11. public types selectone(types t);

  12.  
  13. }


实现类程序:

 

 

 
  1. public class TypeDaoImp implements TypeDao{

  2. Connection con=null;

  3. PreparedStatement ps=null;

  4. ResultSet rs=null;

  5.  
  6. public int addtype(String name){

  7. int a=0;

  8. try {

  9. //连接数据库

  10. con=Shujuku.conn();

  11. String sql="insert into typesname values(?)"; //设置id自增

  12. ps=con.prepareStatement(sql);

  13. ps.setString(1, name);

  14. a=ps.executeUpdate();

  15. } catch (SQLException e) {

  16. // TODO Auto-generated catch block

  17. e.printStackTrace();

  18. }

  19. return a;

  20. }

  21.  
  22. public List<types> selets(types t) {

  23. List<types> list=new ArrayList<types>();

  24. try {

  25. //连接数据库

  26. con=Shujuku.conn();

  27. String sql="select*from typesname";

  28. ps=con.prepareStatement(sql);

  29. rs=ps.executeQuery();

  30. while(rs.next()){

  31. types ty=new types();

  32. ty.setId(rs.getInt("id"));

  33. ty.setTypename(rs.getString("typename"));

  34. list.add(ty);

  35. }

  36. } catch (SQLException e) {

  37. // TODO Auto-generated catch block

  38. e.printStackTrace();

  39. }

  40. return list;

  41. }

  42. /*******删除************************************************************************************/

  43. public int delete(types t) {

  44. int a=0;

  45. try {

  46. con=Shujuku.conn();

  47. String sql="delete from typesname where id="+t.getId();

  48. ps=con.prepareStatement(sql);

  49. a=ps.executeUpdate();

  50. } catch (SQLException e) {

  51. // TODO Auto-generated catch block

  52. e.printStackTrace();

  53. }

  54. return a;

  55. }

  56. /*******修改************************************************************************************/

  57. public int update(types t) {

  58. int a=0;

  59. try {

  60. con=Shujuku.conn();

  61. String sql="update typesname set typename=? where id=?";

  62. ps=con.prepareStatement(sql);

  63. ps.setString(1, t.getTypename());

  64. ps.setInt(2, t.getId());

  65. a=ps.executeUpdate();

  66. } catch (SQLException e) {

  67. // TODO Auto-generated catch block

  68. e.printStackTrace();

  69. }

  70. return a;

  71. }

  72. /*******查找一个************************************************************************************/

  73. public types selectone(types t) {

  74. types tp=new types();

  75. try {

  76. con=Shujuku.conn();

  77. String sql="select * from typesname where id=?";

  78. ps=con.prepareStatement(sql);

  79. ps.setInt(1, t.getId());

  80. rs=ps.executeQuery();

  81. if(rs.next()){

  82. tp.setId(rs.getInt("id"));

  83. tp.setTypename(rs.getString("typename"));

  84. }

  85. } catch (SQLException e) {

  86. // TODO Auto-generated catch block

  87. e.printStackTrace();

  88. }

  89. return tp;

  90. }

  91. }

 

 

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

代码如下:

 

 
  1. public class Shujuku {

  2.  
  3. public static Connection conn(){

  4. //定义地址

  5. String url="jdbc:sqlserver://localhost:1433;DatabaseName=test;";

  6. //定义连接初始值

  7. Connection connection=null;

  8. try {

  9. //加载驱动

  10. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

  11. //建立连接

  12. connection=DriverManager.getConnection(url, "sa", "DCX5201314");

  13. } catch (SQLException e) {

  14. // TODO Auto-generated catch block

  15. e.printStackTrace();

  16.  
  17. } catch (ClassNotFoundException e) {

  18. // TODO Auto-generated catch block

  19. e.printStackTrace();

  20. }

  21. return connection;

  22. }

  23. }


属性包,代码如下:

 

 

 
  1. public class types {

  2. private int id;

  3. private String typename;

  4. public int getId() {

  5. return id;

  6. }

  7. public void setId(int id) {

  8. this.id = id;

  9. }

  10. public String getTypename() {

  11. return typename;

  12. }

  13. public void setTypename(String typename) {

  14. this.typename = typename;

  15. }

  16. }

 

 

java后台程序就这么多;

 

接下来是数据库部分:

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

页面如何获取数据库数据

 

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

 

网页部分

在myeclipse中新建7个jsp文件

 

页面如何获取数据库数据

index.jsp是一个总的网页

设置代码如下:

 

 
  1. </head>

  2. <frameset rows="20%,80%">

  3. <frame src="head.jsp">

  4. <frameset cols="20%,80%">

  5. <frame src="left.jsp">

  6. <frame src="right.jsp" name="rights">

  7. </frameset>

  8. </frameset>

  9. <body>

  10. </body>

head.jsp

 

 

 
  1. <body>

  2. <h1>这是头部</h1>

  3. </body>

 

left.jsp

 

 
  1. <body>

  2. <h1>这是左边</h1>

  3. <ul>

  4. <li><a href="addtype.jsp" target="rights">添加新闻类型</a></li>

  5. <li><a href="showtype.jsp" target="rights">查看新闻类型</a></li>

  6. </ul>

  7. </body>

right.jsp

 

 

 
  1. <body>

  2. <h1>这是右边</h1>

  3. </body>

addtype.jsp

 

 

 
  1. </head>

  2. <%

  3. request.setCharacterEncoding("UTF-8");

  4. String name= request.getParameter("typename");

  5. if(name!=null){

  6. TypeServlet tp=new TypeServlet();

  7. int a=tp.addtype(name);

  8. if(a>0){

  9. RequestDispatcher rd = request

  10. .getRequestDispatcher("showtype.jsp");

  11. rd.forward(request, response);

  12. }else{

  13. RequestDispatcher rd = request

  14. .getRequestDispatcher("addtype.jsp");

  15. rd.forward(request, response);

  16. }

  17. }

  18. %>

  19. <body>

  20. <h1>添加新闻类型</h1><hr/>

  21. <form action="addtype.jsp" method="post">

  22. 新闻类型:<input type="text" name="typename"></br>

  23. <input type="submit" value="提交">

  24. </form>

  25. </body>

showtype.jsp

 

 

 
  1. <script type="text/javascript">

  2. function deletes_(id){

  3.  
  4. var f=confirm("是否确定删除?");

  5. if(f){

  6. location.href="showtype.jsp?ids="+id;

  7. }else{

  8. alert("您取消删除");

  9. }

  10. }

  11.  
  12. function update(id){

  13. location.href="updatetype.jsp?ids="+id;

  14. }

  15. </script>

  16. </head>

  17. <%

  18. request.setCharacterEncoding("UTF-8");

  19. String id=request.getParameter("ids");

  20. String type=request.getParameter("type");

  21. if(id!=null){

  22. TypeServlet ts=new TypeServlet();

  23. int a = ts.delete(Integer.parseInt(id));

  24. response.sendRedirect("showtype.jsp");

  25.  
  26. }

  27. %>

  28. <body>

  29. <h1> 展示类型</h1>

  30. <table border="1">

  31. <tr><td>编号</td><td>类型名称</td><td>操作</td></tr>

  32. <%

  33. //直接调用后台数据

  34. TypeServlet ts=new TypeServlet();

  35. List<types> list=ts.selets();

  36. for(int i=0;i<list.size();i++){

  37. types n=list.get(i);

  38. %>

  39. <tr>

  40. <td><%=n.getId() %></td>

  41. <td><%=n.getTypename() %></td>

  42. <td><input type="button" onclick="update(<%=n.getId() %>)" value="修改"/>

  43.   

  44. <input type="button" onclick="deletes_(<%=n.getId() %>)" value="删除"/></td>

  45. </tr>

  46. <%

  47. }

  48. %>

  49. </body>

updatetype.jsp

 

 
  1. <body>

  2. <%

  3. request.setCharacterEncoding("UTF-8");

  4. String id=request.getParameter("ids");

  5. TypeServlet tsl=new TypeServlet();

  6. types ts=new types();

  7. String type= request.getParameter("type");

  8. if(type!=null){

  9. String typename=request.getParameter("newtype");//从下面的输入取值

  10. String id1=request.getParameter("id");

  11. ts.setId(Integer.parseInt(id1));//强转

  12. ts.setTypename(typename);

  13. int a=tsl.update(ts);

  14. response.sendRedirect("showtype.jsp");

  15. }else{

  16. if(id!=null){

  17. ts=tsl.selectone(Integer.parseInt(id));

  18. }

  19. }

  20. %>

  21.  
  22. <h1>修改新闻类型界面</h1>

  23. <hr/>

  24. <form action="updatetype.jsp" method="post">

  25. <input type="hidden" name="type" value="3">

  26. <input type="hidden" name="id" value="<%=ts.getId() %>">

  27. 新闻类型:<input type="text" value="<%=ts.getTypename() %>" name="newtype"><br/>

  28. <input type="submit" value="提交">

  29. </form>

  30. </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

有问题也希望各位提出,一起进步页面如何获取数据库数据

页面如何获取数据库数据

 

相关文章: