增加语法
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//设置编码方式
request.setCharacterEncoding("utf-8");
//接收表单的值 根据name取value值
String ntid=request.getParameter("ntid");
String ntitle=request.getParameter("ntitle");
String nauthor=request.getParameter("nauthor");
String nsummary=request.getParameter("nsummary");
String ncontent=request.getParameter("ncontent");
String naddtime=new Date().toLocaleString();//系统当前时间
/*
主键xid(目标是为了唯一且不为空)
1.标识列:序列+触发器
2.获取主键的最大序号+1
*/
//两个字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String cname="oracle.jdbc.driver.OracleDriver";
//加载驱动
Class.forName(cname);
//创建连接
Connection con=DriverManager.getConnection(url, "scott", "tiger");
//定义sql语句
String sql="select nvl(max(xid),0) from xw";
//获得执行对象
PreparedStatement ps=con.prepareStatement(sql);
//获得结果集
ResultSet rs=ps.executeQuery();
int xid=0;//扩大作用域
//判断/遍历
if(rs.next()){
//赋值
xid=rs.getInt(1)+1;//获取主键的最大序号+1
}
//开始执行增加操作
sql="insert into xw(xzt,xbt,xzz,xzy,xnr,xsj,xid) values(?,?,?,?,?,?,?)";
//重新执行sql语句
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, Integer.parseInt(ntid));
ps.setString(2, ntitle);
ps.setString(3, nauthor);
ps.setString(4, nsummary);
ps.setString(5, ncontent);
ps.setString(6, naddtime);
ps.setInt(7, xid);
//开始执行
int n=ps.executeUpdate();
//关闭资源
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
//判断
if(n>0){
response.sendRedirect("/javaWeb4/news/admin.jsp");
}else{
out.print("<script>alert('发表失败');location.href='add.jsp';</script>");
}
%>
删除语法
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%
//接收xid
String xid=request.getParameter("xid");
//jdbc根据id进行删除
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String cname="oracle.jdbc.driver.OracleDriver";
Class.forName(cname);
Connection con=DriverManager.getConnection(url,"scott","tiger");
String sql="delete from xw where xid="+xid;
PreparedStatement ps=con.prepareStatement(sql);
int n=ps.executeUpdate();
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(n>0){
//删除成功
response.sendRedirect("/javaWeb5/news/admin.jsp");
}else{
//删除失败
out.print("<script>alert('删除失败');location.href='admin.jsp';</script>");
}
%>
查询单个语法
<body> <% //接收从admin.jsp传递过来的xid String xid=request.getParameter("xid"); //思路:根据xid拿到其对应的信息 String url="jdbc:oracle:thin:@localhost:1521:orcl"; String cname="oracle.jdbc.driver.OracleDriver"; Class.forName(cname); Connection con=DriverManager.getConnection(url,"scott","tiger"); String sql="select * from xw where xid="+xid;//单个查询 PreparedStatement ps=con.prepareStatement(sql); ResultSet rs=ps.executeQuery(); //扩大作用域 String xbt="";//标题 String xzz="";//作者 String xsj="";//时间 String xnr="";//内容 if(rs.next()){ xbt=rs.getString(2); xzz=rs.getString(3); xnr=rs.getString(5); xsj=rs.getString(7); } if(con!=null&&!con.isClosed()){ con.close(); } if(ps!=null){ ps.close(); } if(rs!=null){ rs.close(); } %> <table width="80%" align="center"> <tr width="100%"> <td colspan="2" align="center"><%=xbt %></td> </tr> <tr> <td colspan="2"><hr /> </td> </tr> <tr> <td align="center"><%=xsj %></td> <td align="left"><%=xzz %></td> </tr> <tr> <td colspan="2" align="center"></td> </tr> <tr> <td colspan="2"><%=xnr %></td> </tr> <tr> <td colspan="2"><hr /> </td> </tr> </table> <body>
修改语法
<!-- form表单隐藏域传值 会随着表单的提交而提交 后期根据name值取value值-->
<input name="xid" type="hidden" value="<%=xid%>">使用修改语法之前要先用查询单个的方法用id得到把值再把值给赋进去
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//设置编码方式
request.setCharacterEncoding("utf-8");
//接收表单的值 根据name取value值
String xid=request.getParameter("xid");
String ntid=request.getParameter("ntid");
String ntitle=request.getParameter("ntitle");
String nauthor=request.getParameter("nauthor");
String nsummary=request.getParameter("nsummary");
String ncontent=request.getParameter("ncontent");
String naddtime=new Date().toLocaleString();//系统当前时间
//两个字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String cname="oracle.jdbc.driver.OracleDriver";
//加载驱动
Class.forName(cname);
//创建连接
Connection con=DriverManager.getConnection(url, "scott", "tiger");
//定义sql语句
String sql="update xw set xzt=?,xbt=?,xzz=?,xzy=?,xnr=?,xsj=? where xid=?";
//获得执行对象
PreparedStatement ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, Integer.parseInt(ntid));
ps.setString(2, ntitle);
ps.setString(3, nauthor);
ps.setString(4, nsummary);
ps.setString(5, ncontent);
ps.setString(6, naddtime);
ps.setInt(7, Integer.parseInt(xid));
//开始执行
int n=ps.executeUpdate();//影响行数
//int n=0;
//关闭资源
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
//判断
if(n>0){
//修改成功
response.sendRedirect("/javaWeb5/news/admin.jsp");
}else{
//修改失败
out.print("<script>alert('修改失败');location.href='update.jsp?xid="+xid+"';</script>");
}
%>