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