Jsp+servlet+mysql Building Routines

Source: Internet
Author: User
Tags stmt

1. Establish a database
Build a database based on your needs
Establish database fields, properties, primary keys, etc.
2, establish the Javaweb project, build the development environment
/webroot/web-inf in the development environment
Set up a Lib folder to store the jar packages you need to use
Commonly used packages:
Mysqldriver.jar
Beanutil.jar
Commons-logging.jar
Jstl.jar
Standard.jar
3, the Java internal writing, the establishment domain package
Under which the Bean class is written, the field of the class corresponds to the database
4. Define DAO and its interface
(1) interface DAO Package: Abstract Daoimlents Package (in order to increase the class extensibility of DAO)
Increase Customer
The data in the database is manipulated to add or delete, adding SQL statements

Template:
Connection Conn =null;
Prestatement stmt = null;
ResultSet rs = null;
if (c==null)
parameter is not the exception see also Nullexception, is the argument is empty time
throw new Illegaexception ();
try{
Conn=jutil.getconnection ();
Stmt=conn.preparestatement ("SQL statement");
Stmt.setstring (1,c.getname ());
.
.
.
.
Stmt.getexcuteupdate ();
}catch (Exception e) {
throw new Daoexception (e);
}finally{
Jtuil.release (*,*,*); * Parameter name
}

void Addcustomer (Customer c);
Delete customer information by ID. Name embodies the function to be realized
void Delcustomerbyid (String customerId);
Change the customer's information idisnullexception if the parameter ID is null, an exception is thrown
Create exception package, name Idisnullexception class inheritance exception
void UpdateCustomer (Customer c) throw idisnullexception;
Querying all user Information
List<customer> FindAll ();
Query customer information based on customer ID (a single customer)
Customer Findcustomerbyid (String customerId);

Note
Query methods can also be extended, for example, according to the user's other hobbies, addresses, and other information to find users
(2) daoimlents package to DAO concrete implementation, is the realization of the function in this writing,
Four ways to delete and change the search
Daoimlents implements dao{}
5. Establish a Jutil package with database connection
Which write Jutil class
Two methods:
Writing data Links
To increase the extensibility generally, string strings in the code are written in the properties configuration file, Class.forName ("Com.mysql.jdbc.Driver");
Build the properties text, and then write
Classname=com.mysql.jdbc.driver
Url=jdbc:mysql://localhost:3306/databasename
User=root
password=123456
Create a method to read this text
Inputsteam in= Jutil.class.getClassLoader.getReSourseAsStream ("propertiesname.properties");
Properties Pro = new properties ();
Pro.load (in);
Url=pro.getproperties ("url");
Password=pro.getproperties ("password");
User=pro.getproperties ("user");
Classname=pro.getproperties ("ClassName");
public static Connection getconnection () {}
Write Resource Release
public static void Relase (ResultSet st,statement st,connection conn) {}
6. Writing template Location
7. Testing
Java.lang.NoClassDefFoundError:Could not initialize class com. Jutil.jutil
At Com.daoImpl.CustomerDaoImpl.addCustomer (customerdaoimpl.java:44)
At Com.test.CustomerDaoImplTest.testAddCustomer (customerdaoimpltest.java:30)
Database name differs from JavaBean name, configuration file name is different, the jar package is not changed to BuildPath

8, Business layer Construction
Establishing the Service Interface
Writing service implementations
Because the business layer calls the DAO layer
Private Customerdao dao= new Customerdaoimpl ();

9. servlet and JSP writing
Open Web. XML to set the servlet as the home page
The servlet calls the service so it needs
Private Service s = new Serviceimpl ();
Servlet establishes a connection to the interface that displays all information
List<customer> cs = S.findall ();
Request.setattribute ("CS", CS);
Request.getrequestdispathcher ("/listshow.jsp"). ForWord (Request,response);

Jsp+servlet+mysql Build Routines

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.