Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of address book items

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

/ * *

* @ author Mr.Deng

* @ since 2016-5-14

* @ version 1.0

* use notepad to open it for easy reading

* you are welcome to correct your mistakes and write down your opinions

, /

Project description: users can log in to manage their own address book information.

The project depends on software: mysql eclipse.

Project dependent technology: javaSE, JDBC, swing components

1. Architecture: 3-tier architecture

1. Display layer: 1. Users can only operate on themselves and their address books (private permissions)

two。 Use the java GUI image interface:

1. Log in to the registration main page interface

two。 Registration page

3. Function page: layout according to the upper, middle and lower layers:

1. Above: welcome window

two。 Medium: show the details of all the user's address book information

3. Below: the button for the user to operate on the address book

two。 Business layer: 1. The background staff operates on all users (administrator privileges)

two。 Writing daemons with java: writing interfaces and implementing classes implementation classes implement functions by calling interfaces written in the persistence layer

3. Persistence layer: 1.java programmers implement various operations by writing source code (Creator)

two。 Use JDBC connection database to save data

3. Use the configuration file to connect to JDBC if the database is modified. You only need to modify the configuration file, not the source code.

Specific implementation: each layer of the 3-tier architecture is connected through interfaces (dependencies)

1. Display layer-business layer: 1. API: UsersService,ContactsService

two。 Implementation class: UsersServiceImpl,ContactsServiceImpl

Function: the data or operations obtained by the presentation layer are transferred to the business layer

Business layer process: 1. Call the interface of the persistence layer

two。 Create an interface object

3. Call the method on the interface object for data operation

Principle: 1. The interface only defines the method body, has no implementation details, and cannot create objects directly.

The 2.ContactDaoImpl class is an implementation class that overrides all the methods of the interface and has implementation details

3. Instantiate the interface by creating an implementation class object. Interface can call all the methods of the implementation class to operate

ContactDao ctd = new ContactDaoImpl ()

two。 Business layer-persistence layer: 1. API: UsersDao

two。 Implementation class: UsersDaoImpl

Function: perform various operations on the data and put it into the database

Principle: 1. The address book and user object exist with the method call, and the method call ends the object disappearing.

two。 The object exists in memory, and the properties on the object can be saved by connecting to the database through JDBC.

3. Persistence layer-presentation layer: all functions of the persistence layer depend on the data obtained by the presentation layer

The existence of a data persistence layer is meaningless.

There is no functional implementation of the persistence layer, and the presentation layer does not make any sense.

two。 Package naming convention:

1. Persistence layer: 1.com.lovo.contacts.bean: entity package stores all entity objects. Users,contactsdetail follows javabean specification and implements serialization interface Serializable.

2.com.lovo.contacts.dao: the interface package stores all interfaces of the persistence layer

3.com.lovo.contacts.dao.impl: the implementation class holds all classes that implement the interface of the dao package

4.com.lovo.contacts.util: the toolkit holds all the tools needed by the persistence layer code, such as files, DBUtil

two。 Business layer: 1.com.lovo.contacts.service: business layer package stores all business layer interfaces UsersService,ContactsService

2.com.lovo.contacts.service.impl: the implementation class package holds all the implementation classes that implement the business layer package interface

3. Display layer: com.lovo.contacts.gui: graphics boundary bread stores all interface classes

4.javabean specification: 1. Common class

two。 Property privatization

3. Provide public get,set methods

4. Construction method without parameters

5. Implement the serialization interface Serializable

Summary: 1. Naming standardization ensures the understandability of the project and facilitates management. The project is written by multiple programmers with a good naming convention and structure to facilitate changes to achieve synchronous writing.

two。 Each project has its own naming convention, which is roughly the same.

3. The code flow of each method block: 1. Get database connection: Connection con = DBUtil.getconnection

two。 Write SQL statements that use question marks to prevent SQL injection

3. Create a precompiled processing object: PreparedStatement pstmt = con.prepardStatement (sql)

4. Change the value of the question mark

5. Execute the sql statement:

1. Increase: 1. (sql, PreparedStatement.RETURN_GENERATED_KEYS) guarantee the self-growth of the primary key

two。 Get the value on the object and set it to the corresponding field in the database

3. Get the result set of the database and judge that the ID of the first object is set to 1, and multiple objects are self-growing.

4. Close the connection

two。 Check: 1. Query the values of all fields into the result set, conditional id=? And status=1 1 indicates existence

two。 Create a new contactdetail object

3. Set the corresponding value of the result set to the contactdetail object

4. Close the connection

3. Delete: the 1.status field indicates the status of the table. According to the query statement, users can query this table only if there is a status=1.

two。 This table cannot be queried by setting up status=0 users. Logical deletion: the data still exists, but the user cannot query it. Physical deletion: data disappears from the database

3. Update the database

4. Close the connection

4. Change: 1. The user passes in the contactdetail object, and the values of some fields are modified

two。 Get the value on the contactdetail object

3. Overwrite the value of each field in the contactdetail field in the database

4. Close the connection

Extend:

1. Custom exception: 1. Define the properties and methods that an exception class inherits from Exception

two。 Provide construction methods with and without parameters

Public NameNotFoundException () {}

Public NameNotFoundException (String message) {super (message);}

3. Judgment statement if the user name is incorrect throw new NameNotFoundException ("user name does not exist")

2.try {} finally {} function: to ensure that the contents of the finally block can be executed whenever an exception occurs in the try block

Summary: 1. After making changes to the data in the database, it needs to be updated. Query is not required

two。 First analyze the function and sort out the train of thought, and then write the process notes. Finally, the code is populated according to the process comments.

3. According to the return type, the parameter list. Write code flexibly and remain the same.

4. Time conversion:

1.util.date---sql.date:

1. Get the number of milliseconds of time on the Contact object, if you don't get the milliseconds of system time, ternary operator: condition? Expression 1 (true): expression 2 (false)

Long time = detail.getCreateTime () = = null? System.currentTimeMillis (): detail.getCreateTime () .getTime ()

two。 Call the parameterized constructor of the sql.date class to pass the time milliseconds into the time to get the sql packet

New Date (time))

2.sql.date---util.date:

1. Gets the number of milliseconds of sql.date and passes it as an argument to the constructor class of util.date

New java.util.Date (rs.getDate ("d.createtime") .getTime ())

5. Attribute object: there is a 1-to-many relationship between 1.sql user table and detail table. A user can have more than one address book.

two。 Put a user object in the contactdetail class to connect the user class with the contactdetail class

User.getid () = contactdetail.getuser () .getid ()

Implement the 1-to-many relationship between java user classes and contactdetail classes

Sql.userid----contact.user

1. Create a user object

two。 Set userid to the user object

3. The parameter of the set method for analyzing user is the user object, and the created object is passed into the set method of user

6. Two-dimensional array: define the method getAllContact in the business layer return type: Object [] [] Parameter: useid

1. Get a collection of contactdetail objects

List list = cd.queryContactListByUser (userId)

two。 Defines the length of the array. The first field represents the number of address book entities, and the second field represents the number of address book information.

Object [] [] datas = new Object [list.size ()] [8]

3. The circular array loops the first parenthesis + 1 at a time, and the second parenthesis is put into the address book information one by one.

For (int I = 0; I < list.size (); iTunes +) {

ContactDetail detail = list.get (I)

Datas [I] [0] = detail.getId ()

Datas [I] [1] = detail.getName ()

. }

4. Return to datas

Eclipse: import Project: file---import---Existing Projects into Workspace--- Select Project path-finish

Import picture: project name-src---general---file system--- file path-Select picture-finish--- move the picture to the lib package

Generate document: right click on the project-export---java---javadoc--- Select the build path-finish

Java rack package: right-click the project-export---java---jar file select the build path-rack package name-finish

Import rack package: copy the rack package to the project-build path

Interface plug-ins use: class right-click-open with---windowbuilder editor--- lower left corner Design button

Attachment: http://down.51cto.com/data/2367904

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report