1个回答
展开全部
(1)获取文本框内容
(2)连接数据库
(3)查询数据库
(4)插入数据
(5)以表格形式输出数据库中的数据
<%@page contentType="text/html;charset=gbk" %>
<%@page import="java.sql.*" %>
<%
String sidbook=request.getParameter("idbook");// 获取文本框内容
String idbook=new String(sidbook.getBytes("iso-8859-1"));
String scategoryname=request.getParameter("categoryname");
String categoryname=new String(scategoryname.getBytes("iso-8859-1"));
String sbookname=request.getParameter("bookname");
String bookname=new String(sbookname.getBytes("iso-8859-1"));
String sauthor=request.getParameter("author");
String author=new String(sauthor.getBytes("iso-8859-1"));
String ssuppliername=request.getParameter("suppliername");
String suppliername=new String(ssuppliername.getBytes("iso-8859-1"));
String soutday=request.getParameter("outday");
String outday=new String(soutday.getBytes("iso-8859-1"));
String sprice=request.getParameter("price");
String price=new String(sprice.getBytes("iso-8859-1"));
String sstock=request.getParameter("stock");
int stock=Integer.parseInt(sstock); //将字符转换为数字
String sfabu=request.getParameter("selectfabu");
String fabu=new String(sfabu.getBytes("iso-8859-1"));
String shotdeal=request.getParameter("selecthotdeal");
String hotdeal=new String(shotdeal.getBytes("iso-8859-1"));
String ssale=request.getParameter("sale");
String sale=new String(ssale.getBytes("iso-8859-1"));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:booksystem");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("select * from books where idbook='"+idbook+"'"); //按照idbook查询数据库
if(rs.next())
out.print("此书(图书编号:'"+idbook+"')已存在于数据库中,插入失败 !"+"<p>");
else
{
sql.executeUpdate("insert into books values('"+idbook+"','"+categoryname+"','"+bookname+"','"+author+"','"+suppliername+"','"+outday+"','"+price+"','"+stock+"','"+fabu+"','"+hotdeal+"','"+sale+"') "); //插入数据到数据库中
out.print("<strong>已将图书(图书编号:'"+idbook+"')录入数据库!</strong>"+"<p>");
}
rs=sql.executeQuery("select * from books ");
out.print("数据库中的书目如下: ");
out.print("<table border>");
out.print("<tr>");
out.print("<th width=72>图书编号</td>");
out.print("<th width=72>图书类别</td>");
out.print("<th width=50>图书名称</td>");
out.print("<th width=50>作者</td>");
out.print("<th width=62>出 版 商</td>");
out.print("<th width=50>出版日期</td>");
out.print("<th width=50>售价</td>");
out.print("<th width=50>库存(本)</td>");
out.print("<th width=50>是否发布</td>");
out.print("<th width=50>是否热点书</td>");
out.print("<th width=50>出售(本)</td>");
out.print("</tr>");
while(rs.next())
{
String a,b,c,d,e,f,g,h,i,j,k; //这里将数据以表格形式输出
out.print("<tr>");
a=rs.getString(1);
out.print("<td>"+a+"</td>");
b=rs.getString(2);
out.print("<td>"+b+"</td>");
c=rs.getString(3);
out.print("<td>"+c+"</td>");
d=rs.getString(4);
out.print("<td>"+d+"</td>");
e=rs.getString(5);
out.print("<td>"+e+"</td>");
f=rs.getString(6);
out.print("<td>"+f+"</td>");
g=rs.getString(7);
out.print("<td>"+g+"</td>");
h=rs.getString(8);
out.print("<td>"+h+"</td>");
i=rs.getString(9);
out.print("<td>"+i+"</td>");
j=rs.getString(10);
out.print("<td>"+j+"</td>");
k=rs.getString(11);
out.print("<td>"+k+"</td>");
out.print("</tr>");
}
con.close();
%>
(2)连接数据库
(3)查询数据库
(4)插入数据
(5)以表格形式输出数据库中的数据
<%@page contentType="text/html;charset=gbk" %>
<%@page import="java.sql.*" %>
<%
String sidbook=request.getParameter("idbook");// 获取文本框内容
String idbook=new String(sidbook.getBytes("iso-8859-1"));
String scategoryname=request.getParameter("categoryname");
String categoryname=new String(scategoryname.getBytes("iso-8859-1"));
String sbookname=request.getParameter("bookname");
String bookname=new String(sbookname.getBytes("iso-8859-1"));
String sauthor=request.getParameter("author");
String author=new String(sauthor.getBytes("iso-8859-1"));
String ssuppliername=request.getParameter("suppliername");
String suppliername=new String(ssuppliername.getBytes("iso-8859-1"));
String soutday=request.getParameter("outday");
String outday=new String(soutday.getBytes("iso-8859-1"));
String sprice=request.getParameter("price");
String price=new String(sprice.getBytes("iso-8859-1"));
String sstock=request.getParameter("stock");
int stock=Integer.parseInt(sstock); //将字符转换为数字
String sfabu=request.getParameter("selectfabu");
String fabu=new String(sfabu.getBytes("iso-8859-1"));
String shotdeal=request.getParameter("selecthotdeal");
String hotdeal=new String(shotdeal.getBytes("iso-8859-1"));
String ssale=request.getParameter("sale");
String sale=new String(ssale.getBytes("iso-8859-1"));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:booksystem");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("select * from books where idbook='"+idbook+"'"); //按照idbook查询数据库
if(rs.next())
out.print("此书(图书编号:'"+idbook+"')已存在于数据库中,插入失败 !"+"<p>");
else
{
sql.executeUpdate("insert into books values('"+idbook+"','"+categoryname+"','"+bookname+"','"+author+"','"+suppliername+"','"+outday+"','"+price+"','"+stock+"','"+fabu+"','"+hotdeal+"','"+sale+"') "); //插入数据到数据库中
out.print("<strong>已将图书(图书编号:'"+idbook+"')录入数据库!</strong>"+"<p>");
}
rs=sql.executeQuery("select * from books ");
out.print("数据库中的书目如下: ");
out.print("<table border>");
out.print("<tr>");
out.print("<th width=72>图书编号</td>");
out.print("<th width=72>图书类别</td>");
out.print("<th width=50>图书名称</td>");
out.print("<th width=50>作者</td>");
out.print("<th width=62>出 版 商</td>");
out.print("<th width=50>出版日期</td>");
out.print("<th width=50>售价</td>");
out.print("<th width=50>库存(本)</td>");
out.print("<th width=50>是否发布</td>");
out.print("<th width=50>是否热点书</td>");
out.print("<th width=50>出售(本)</td>");
out.print("</tr>");
while(rs.next())
{
String a,b,c,d,e,f,g,h,i,j,k; //这里将数据以表格形式输出
out.print("<tr>");
a=rs.getString(1);
out.print("<td>"+a+"</td>");
b=rs.getString(2);
out.print("<td>"+b+"</td>");
c=rs.getString(3);
out.print("<td>"+c+"</td>");
d=rs.getString(4);
out.print("<td>"+d+"</td>");
e=rs.getString(5);
out.print("<td>"+e+"</td>");
f=rs.getString(6);
out.print("<td>"+f+"</td>");
g=rs.getString(7);
out.print("<td>"+g+"</td>");
h=rs.getString(8);
out.print("<td>"+h+"</td>");
i=rs.getString(9);
out.print("<td>"+i+"</td>");
j=rs.getString(10);
out.print("<td>"+j+"</td>");
k=rs.getString(11);
out.print("<td>"+k+"</td>");
out.print("</tr>");
}
con.close();
%>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询