当前位置:   article > 正文

JSP + JDBC 技术对数据库进行增删改查操作_编写jsp页面通过使用jdbc技术实现对学生表中的数据增删改查

编写jsp页面通过使用jdbc技术实现对学生表中的数据增删改查

遇到了很多问题:

1.字符编码问题

error:Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
解决:String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";

主要是由于每个jsp文件的编码方式不同,数据库传过来的参数jsp页面无法识别,导致500错误;

2.数据库操作不对应

怪我数据库每学扎实,insert数据的时候忘记了table后面的参数:insert table(sno,sname) value(?,?);

3.没有进行关联

这个困扰了我很久,驱动也放到了lib文件夹下面,就是一直报错;
解决:右击项目——>  build path  ——>  add jars  ——>找到目前项目对应的驱动包的位置即可;

3 问题图片

4.java web项目中如何引入css/js代码

  1. //文件的开始需要写这一段,以便写入绝对路径,相对路径容易访问失败
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://" + request.getServerName() + ":"
  5. + request.getServerPort() +path + "/";
  6. %>

 然后head里面加入link即可,我的文档结构 : sy3项目名称/css文件名/query.css

  1. <head>
  2. <link rel = "stylesheet" type = "text/css" href = "/sy3/css/query.css">
  3. </head>

疑问:

①  一个eclipse可以下载多个tomcat么?

答:可以,打开server窗口,需要用哪个tomcat就启动哪个,然后把项目部署到tomcat上,打开网址输入相应的端口号即可。

②  同时下载了mysql与sql server会冲突吗?

答:不会,两个数据库的驱动不一样,把对应的驱动放到lib文件下面,并写入完整的驱动代码即可。

③  驱动应该怎么下载?版本不对应会影响么?

答:一般没什么影响,我mysql是8.0版本的,驱动是5.0.8版本,可以正常启动。

基本步骤:

①  打开eclipse,新建项目Java web

②  部署驱动到lib文件夹,并与当前的项目关联

③  写代码

④  打开server窗口,将项目部署到tomcat中,启动tomcat

⑤  在浏览器窗户输入ip地址:http://localhost:8080/sy3/mysqlquery.jsp

附:源码

1.mysqlquery.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@page import="java.sql.*"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme() + "://" + request.getServerName() + ":"
  6. + request.getServerPort() +path + "/";
  7. %>
  8. <html>
  9. <head>
  10. <link rel = "stylesheet" type = "text/css" href = "/sy3/css/query.css">
  11. </head>
  12. <body>
  13. <div class = "dv1" align = "center">
  14. <span class="title">MYSQL数据库连接</span>
  15. </div>
  16. <%!
  17. public static final String DBDRIVER="com.mysql.jdbc.Driver";
  18. public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
  19. public static final String DBUSER="root";
  20. public static final String DBPASS="imshsyxxarsh";
  21. %>
  22. <%
  23. Connection conn=null;
  24. PreparedStatement pstmt=null;
  25. ResultSet rs=null;
  26. Class.forName(DBDRIVER); //
  27. conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS); //�������Ӷ���
  28. String sql="select * from xs";
  29. pstmt=conn.prepareStatement(sql); //��ӡ�ɾ�����޸�
  30. rs=pstmt.executeQuery();
  31. %>
  32. <div class = "dv2" align = "center">
  33. <table class = "table">
  34. <tr style = "background-color:#FF6666; height:60px; color:white;">
  35. <td width = "33%">学号</td>
  36. <td width = "33%">姓名</td>
  37. <td>编辑</td>
  38. </tr>
  39. <%
  40. int num=1;
  41. while(rs.next()){
  42. if(num%2!=0) {
  43. %>
  44. <tr>
  45. <td style = "height:60px;">
  46. <%=rs.getString("sno") %>
  47. </td>
  48. <td>
  49. <%=rs.getString("sname") %>
  50. </td>
  51. <td>
  52. <a class = "lik" href="mysqlupdate.jsp?sno=<%=rs.getString("sno")%>&&sname=<%=rs.getString("sname")%>">update</a>&nbsp;&nbsp;<a class = "lik" href="mysqldelete.jsp?sno=<%=rs.getString("sno")%>">delete</a>
  53. </td>
  54. </tr>
  55. <%
  56. }else {
  57. %>
  58. <tr style = "background-color:#FFCCCC; height:60px;">
  59. <td>
  60. <%=rs.getString("sno") %>
  61. </td>
  62. <td>
  63. <%=rs.getString("sname") %>
  64. </td>
  65. <td>
  66. <a class = "lik" href="mysqlupdate.jsp?sno=<%=rs.getString("sno")%>&&sname=<%=rs.getString("sname")%>">update</a>&nbsp;&nbsp;<a class = "lik" href="mysqldelete.jsp?sno=<%=rs.getString("sno")%>">delete</a>
  67. </td>
  68. </tr>
  69. <%
  70. }
  71. num++;
  72. }
  73. %>
  74. </table>
  75. <br><br>
  76. <div class = "lik2"><a class="title2" href="mysqlinsert.html">添加学生基本信息</a></div>
  77. </div>
  78. <%
  79. rs.close();
  80. pstmt.close();
  81. conn.close();
  82. %>
  83. </body>
  84. </html>

2. mysqldelete.jsp

  1. <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <%
  7. String sno=request.getParameter("sno");
  8. %>
  9. <%!
  10. public static final String DBDRIVER="com.mysql.jdbc.Driver";
  11. public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
  12. public static final String DBUSER="root";
  13. public static final String DBPASS="imshsyxxarsh";
  14. %>
  15. <%
  16. Connection conn=null;
  17. PreparedStatement pstmt=null;
  18. ResultSet rs=null;
  19. Class.forName(DBDRIVER);
  20. conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
  21. String sql="delete from xs where sno=?";
  22. pstmt=conn.prepareStatement(sql);
  23. pstmt.setString(1,sno);
  24. pstmt.executeUpdate();
  25. %>
  26. <%
  27. pstmt.close();
  28. conn.close();
  29. response.sendRedirect("mysqlquery.jsp");
  30. %>
  31. </body>
  32. </html>

3.mysqlinsert.jsp

  1. <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
  2. <body>
  3. <%
  4. //request.setCharacterEncoding("utf-8");
  5. String sno=request.getParameter("usernum");
  6. String sname=request.getParameter("username");
  7. String sname1="章";
  8. System.out.println(sname1);
  9. sname=new String(sname.getBytes("ISO-8859-1"), "gb2312");
  10. System.out.println(sname);
  11. %>
  12. <%!
  13. public static final String DBDRIVER="com.mysql.jdbc.Driver";
  14. public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
  15. public static final String DBUSER="root";
  16. public static final String DBPASS="imshsyxxarsh";
  17. %>
  18. <%
  19. Connection conn=null;
  20. PreparedStatement pstmt=null;
  21. ResultSet rs=null;
  22. Class.forName(DBDRIVER);
  23. conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
  24. String sql="insert into xs(sno,sname) values(?,?)";
  25. pstmt=conn.prepareStatement(sql);
  26. pstmt.setString(1,sno);
  27. pstmt.setString(2,sname);
  28. pstmt.executeUpdate();
  29. %>
  30. <%
  31. pstmt.close();
  32. conn.close();
  33. response.sendRedirect("mysqlquery.jsp");
  34. %>
  35. </body>

4.mysqlinsert.html

  1. <head>
  2. <link href = "/sy3/css/query.css" rel = "stylesheet" type = "text/css">
  3. </head>
  4. <body>
  5. <div class = "submit" align = "center">
  6. <form action="mysqlinsert.jsp" method="post">
  7. <table class = "table2">
  8. <tr height = "60px;">
  9. <td width = "150px" align = "right">学号 :</td>
  10. <td><input class = "put" type="text" name="usernum"></td>
  11. </tr>
  12. <tr height = "60px;">
  13. <td align = "right"> 姓名:</td>
  14. <td><input class = "put" type="text" name="username"></td>
  15. </tr>
  16. <tr height = "60px;" align = "center">
  17. <td colspan = "2"><input class = "sub" type="submit" value="提交 "></td>
  18. </tr>
  19. </table>
  20. </form>
  21. </div>
  22. </body>

5.mysqlupdate.jsp

  1. <%@ page language = "java" pageEncoding = "utf-8" %>
  2. <head>
  3. <link href = "/sy3/css/query.css" rel = "stylesheet" type = "text/css">
  4. </head>
  5. <body>
  6. <%
  7. String sname=request.getParameter("sname");
  8. //byte b[]=sname.getBytes("ISO-8859-1");
  9. //sname=new String(b);
  10. sname=new String(sname.getBytes("ISO-8859-1"),"gb2312");
  11. %>
  12. <div class = "submit" align = "center">
  13. <form action="mysqlupdate1.jsp" method="post">
  14. <table class = "table2">
  15. <tr height = "60px;">
  16. <td width = "150px" align = "right">学号 :</td>
  17. <td><input class = "put" type="text" name="usernum" value="<%=request.getParameter("sno")%>"></td>
  18. </tr>
  19. <tr height = "60px;">
  20. <td align = "right"> 姓名:</td>
  21. <td><input class = "put" type="text" name="username" value="<%=sname%>"></td>
  22. </tr>
  23. <tr height = "60px;" align = "center">
  24. <td colspan = "2"><input class = "sub" type="submit" value="提交 "></td>
  25. </tr>
  26. </table>
  27. </form>
  28. </div>
  29. </body>
  30. </html>

6.mysqlupdate1.jsp

  1. <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <%
  7. request.setCharacterEncoding("utf-8");
  8. String sno=request.getParameter("usernum");
  9. String sname=request.getParameter("username");
  10. %>
  11. <%!
  12. public static final String DBDRIVER="com.mysql.jdbc.Driver";
  13. public static final String DBURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
  14. public static final String DBUSER="root";
  15. public static final String DBPASS="imshsyxxarsh";
  16. %>
  17. <%
  18. Connection conn=null;
  19. PreparedStatement pstmt=null;
  20. ResultSet rs=null;
  21. Class.forName(DBDRIVER);
  22. conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
  23. String sql="update xs set sname=? where sno=?";
  24. pstmt=conn.prepareStatement(sql);
  25. pstmt.setString(2,sno);
  26. pstmt.setString(1,sname);
  27. pstmt.executeUpdate();
  28. %>
  29. <%
  30. pstmt.close();
  31. conn.close();
  32. response.sendRedirect("mysqlquery.jsp");
  33. %> <br>
  34. </body>
  35. </html>

7.query.css

  1. @charset "UTF-8";
  2. body{
  3. color:#666666;
  4. }
  5. .title{
  6. display:block;
  7. width:400px;
  8. margin:auto;
  9. margin-top:80px;
  10. color:#666666;
  11. border:2px;
  12. font-size:40px;
  13. font-weight:bold;
  14. }
  15. a{
  16. text-decoration:none;
  17. }
  18. .lik{
  19. color:#993366;
  20. }
  21. .dv1{
  22. width:100%;
  23. height:120px;
  24. border:2px;
  25. }
  26. .dv2{
  27. width:100%;
  28. }
  29. .lik2{
  30. width:200px;
  31. height:35px;
  32. border-radius:14px;
  33. padding-top:5px;
  34. background-color:#FF6666;
  35. }
  36. .title2{
  37. font-size:20px;
  38. font-weight:blod;
  39. color:white;
  40. margin:auto;
  41. }
  42. .table{
  43. width:80%;
  44. border:1px solid #ffcece;
  45. }
  46. .submit{
  47. background:#ffcece;
  48. height:600px;
  49. padding-top:200px;
  50. }
  51. .put{
  52. width:300px;
  53. height: 40px;
  54. }
  55. .sub{
  56. border:none;
  57. background:#FF6666;
  58. width:70px;
  59. height:30px;
  60. color:white;
  61. font-size:15px;
  62. }
  63. .table2{
  64. font-size:15px;
  65. font-weight:bold;
  66. align:center;
  67. width:40%;
  68. }

深深记得老师的话:你就不听我讲,这能学会啦???那魔性的声音,我相信,他不会看的,hhhh,我偏偏要学会,能咋地,丢了老脸又何妨?

告大家一个小秘密:我们东来老师,,,还喷香水,,,,hhhhhhh

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/575402
推荐阅读
相关标签
  

闽ICP备14008679号