📄 addgood.jsp
字号:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="shop.logBean"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%!
public String codeString(String s){
String str=s;
try{
byte b[]=str.getBytes("ISO-8859-1");
str=new String (b);
return str;
}catch(Exception e){return str;}
}
%>
<%
if(session.isNew()){
response.sendRedirect("index.jsp");
}
else {
%>
<jsp:useBean id="log" class="shop.logBean" scope="session"/>
<%
String islog=log.getIslog();
String userid=log.getId();
if(!islog.equals("true")||!userid.equals("admini")){
//out.println("admini!");
response.sendRedirect("index.jsp");
}
else{
String id=request.getParameter("id");
String type=request.getParameter("type");
String name=request.getParameter("name");
String price=request.getParameter("price");
String amount=request.getParameter("amount");
String distr=request.getParameter("distr");
String product=request.getParameter("product");
if(id==null)id="";
if(type==null)type="";
if(name==null)name="";
if(price==null)price="";
if(amount==null)amount="";
if(distr==null)distr="无信息";
if(product==null)product="无信息";
id=codeString(id);
type=codeString(type);
name=codeString(name);
price=codeString(price);
amount=codeString(amount);
distr=codeString(distr);
product=codeString(product);
if(id.equals("")||
type.equals("")||
name.equals("")||
price.equals("")||
amount.equals("")){
//out.println("输入不能为空!");
response.sendRedirect("admini.jsp");
}
else{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:myshop","","");
Statement sql = con.createStatement();
String condition="select * from goods where name='"+name+"'";
ResultSet rs=sql.executeQuery(condition);
if(rs.next()){
if((rs.getString("id")).equals(id)){
int oldamount=(Integer.valueOf(rs.getString("amount"))).intValue();
oldamount+=(Integer.valueOf(amount)).intValue();
PreparedStatement pstmt=con.prepareStatement("update goods set amount=?,price=? where name=?;");
pstmt.setString(1,String.valueOf(oldamount));
pstmt.setString(2,price);
pstmt.setString(3,name);
pstmt.executeUpdate();
pstmt.close();
con.close();
response.sendRedirect("admini.jsp");
}else{
con.close();
response.sendRedirect("admini.jsp");
}
}
condition="select * from goods where id='"+id+"'";
rs=sql.executeQuery(condition);
if(rs.next()){
if((rs.getString("name")).equals(name)){
int oldamount=(Integer.valueOf(rs.getString("amount"))).intValue();
oldamount+=(Integer.valueOf(amount)).intValue();
PreparedStatement pstmt=con.prepareStatement("update goods set amount=?,price=? where name=?;");
pstmt.setString(1,String.valueOf(oldamount));
pstmt.setString(2,price);
pstmt.setString(3,name);
pstmt.executeUpdate();
pstmt.close();
con.close();
response.sendRedirect("admini.jsp");
}else{
con.close();
response.sendRedirect("admini.jsp");
}
}
PreparedStatement pstmt=con.prepareStatement("insert into goods values (?,?,?,?,?,?,?);");
pstmt.setString(1,id);
pstmt.setString(2,type);
pstmt.setString(3,name);
pstmt.setString(4,price);
pstmt.setString(5,amount);
pstmt.setString(6,distr);
pstmt.setString(7,product);
pstmt.executeUpdate();
pstmt.close();
con.close();
//out.println("ok");
response.sendRedirect("admini.jsp");
}catch(SQLException e){
out.println("sql error!");
//response.sendRedirect("index.jsp");
}catch(ClassNotFoundException e){
response.sendRedirect("index.jsp");
}
}
}
}
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -