JDBC enables simple additions and deletions

Source: Internet
Author: User

First book class.

Slightly

Then a homepage, write a form, submit the book information to Addbook.

Slightly

Addbook.jsp connects to JDBC and inserts it into the Book table.

<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%@ Page Import="java.sql.Connection"%>    <%@ Page Import="Java.sql.DriverManager"%>   <%@ Page Import="java.sql.PreparedStatement"%>   <%@ Page Import="Java.sql.ResultSet"%>   <%@ Page Import="java.sql.Statement"%>  <%@ Page Import="java.sql.*"%> <%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>  <Head>    <Basehref= "<%=basePath%>">        <title>Uuuu</title>    <Metahttp-equiv= "Pragma"content= "No-cache">    <Metahttp-equiv= "Cache-control"content= "No-cache">    <Metahttp-equiv= "Expires"content= "0">        <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3">    <Metahttp-equiv= "description"content= "This is my page">  </Head>    <Body>          <%request.setcharacterencoding ("UTF-8"); %>        <Jsp:usebeanID= "book"class= "book" ></Jsp:usebean>        <Jsp:setproperty Property=""name= "book"/>  <%try{Class.forName ("Oracle.jdbc.driver.OracleDriver"); //NewOracle.jdbc.driver.OracleDriver (); Connection Conn=Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:xe", "Yu", "123"); StringSQL= "INSERT into book (Id,name,price,book_count,author) VALUES (?,?,?,?,?)"; PreparedStatement PS=conn.preparestatement (SQL); Ps.setint (1, Book.getid ()); Ps.setstring (2, Book.getname ()); Ps.setdouble (3, Book.getprice ()); Ps.setint (4, Book.getbookcount ()); Ps.setstring (5, Book.getauthor ()); intRow=ps.executeupdate (); if(Row> 0) {Out.print ("it worked." +Row+ "Bar Data");            } ps.close ();    Conn.close ();   }catch (Exception e) {e.printstacktrace (); }              %> <BR> <ahref= "index.jsp">Return</a>  </Body></HTML>

The idea is so simple.

The query needs to use resultset.

Modify the SQL statement that requires only update.

Delete the SQL statement that simply deletes.

JDBC enables simple additions and deletions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.