1.建立数据库连接要配置 jdbc.properties,且要放在src目录下,不能放在web目录下
否则会有错误信息At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
java.lang.NullPointerException
url=jdbc:mysql://localhost:3306/pest driverClass=com.mysql.jdbc.Driver user=root password=1234
2.要导入这几个包,不然连接时会找不到
3.在写getConnection()方法时,不能关闭connection,否则后面无法获得数据库连接
4.注意导入到是,mysql和sql分清楚
5.在传入的sql语句中,若含有占位符,则必须只写? 不能写成"?" 或者'?'
conn.prepareStatement("insert into users(name,password,email,birthday) values('?','?','?','?')")
应该写作:
conn.prepareStatement("insert into users(name,password,email,birthday) values(?,?,?,?)");
6.遍历ResultSet时,下标是从1开始,首先要有resultSet.next()
7.使用标准标签库时JSTL时,要先导入两个包
8.注意在使用重定向到某页面时,request.setAttribute的东西读取不到,需要HttpSession session = request.getSession(), session.setAttribute() 然后在jsp页面上利用sessionScope.来获取
9.折叠菜单 看另外一篇折叠菜单blog
10.显示本地图片到浏览器:注意要把图片放在web文件夹下,才能显示在浏览器上<img border="0" src="/image/4.jpg" height="45%" width="45%" class="PIC"/>
<div style="font-size:100px"> <img border="0" src="/image/2.jpg" height="45%" width="45%" class="PIC"/> <img border="0" src="/image/3.jpg" height="45%" width="45%" class="PIC"/> <img border="0" src="/image/4.jpg" height="45%" width="45%" class="PIC"/> <img border="0" src="/image/5.jpg" height="45%" width="45%" class="PIC"/> </div>
11.jsp中遍历List
利用jstl的forEach来实现
注意判断条件需要放在大括号里面 <c:if test="${sessionScope.discussRecordList == null} ">
<%--从ExpertsServlet中读取数据库中全部的专家信息,并显示--%> <c:if test="${sessionScope.experts == null}"> <jsp:forward page="/ExpertsServlet"></jsp:forward> </c:if> <c:if test="${sessionScope.experts != null}"> <div <%--class="div-right"--%> align="center"> <%--上 右 下 左--%> <table width="400" style="margin:50px 0 0 10%" border="1" cellspacing="0" cellpadding="0"> <tr> <td>姓名</td> <td colspan="3">工作单位</td> <td>专长</td> <td>职务</td> <td>电话</td> </tr> <c:forEach items="${sessionScope.experts}" var="experts1"> <tr> <td>${experts1.name}</td> <td colspan="3">${experts1.company}</td> <td>${experts1.expertise}</td> <td>${experts1.position}</td> <td>${experts1.phone}</td> </tr> </c:forEach> </table> </div> </c:if>
12.编写DAO的Servlet时,从jsp传入需要调用的方法名,可以设置为<form action="insert.do" method="post"> 并且将该Servlet的配置文件配置为*.do,这样在Servlet中,就可以利用getServletPath()来获取该方法名
// /experts/event/insert.do String servletPath = request.getServletPath(); String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1); //insert methodName = methodName.substring(0, methodName.length() - 3);
在doPOST方法中调用相应的DAO方法:
//1.获取servletPath:/add.do 或 /delete.do等信息 String servletPath = req.getServletPath(); //2.取出/和.do得到方法名 String methodName = servletPath.substring(1); methodName = methodName.substring(0, methodName.length() - 3); //3.利用反射获取methodName对应的方法 add query delete等下面写的方法 Method method = null; try { method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //4.利用发射调用对应的方法 add query delete等下面写的方法 method.invoke(this, req, resp); } catch (Exception e) { e.printStackTrace(); resp.sendRedirect("error.jsp"); }
13.乱码问题:
1)从web端存入数据到数据库,结果存入数据库的中文数据出现乱码。当然读取出来的时候也是乱码乱码的原因是tomcat的内部编码格式iso8859-1导致。 而在每个jsp页面设置的utf-8仅仅是该页面显示是用utf-8,而你用form表单提交的数据仍然是iso8859-1.所以要在接受form表单数据的servlet页面加上一行代码request.setCharacterEncoding("utf-8").
String name = request.getParameter("name");
name=new String(name.getBytes("ISO-8859-1"),"UTF-8");
/experts/record.do String servletPath = request.getServletPath(); String methodName = servletPath.substring(servletPath.lastIndexOf("/")); methodName = methodName.substring(1, methodName.length() - 3); record
19.利用href从jsp传递多个参数到Servlet 利用?连接,多个参数之间利用一个&连接
<td colspan="3"><input type="button" value="查看"
onclick="window.location.href='query.do?name=${experts.name}&expertise=${experts.expertise}&company=${experts.company}'"> </td>
20.MySQL联合查询语法内联、左联、右联、全联
第一:内联( inner join )
如果想把用户信息、积分、等级都列出来,那么一般会这样写:
select * from T1, T3 where T1.userid = T3.userid (其实这样的结果等同于 select * from T1 inner join T3 on T1.userid=T3.userid )。
把两个表中都存在userid的行拼成一行(即内联),但后者的效率会比前者高很多,建议用后者(内联)的写法。
SQL语句:select * from T1 inner join T2 on T1.userid = T2.userid
第二:左联( left join )
显示左表T1中的所有行,并把右表T2中符合条件加到左表T1中;
右表T2中不符合条件,就不用加入结果表中,并且NULL表示。
SQL语句:select * from T1 left join T2 on T1.userid = T2.userid
第三:右联( right join )。
显示右表T2中的所有行,并把左表T1中符合条件加到右表T2中;
左表T1中不符合条件,就不用加入结果表中,并且NULL表示。
SQL语句:select * from T1 right join T2 on T1.userid = T2.userid
第四:全联( full join )
显示左表T1、右表T2两边中的所有行,即把左联结果表 + 右联结果表组合在一起,然后过滤掉重复的。
SQL语句:select * from T1 full join T2 on T1.userid = T2.userid
21.多对多关系表
//建立选修表 CREATE TABLE sc( sid VARCHAR(10), cid VARCHAR(10) ); 添加主键组 ALTER TABLE sc ADD CONSTRAINT pk_sc PRIMARY KEY(sid,cid); 添加外键约束 ALTER TABLE sc ADD CONSTRAINT fk_student FOREIGN KEY(sid) REFERENCES student(id); ALTER TABLE sc ADD CONSTRAINT fk_course FOREIGN KEY(cid) REFERENCES course(id);
查询时注意:要想用哪个其他表的条件,记得用LEFT JOIN或其他的JOIN先声明一下
SELECT experts.* FROM experts LEFT JOIN info ON experts.name=info.expert LEFT JOIN record ON record.event=info.event WHERE record.event='卧龙鼠害事件';
22.多选用checkbox
<form action="expertList.re" method="post"> <tr> <td>会商人员</td> <br> </tr> <c:forEach items="${requestScope.experts}" var="exp"> <tr> <td><input type="checkbox" name="ex" value="${exp.name}">${exp.name}</td> <br> </tr> </c:forEach> <input type="submit" value="添加会商信息"> </form>
在Servlet中获取checkbox选中的属性
request.setCharacterEncoding( "utf-8" ); //防止乱码 String[] ex = (String[])request.getParameterValues("ex"); if(ex != null && ex.length > 0){ for(int i = 0; i < ex.length; i++){ out.println(ex[i]); } }