JDBC database CRUD integrated application instance

Source: Internet
Author: User
Tags driver manager
A comprehensive example is provided to enhance the application of adding, deleting, modifying, and querying JDBC database operations. CREATETABLE 'tb _ Book' ('id' int (10) unsignedNOTNULLAUTO_ I

A comprehensive example is provided to enhance the application of adding, deleting, modifying, and querying JDBC database operations. CREATETABLE 'tb _ Book' ('id' int (10) unsignedNOTNULLAUTO_ I

A comprehensive example is provided to enhance the application of adding, deleting, modifying, and querying JDBC database operations.

Classic Library Information Entry Example


Design Database

Create table 'tb _ Book' ('id' int (10) unsigned not null AUTO_INCREMENT, 'name' varchar (45) not null, 'price' double not null, 'bookcount' int (10) unsigned not null, 'author' varchar (45) not null, primary key ('id '))



Write a Book class to encapsulate the Book information

Package com. lixiyu; public class Book {private int id; private String name; private double price; private int bookCount; private String author; public int getId () {return id ;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public double getPrice () {return price;} public void setPrice (double price) {this. price = price;} public int getbookCount () {return bookCount;} public void setbookCount (int bookCount) {this. bookCount = bookCount;} public String getAuthor () {return author;} public void setAuthor (String author) {this. author = author ;}}




Add (insert) Books


Create an AddBook. jsp page to process the added book information

<% @ Page language = "java" contentType = "text/html; charset = GB18030" pageEncoding = "GB18030" %> <% @ page import = "java. SQL. connection "%> <% @ page import =" java. SQL. driverManager "%> <% @ page import =" java. SQL. preparedStatement "%> Insert title here<% Request. setCharacterEncoding ("UTF-8"); %> <% Try {Class. forName ("com. mysql. jdbc. driver "); // load the database Driver and register it with the Driver manager String url =" jdbc: mysql: // localhost: 3306/db_test "; // database Connection String username = "root"; // database username String password = "lixiyu"; // Database password Connection conn = DriverManager. getConnection (url, username, password); // create a Connection String SQL = "insert into tb_books (name, price, bookCount, author) values (?,?,?,?) "; // Add the SQL statement PreparedStatement ps = conn. prepareStatement (SQL); // get PreparedStatementps. setString (1, book. getName (); // assign values to the 1st parameters in the SQL statement. setDouble (2, book. getPrice (); ps. setInt (3, book. getbookCount (); ps. setString (4, book. getAuthor (); // assign int rowdeskps.exe cuteUpdate () to the 4th parameters in the SQL statement; // execute the update operation and return the affected rows if (row> 0) {out. print ("+ row +" data entries "added successfully);} ps. close (); conn. close ();} catch (Exception e) {out. print ("adding book information failed! "); E. printStackTrace () ;}%>


Create insert. jsp to create the form required to add the library information

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> Add book information

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.