web数据库实验报告

1.     答:JDBC有四种驱动类型:

 

2.     使用jdbc的基本过程:

3.

选择对数据库进行操作方式的界面:

查询操作的界面:

添加操作的页面:

删除操作的界面:

各个部分的代码展示:

方式界面:

fanshi.jsp: <%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML>

<BODY>

<P>要进行的操作方式:

<Fontsize=4>

<FORMaction="zhuanhuan.jsp"method=postname=form>

       <INPUT type="radio" name="R" value="查询">查询操作

       <INPUT type="radio" name="R" value="删除">删除操作

       <INPUT type="radio" name="R" value="添加">添加操作

<BR>  <Input type=submit name="g" value="提交操作">

</Form>

<P>图书数据库删除记录前的数据记录是:

 <%

    String name,number,math,english;

    int physics;

    Connection con;

    Statement sql;

    ResultSet rs;

    try{Class.forName("com.mysql.jdbc.Driver");

       }

    catch(ClassNotFoundException e){}

    try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs?user=tanzhifei&password=tan123");

         sql=con.createStatement();

         rs=sql.executeQuery("SELECT * FROM books");

         out.print("<Table Border>");

         out.print("<TR>");

         out.print("<TH width=100>"+"书号");

         out.print("<TH width=100>"+"书名");

         out.print("<TH width=100>"+"出版社");

         out.print("<TH width=100>"+"作者");

         out.print("<TH width=120>"+"出版时间");

         out.print("</TR>");

       while(rs.next())

       { out.print("<TR>");

             number=rs.getString(1);

             out.print("<TD >"+number+"</TD>");

             name=rs.getString(2);

             out.print("<TD >"+name+"</TD>");

             math=rs.getString("chubanshe");

             out.print("<TD >"+math+"</TD>");

             english=rs.getString("zuozhe");

             out.print("<TD >"+english+"</TD>");

             physics=rs.getInt("date");

             out.print("<TD >"+physics+"</TD>");  

        out.print("</TR>") ;

        }

        out.print("</Table>");

        con.close();

     }

   catch(SQLException e1) {}

 %>

</BODY>

</HTML>

Zhuanhuan.jsp: <%@ page contentType="text/html;charset=GB2312" %>

<HTML>

<BODY>

  <% //获取提交的操作方式:

    String name=request.getParameter("R");

          if(name==null)

            {name="";}

     byte b[]=name.getBytes("ISO-8859-1");

     name=new String(b);

    String para="";

    if (name.equals("查询"))

        response.sendRedirect("chaxun.jsp");

    if (name.equals("删除"))

        response.sendRedirect("shanchu.jsp");

    if (name.equals("添加"))

        response.sendRedirect("tianjia.jsp");

 %>

</BODY>

</HTML>

查询界面:

chaxun.jsp:

<%@ page contentType="text/html;charset=GB2312"%>

<HTML>

    <BODY>

        <Fontsize=4>

            <FORMaction="byname.jsp" Method="post">

                <P>

                    书名查询

                    <P>

                        输入书名:

                        <Input type=text name="name">

                        <Input type=submit name="g" value="提交">

            </Form>

    </BODY>

</HTML>

byname.jsp:

<%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML>

<BODY>

  <% //获取提交的书名:

    String name=request.getParameter("name");

          if(name==null)

            {name="";

            }

     byte b[]=name.getBytes("ISO-8859-1");

     name=new String(b);

    Connection con=null;

    Statement sql=null;

    ResultSet rs=null;

       try{Class.forName("com.mysql.jdbc.Driver");

          }

       catch(ClassNotFoundException e){}

       try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs?user=tanzhifei&password=tan123");

             sql=con.createStatement();

             String condition=

             "SELECT * FROM books WHERE shuming = "+"'"+name+"'";

             rs=sql.executeQuery(condition);

         out.print("<Table Border>");

              out.print("<TR>");

              out.print("<TH width=100>"+"书号");

              out.print("<TH width=100>"+"书名");

              out.print("<TH width=100>"+"出版社");

              out.print("<TH width=100>"+"作者");

              out.print("<TH width=120>"+"出版时间");

              out.print("</TR>");

              while(rs.next())

                   { out.print("<TR>");

                     out.print("<TD >"+rs.getInt("shuhao")+"</TD>");

                     out.print("<TD >"+rs.getString("shuming")+"</TD>");

                     out.print("<TD >"+rs.getString("chubanshe")+"</TD>");

                     out.print("<TD >"+rs.getString("zuozhe")+"</TD>");

                     out.print("<TD >"+rs.getInt("date")+"</TD>");  

                     out.print("</TR>") ; 

                    }

              out.print("</Table>");

              con.close();

           }

    catch(SQLException e)

           {  }

%>

<p>单击此超链接,可到操作方式选择页面

<A HREF="fangshi.jsp">返回</A>

</BODY>

</HTML>

添加操作的界面:

tianjia.jsp:

<%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML>

<BODY  >

<Font size=4>

<P>添加新的记录到数据库:

<FORMaction="newDatabase.jsp"method=post>

图书书号:

<Input type="text" name="number">

<BR>图书书名:

<Input type="text" name="name">

<BR>出版社:

<Input type="text" name="chubanshe">

<BR>作者:

<Input type="text" name="zuozhe">

<BR>出版日期:

<Input type="text" name="date">

<BR><Input type="submit" name="b" value="提交添加">

<P>数据库添加记录前的数据记录是:

 <%

    String name,number,math,english;

    int physics;

    Connection con;

    Statement sql;

    ResultSet rs;

    try{Class.forName("com.mysql.jdbc.Driver");

       }

    catch(ClassNotFoundException e){}

    try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs?user=tanzhifei&password=tan123");

         sql=con.createStatement();

         rs=sql.executeQuery("SELECT * FROM books");

         out.print("<Table Border>");

         out.print("<TR>");

         out.print("<TH width=100>"+"书号");

         out.print("<TH width=100>"+"书名");

         out.print("<TH width=100>"+"出版社");

         out.print("<TH width=100>"+"作者");

         out.print("<TH width=120>"+"出版时间");

         out.print("</TR>");

       while(rs.next())

       { out.print("<TR>");

             number=rs.getString(1);

             out.print("<TD >"+number+"</TD>");

             name=rs.getString(2);

             out.print("<TD >"+name+"</TD>");

             math=rs.getString("chubanshe");

             out.print("<TD >"+math+"</TD>");

             english=rs.getString("zuozhe");

             out.print("<TD >"+english+"</TD>");

             physics=rs.getInt("date");

             out.print("<TD >"+physics+"</TD>");  

        out.print("</TR>") ;

        }

        out.print("</Table>");

        con.close();

     }

   catch(SQLException e1) {}

 %>

</Font>

<p>单击此超链接,可到操作方式选择页面

<A HREF="fangshi.jsp">返回</A>

</BODY>

</HTML>

newDatabase.jsp:

<%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML>

<BODY><Font size=4>

  <% //获取提交的书号:

    String number=request.getParameter("number");

          if(number==null)

            {number="";}

          byte b[]=number.getBytes("ISO-8859-1");

          number=new String(b);

    //获取提交的书名:

    String name=request.getParameter("name");

          if(name==null)

            {name=" ";

            }

     byte a[]=name.getBytes("ISO-8859-1");

     name=new String(a);

   

    String c=request.getParameter("chubanshe");

          if(c==null)

            {c=" ";

            }

     byte d[]=c.getBytes("ISO-8859-1");

     c=new String(d);

    String w=request.getParameter("zuozhe");

          if(w==null)

            {w=" ";

            }

     byte e[]=w.getBytes("ISO-8859-1");

     w=new String(e);

     String s=request.getParameter("date");

          if(s==null)

            {s="0"; }

    Connection con=null;

    Statement sql=null;

    ResultSet rs=null;

       try{Class.forName("com.mysql.jdbc.Driver"); }

       catch(ClassNotFoundException event){}

   try {con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs?user=tanzhifei&password=tan123");

       sql=con.createStatement();

       String condition= "INSERT INTO books VALUES"+"("+"'"+number+"','"+name+"','"+c+"','"+w+"',"+s+")";

       sql.executeUpdate(condition); //执行添加操作:

       //显示添加新记录后表中的记录:

     %>

     <P>添加新记录后的表:

     <%

     rs=sql.executeQuery("SELECT * FROM books ORDER BY shuhao ");

     out.print("<Table Border>");

          out.print("<TR>");

          out.print("<TH width=100>"+"书号");

          out.print("<TH width=100>"+"书名");

          out.print("<TH width=100>"+"出版社");

          out.print("<TH width=100>"+"作者");

          out.print("<TH width=120>"+"出版时间");

          out.print("</TR>");

     while(rs.next())

         {    out.print("<TR>");

              String n=rs.getString("shuhao");

               out.print("<TD >"+n+"</TD>");

              String shuming=rs.getString("shuming");

               out.print("<TD >"+shuming+"</TD>");

              String  math=rs.getString("chubanshe");

               out.print("<TD >"+math+"</TD>");

              String english=rs.getString("zuozhe");

               out.print("<TD >"+english+"</TD>");

              int physics=rs.getInt("date");

               out.print("<TD >"+physics+"</TD>");

          out.print("</TR>") ; 

       }

     out.print("</Table>");

    con.close();

   }

  catch(SQLException event)

        {out.print(""+event);

        }

%>

</FONT>

<p>单击此超链接,可到操作方式选择页面

<A HREF="fangshi.jsp">返回</A>

</BODY>

</HTML>

删除操作:

Shanchu.jsp: <%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML>

    <BODY>

        <Fontsize=4>

            <FORMaction="delete.jsp"Method="post">

                <P>

                    删除操作

                    <P>

                        输入要删除的书号:

                        <Input type=text name="number">

                        <Input type=submit name="g" value="提交">

            </Form>

<P>图书数据库删除记录前的数据记录是:

 <%

    String name,number,math,english;

    int physics;

    Connection con;

    Statement sql;

    ResultSet rs;

    try{Class.forName("com.mysql.jdbc.Driver");

       }

    catch(ClassNotFoundException e){}

    try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs?user=tanzhifei&password=tan123");

         sql=con.createStatement();

         rs=sql.executeQuery("SELECT * FROM books");

         out.print("<Table Border>");

         out.print("<TR>");

         out.print("<TH width=100>"+"书号");

         out.print("<TH width=100>"+"书名");

         out.print("<TH width=100>"+"出版社");

         out.print("<TH width=100>"+"作者");

         out.print("<TH width=120>"+"出版时间");

         out.print("</TR>");

       while(rs.next())

       { out.print("<TR>");

             number=rs.getString(1);

             out.print("<TD >"+number+"</TD>");

             name=rs.getString(2);

             out.print("<TD >"+name+"</TD>");

             math=rs.getString("chubanshe");

             out.print("<TD >"+math+"</TD>");

             english=rs.getString("zuozhe");

             out.print("<TD >"+english+"</TD>");

             physics=rs.getInt("date");

             out.print("<TD >"+physics+"</TD>");  

        out.print("</TR>") ;

        }

        out.print("</Table>");

        con.close();

     }

   catch(SQLException e1) {}

 %>

<p>单击此超链接,可到操作方式选择页面

<A HREF="fangshi.jsp">返回</A>

    </BODY>

</HTML>

delete.jsp:

<%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.sql.*" %>

<HTML>

<BODY><Font size=1>

  <% //获取提交的书号:

    String number=request.getParameter("number");

          if(number==null)

            {number="";

            }

     byte b[]=number.getBytes("ISO-8859-1");

     number=new String(b);

    Connection con=null;

    Statement sql=null;

    ResultSet rs=null;

       try{Class.forName("com.mysql.jdbc.Driver");

          }

       catch(ClassNotFoundException event){}

       try {con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pubs?user=tanzhifei&password=tan123");

       sql=con.createStatement();

       //删除操作:

       String deleteAll="DELETE  FROM books WHERE shuhao"+" = "+"'"+number+"'";

       sql.executeUpdate(deleteAll);

      %>

     <P>删除记录后的表:

     <%

       rs=sql.executeQuery("SELECT * FROM books ORDER BY shuhao ");

       out.print("<Table Border>");

            out.print("<TR>");

            out.print("<TH width=100>"+"书号");

            out.print("<TH width=100>"+"书名");

            out.print("<TH width=100>"+"出版社");

            out.print("<TH width=100>"+"作者");

            out.print("<TH width=120>"+"出版时间");

            out.print("</TR>");

       while(rs.next())

           {    out.print("<TR>");

                String n=rs.getString("shuhao");

                 out.print("<TD >"+n+"</TD>");

                String shuming=rs.getString("shuming");

                 out.print("<TD >"+shuming+"</TD>");

                String  math=rs.getString("chubanshe");

                 out.print("<TD >"+math+"</TD>");

                String english=rs.getString("zuozhe");

                 out.print("<TD >"+english+"</TD>");

                int physics=rs.getInt("date");

                 out.print("<TD >"+physics+"</TD>");

            out.print("</TR>") ; 

         }

       out.print("</Table>");

      con.close();

     }

    catch(SQLException event)

          {out.print(""+event);

          }

 %>

</FONT>

<p>单击此超链接,可到操作方式选择页面

<A HREF="fangshi.jsp">返回</A>

</BODY>

</HTML>