A review of yesterday's knowledge
- Package COM.JDBC;
- Import java.sql.Connection;
- Import Java.sql.DriverManager;
- Import Java.sql.ResultSet;
- Import java.sql.Statement;
- Public class Firstjdbc {
- Public static void Main (string[] args)
- {
- Invoke operation to connect to the database
- Connection con = createconnection ();
- }
- /**
- * JDBC establishes a SQL Server database connection
- */
- Private static Connection createconnection () {
- Defining Load Drivers
- String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
- Define connection server and Database sample
- String Dburl = "jdbc:sqlserver://localhost:1433; DataBaseName = Sample1 ";
- Default user name, do not use Windows default authentication
- String userName = "sa";
- String UserPassword = "Zhichao";
- Connection Connection = null;
- Statement STA = null;
- try {
- Official Load Driver
- Class.forName (drivername);
- Start connection
- Connection = Drivermanager.getconnection (Dburl, UserName, UserPassword);
- System.out.println ("Connection Success!");
- Execute SQL statements to the database
- STA = Connection.createstatement ();
- ResultSet rs = sta.executequery ("Select Id,name,height from Table_1");
- while (Rs.next ())
- {
- int id = rs.getint ("id");
- String name = rs.getstring ("name");
- float height = rs.getfloat ("height");
- SYSTEM.OUT.PRINTLN ("id =" +id+"name =" +name+"height =" +height ");
- }
- } catch (Exception e) {
- System.out.println ("Connection Fail!");
- E.printstacktrace ();
- }
- /**
- * Close Database
- * @param connection
- */
- Finally
- {
- try {
- if (null! = STA)
- {
- Sta.close ();
- STA = null;
- System.out.println ("Statement closed successfully");
- }
- if (null! = connection)
- {
- Connection.close ();
- connection = null;
- System.out.println ("Connection closed successfully");
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- return connection;
- }
- }
Summary:
To write a JDBC program, first load the corresponding database driver, the driver is best placed in the project you built, you can build a Lib folder under your project to store the external jar files , so that your project copy to another computer to run, Can still execute successfully.
JDBC Code general steps:
1) Load external driver (jar package)
2) load driver officially (Class.forName (drivername))
3) Get the connection connection (in the SQL package in the JDK, only one class is provided that is Drivermaneger, by calling its static method getconnection (), you can get a connection to the database
4) Create a declaration of the SQL statement (Statement), execute the SQL statement (query), traverse the result set
5) Close the database connection (generally with finally{} to handle, or call the form of the method to complete, before closing to determine whether you want to close the object connection is empty, if empty that will throw an exception, so first judge)
Today's study is the DAO design pattern realizes the data deletion and modification (further encapsulates the JDBC tool class)
I. Introduction to the DAO pattern
The DAO is the data Access object, which is the datasource. Data access interfaces are sandwiched between business access and databases, dealing with databases.
The DAO pattern is actually a combination of two patterns, the data Accessor mode and the Active domain object (domain objects) pattern . The data Accessor mode enables the separation of access and business logic, and the Active Domain object mode enables the object encapsulation of business data.
Two. The formation of DAO patterns
A typical DAO implementation has the following components:
- A DAO interface;
- A concrete class to implement DAO interface;
- Data passing Object (DTO): Sometimes called value object (VO) or domain model (domain)
Use DAO mode to make additions and deletions to database
This pattern can be roughly divided into three layers: 1. DAO Layer 2. Service Layer 3. Presentation Layer
1) Presentation layer: The role that the client uses to view and submit information
2) Service layer: Is the expression Layer and DAO layer of the link, in fact, do not do is to inform the role of the message
3) DAO: The real role to do (some operations on the database)
Give a life example:
It's like you go to a restaurant, you act as a (presentation) role, then you have a waitress (service layer), ask what you need to eat, give you the next order, and let you fill it out. Then the waiter sent the order to the Chef (DAO layer) there, the specific operation of the chef will take care of, a time after the chef to do a good food to the waiter, the waiter to the food to the customer, these operations are basically completed.
Execution order: presentation Layer--Service layer-->dao layer--back to service layer--return to presentation layer
Instance Link: http://blog.csdn.net/hzc543806053/article/details/7395998
Real-Home Java Backend Engineer Learning course-Task 1 (Day 5)