Dynamic generation page (Dynamically Retrieve data information from the database), Database Dynamic

Source: Internet
Author: User

Dynamic generation page (Dynamically Retrieve data information from the database), Database Dynamic

The course design is a little annoying, so I simply wrote the following code to dynamically display the products added to the shopping cart to the front-end page.

The first is my head. jsp. The "My directory" here uses a servlet name named "selectUserCart" instead of a direct link.

The user, books, and userAccout classes used in my project basically record the basic information and implement the setXxx () and getXxx () methods.

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 
Then there are two classes I have used:
Search for the tool class of my shopping cart: myCartUtil. java
<Pre name = "code" class = "java"> package myCart; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. arrayList; import bookConnUtil. DBUtil; import bookConnUtil. books; public class myCartUtil {public static ArrayList <mycart> list = new ArrayList <mycart> (); public static ArrayList <books> booklist = new ArrayList <books> (); public Static String msg; public static int countbook = 0; // public static mycart [] cart = new mycart [10]; public static int count = 0; public static boolean insertIntoMyCart (String username, String bookname, String pianduanName) throws SQLException {String cartInsert = "insert into userAccount valus (?,?,?) "; Boolean flag = false; Connection conn = null; PreparedStatement stmt = null; try {conn = DBUtil. getConnection (DBUtil. CONNECTION_ SQL); stmt = conn. prepareStatement (cartInsert); stmt. setString (1, username); stmt. setString (2, bookname); stmt. setString (3, pianduanName); int count = stmt.exe cuteUpdate (); if (count> 0) {flag = true;} else {flag = false;} catch (Exception e) {System. out. println ("sorry, your program failed"); e. printStackTrac E ();} finally {conn. close (); stmt. close ();} return flag;} public static boolean selectUserCart (String userName) throws SQLException {boolean flag = false; String cartSelect = "select username, bookname, pianduanName, imagePath, price from userAccount where username =? "; Connection conn = null; ResultSet rs = null; PreparedStatement stmt = null; try {conn = DBUtil. getConnection (DBUtil. CONNECTION_ SQL); stmt = conn. prepareStatement (cartSelect); stmt. setString (1, userName); rs = stmt.exe cuteQuery (); while (rs. next () {mycart cart = new mycart (); cart. setUserName (rs. getString (1); cart. setBookName (rs. getString (2); cart. setPianduanName (rs. getString (3); cart. setImagePath (rs. getString (4); cart. setPrice (rs. getInt (5); list. add (cart); count ++;} System. out. println ("++" + count + "++ "); if (count = 0) {msg = "null";} else {msg = "full";} flag = true;} catch (Exception e) {System. out. println ("sorry, your program failed"); e. printStackTrace ();} finally {conn. close (); stmt. close ();} return flag;} public static boolean selectAllBooks () throws SQLException {boolean flag = false; String cartSelect = "select bookId, bookName, pianduanName, imagesPath, price from books "; Connection conn = null; ResultSet rs = null; PreparedStatement stmt = null; try {conn = DBUtil. getConnection (DBUtil. CONNECTION_ SQL); stmt = conn. prepareStatement (cartSelect); rs = stmt.exe cuteQuery (); while (rs. next () {books book = new books (); book. setBookID (rs. getString (1); book. setBookName (rs. getString (2); book. setPianduanName (rs. getString (3); book. setImagePath (rs. getString (4); book. setPrice (rs. getInt (5); booklist. add (book );
<Span style = "white-space: pre "> </span> // I am not doing well, so I want to use this mark to do better. Please implement countbook ++ on your own;} System. out. println ("++" + count + "++ "); if (count = 0) {msg = "null";} else {msg = "full";} flag = true;} catch (Exception e) {System. out. println ("sorry, your program failed"); e. printStackTrace ();} finally {conn. close (); stmt. close () ;}return flag ;}}

The following is the database connection class:


<Pre name = "code" class = "java"> package bookConnUtil; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; public class DBUtil {public static final int CONNECTION_MYSQL = 2; public static final int CONNECTION_ SQL = 1; public static final int CONNECTION_ODBC = 0; public DBUtil () {} public static Connection getConnection (int connection_type) throws Exception {switch (connection_type) {Case CONNECTION_ODBC: return getConnectionODBC (); case CONNECTION_ SQL: return getConnectionSQL (); case CONNECTION_MYSQL: return getConnectionMYSQL ();} return null ;} /***** ������������/private static Connection getConnectionODBC () throws Exception {Connection conn = null; try {Class. forName ("sun. jdbc. odbc. jdbcOdbcDriver "); System. out. println ("abc"); conn = DriverManager. getConnection ("jdbc: odbc: mybook", "sa", "123" ); System. out. println ("connection successful");} catch (Exception e) {e. printStackTrace (); throw e;} return conn;} private static Connection getConnectionSQL () throws Exception {Connection conn = null; String connectionUrl = "jdbc: sqlserver: // localhost: 1433; databaseName = user; "; try {Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver "); conn = DriverManager. getConnection (connectionUrl, "sa", "123"); System. out. println ("connection successful" );} Catch (Exception e) {e. printStackTrace (); throw e;} return conn;} private static Connection getConnectionMYSQL () throws Exception {String connectionUrl = "jdbc: mysql: // localhost: 3306/user "; string username = "root"; String password = "mysql"; Connection conn = null; try {Class. forName ("com. mysql. jdbc. driver "); conn = DriverManager. getConnection (connectionUrl, username, password); System. out. println ("connection successful ");} Catch (Exception e) {e. printStackTrace (); throw e;} return conn;} public static void close (Connection conn) throws Exception {if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {e. printStackTrace (); throw e ;}}}}

The following shows the user shopping cart search class:

<Pre name = "code" class = "java"> package myCart; import java. io. IOException; import java. SQL. SQLException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import bookConnUtil. bookDAO; public class selectUserCart extends HttpServlet {/*****/private static final long serialVersionUID = 1L; public int count = myCartUtil. count; protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubHttpSession session = req. getSession (); String username = (String) session. getAttribute ("username"); try {// System. out. println ("the query process has not been executed here"); boolean flag = myCartUtil. selectUserCart (username); if (flag = true) {/* for (mycart message: myCartUtil. list) {for (int I = 0; I <3; I ++) {String A = message. getUserName () ;}} * // session. setAttribute ("count", countNum); if (myCartUtil. msg. equals ("null") {session. setAttribute ("null", myCartUtil. msg);} else {session. setAttribute ("full", myCartUtil. msg);} req. getRequestDispatcher ("selectMyAcccount. jsp "). forward (req, resp);} else {req. getRequestDispatcher ("error.html "). forward (req, resp) ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

My directory implementation code:

<Pre name = "code" class = "html"> <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.