Real-Home Java Backend Engineer Learning course-Task 1 (Day 5)

Source: Internet
Author: User

A review of yesterday's knowledge

  1. Package COM.JDBC;
  2. Import java.sql.Connection;
  3. Import Java.sql.DriverManager;
  4. Import Java.sql.ResultSet;
  5. Import java.sql.Statement;
  6. Public class Firstjdbc {
  7. Public static void Main (string[] args)
  8. {
  9. Invoke operation to connect to the database
  10. Connection con = createconnection ();
  11. }
  12. /**
  13. * JDBC establishes a SQL Server database connection
  14. */
  15. Private static Connection createconnection () {
  16. Defining Load Drivers
  17. String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
  18. Define connection server and Database sample
  19. String Dburl = "jdbc:sqlserver://localhost:1433;  DataBaseName = Sample1 ";
  20. Default user name, do not use Windows default authentication
  21. String userName = "sa";
  22. String UserPassword = "Zhichao";
  23. Connection Connection = null;
  24. Statement STA = null;
  25. try {
  26. Official Load Driver
  27. Class.forName (drivername);
  28. Start connection
  29. Connection = Drivermanager.getconnection (Dburl, UserName, UserPassword);
  30. System.out.println ("Connection Success!");
  31. Execute SQL statements to the database
  32. STA = Connection.createstatement ();
  33. ResultSet rs = sta.executequery ("Select Id,name,height from Table_1");
  34. while (Rs.next ())
  35. {
  36. int id = rs.getint ("id");
  37. String name = rs.getstring ("name");
  38. float height = rs.getfloat ("height");
  39. SYSTEM.OUT.PRINTLN ("id =" +id+"name =" +name+"height =" +height ");
  40. }
  41. } catch (Exception e) {
  42. System.out.println ("Connection Fail!");
  43. E.printstacktrace ();
  44. }
  45. /**
  46. * Close Database
  47. * @param connection
  48. */
  49. Finally
  50. {
  51. try {
  52. if (null! = STA)
  53. {
  54. Sta.close ();
  55. STA = null;
  56. System.out.println ("Statement closed successfully");
  57. }
  58. if (null! = connection)
  59. {
  60. Connection.close ();
  61. connection = null;
  62. System.out.println ("Connection closed successfully");
  63. }
  64. } catch (Exception e) {
  65. E.printstacktrace ();
  66. }
  67. }
  68. return connection;
  69. }
  70. }

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)

Related Article

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.