2.14 Java web 复习总结

1.空指针异常原因(NullPointerExceptio)之一:

在Dao层里边 声明

1 Connection conn = DBUtil.getConn();  //不能少
2 Statement state=null;

如果conn 没有与数据库连接就会报错空指针异常

 2.sql 语句

插入:

String sql= "insert into login(name,pwd,status)values('"+id+"','"+id+"','"+1+"')";

注意双引号和单引号的位置 以及获取表单信息 的方式 可以直接给出 也可制作选项让用户选择

查询:

模糊查询

String sql="select * from student where class like '%"+id+"%'";

具体查询:

String sql="select * from tb_qingnian";

 *  代表所有元素 可以改变为 具体的表单元素

修改:

String sql="update tb_qingnian set sex='"+e.getSex()+"',mianmao='"+e.getMianmao()+"',fuwuleibie='"+e.getFuwuleibie()+"'   where name='"+e.getName()+"' ";

删除:

String sql="delete from tb_qingnian where name='"+name+"'"  ;

示例:

执着选项 将表单元素设置为选项的名称,传递给sql语句

jsp文件
<select id="type1" name="type1"> <option value="name">消费类型</option> <option value="money">消费金额</option> <option value="place">消费地点</option> <option value="shijian">消费时间</option> </select>

Servlet 中

String type1=req.getParameter("type1");
String info=req.getParameter("find");
List<info> list =dao.find(type1,info);

dao 包

public static List<info> find(String type1, String info) throws Exception {
Connection conn = DBUtil.getConn();
List<info> list=new ArrayList<>();
Statement state=null;
String sql="select * from info where "+type1+" = '"+info+"'";
state=conn.createStatement();
ResultSet rs=state.executeQuery(sql);
info infos=null;
while(rs.next()) {
String name=rs.getString("name");
String money=rs.getString("money");
String place=rs.getString("place");
String time=rs.getString("shijian");
infos =new info(name,money,place,time);
list.add(infos);
}
return list;
}

这样就可灵活查找了

3.Navicat Premium 出现错误

 创建表单时 勾选了虚拟  目前为什么不清楚 等待解决。 

原文地址:https://www.cnblogs.com/cxy0210/p/12308324.html