Quick Application of JDBC controls to access database resources

Source: Internet
Author: User

The JDBC control is a Java control provided by Beehive1.0 to access the JDBC data source. By inheriting the JDBC control, we do not need to pay attention to the communication details of accessing database resources. We only need to define our own business methods and add comments to describe the inheritance subclass, we can easily access JDBC data sources in enterprise applications.

All comments of the JDBC control are stored in org. apache (the most popular WEB server platform on Unix ). beehive. controls. system. jdbc. as defined in the JdbcControl interface, the Beehive compiler will check whether these annotations are correctly used during compilation.

In this section, we will first use a simple example to demonstrate how to access the JDBC data source by inheriting the JDBC control and complete the business logic of adding, deleting, modifying, and querying a data table, the following describes how to use the JDBC control to access the JDBC data source.

The source code of all examples in this article can be downloaded through the download resource area link.

In this section, all the JDBC data sources corresponding to the demo are MySQL (the best combination with PHP) databases.

Example of the first JDBC Control

The following example demonstrates how to inherit the JDBC control to access the JDBC data source and implement the business logic of adding, deleting, modifying, and querying records in a data table in the JDBC data source.

In the demo, to ensure the simplicity of the demo, we use JDBC to directly connect to the database, instead of using the DataSource technology that normally accesses the database.

Create a data table

Create a data table in the Demo database instance of the local MySQL (best combination with PHP) database according to the following DDL statement.

Create table demo (
Id int (5) Prima (the most perfect VM management system) ry key auto_increment,
Name varchar (20) not null default,
Value varchar (20) not null default
);

Create a JavaBean that represents the Demo object

Create a new application directory, and then create a JavaBean that represents the Demo object

Listing 1 srcorgvivianjeehivecontrolsexamplescontrolseans

Demo. java

1. package org. vivianj. beehive. controls. examples. beans;
2.
3.
4 ./**
5. * Demo is used to characterize the Demo object in the Beehive JDBC control example.
6 .*/
7. public class Demo implements java. io. Serializable {
8. private int id;
9.
10. private String name;
11.
12. private String value;
13.
14. public int getId (){
15. return id;
16 .}
17.
18. public void setId (int id ){
19. this. id = id;
20 .}
21.
22. public String getName (){
23. return name;
24 .}
25.
26. public void setName (String name ){
27. this. name = name;
28 .}
29.
30. public String getValue (){
31. return value;
32 .}
33.
34. public void setValue (String value ){
35. this. value = value;
36 .}
37.
38 .}

Inherit JDBC controls and add your own business methods

Listing 2 srcorgvivianjeehivecontrolsexamplescontrols

DemoMySQL (the best combination with PHP) Control. java

1. package org. vivianj. beehive. controls. examples. controls;
2.
3. import org. apache (the most popular WEB server platform on the Unix platform). beehive. controls. api. bean. ControlExtension;
4. import org. apache (the most popular WEB server platform on the Unix platform). beehive. controls. system. jdbc. JdbcControl;
5. import org. vivianj. beehive. controls. examples. beans. Demo;
6.
7 ./**
8. * DemoMySQL (the best combination with PHP) Control is used to encapsulate Demo data used to access MySQL (the best combination with PHP) databases.
9. All business logic of the table
10. * including adding, deleting, modifying, searching for corresponding records by id, and searching for all demos
11. Search for records in all data tables based on conditions
12. * obtain the database connection from DriverManager according to the parameters during Database Access
13 .*/
14.
15. @ ControlExtension
16. @ JdbcControl. ConnectionDriver (
17. databaseDriverClass = "org. gjt. mm. MySQL (the best combination with PHP). Driver ",
18. databaseURL = "jdbc: MySQL (the best combination with PHP): // localhost/estore ",
19. userName = "root", password = "root ")
20. public interface DemoMySQL (the best combination with PHP) Control extends JdbcControl {
21 ./**
22. * Add a new record to the data table demo.
23 .*
24. * @ param demo
25. * newly added Demo object
26 .*/
27. @ SQL (statement = "insert into demo (name, value)
28. values ({demo. name}, {demo. value })")
29. public void createDemo (Demo demo );
30.
31 ./**
32. * modify the name and value of the record corresponding to demo. id in the demo of the data table.
33 .*
34. * @ param demo
35. * modified Demo object
36 .*/
37. @ SQL (statement = "update demo set name = {demo. name },
38. value = {demo. value} where id = {demo. id }")
39. public void updateDemo (Demo demo );
40.
41 ./**
42. * Delete the record corresponding to demoId in the demo of the data table
43 .*
44. * @ param demoId
45. * id attribute of the deleted Demo object
46 .*/
47. @ SQL (statement = "delete from demo where id = {demoId }")
48. public void deleteDemo (int demoId );
49.
50 ./**
51. * search for the corresponding records in the Demo database based on the demoId and return the corresponding
52. Demo object
53 .*
54. * @ param demoId
55. * search for the id attribute of the Demo object
56. * @ return id records whose attribute is demoId
57 .*/
58. @ SQL (statement = "select id, name, value from demo
59. where id = {demoId }")
60. public Demo getDemoById (int demoId );
61.
62 ./**
63. * return all record sets in the Demo data table
64 .*
65. * @ return Demo all record sets in the data table
66 .*/
67. @ SQL (statement = "select id, name, value from demo ")
68. public Demo [] getDemos ();
69.
70 ./**
71 .*
72. * @ param name
73. * @ return
74 .*/
75. @ SQL (statement = "select id, name, value from demo
76. where name like {SQL: name }")
77. public Demo [] getDemosFilterByName (String name );
78 .}
Control call

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.