There have been some introductions about Beehive Data Control in Beehive PageFlow Tutorial, which will be used in an example, specifically, how to use the Beehive Build-IN data control mechanism to complete the database.
1. First, you need to configure the database connection.
BEEHIVE built-in database is Derby. If you want to use another database, you need to modify some source code, which is located in Beehive_Home / Samples / Controls-DB / SRC / DBControl / below. You can reset the URL of the database in the file DatabaseControlImpl.jcs, for example, you can use SQL Server as follows:
Class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver);
Other source files can also be modified as needed. Then use the ANT tool to recompile and packaged into dbcontrol.jar, place it to the corresponding position of the web application (generally Web-INF / LIB below).
2. Define database operations.
We need to create a JCX file to define the required database related operations, below to explain how to complete the operation.
Circle represents Action, block represents Page
In this example, we jump to DisplayData.jsp on the homepage, click on the DISPLAYDATA.JSP, show the result of the database operation here. So show Action is the core, which is operated by a series of database operations and outputs the result to the next page. Beehive uses its own mechanism to encapsulate the specific implementation of the database operation, so in the Show method, the relevant code amount is very small. In this example we need to create the following file:
l Project_Home / Web-INF / SRC / DBCONTROLLER / MyFirstDbControl.jcx
l Project_home / web-inf / src / forms / Employee.java
l Project_home / controller.jpf
l Project_home / *. JSP
Note: The SRC directory can build a package structure according to your needs, and you can correspond to Import.
MyfirstdbControl.jcx
Package dbcontroller;
Import java.sql.sqlexception;
Import java.io.serializable;
Import java.util.iterator;
Import java.util.hashmap;
Import javax.sql.rowset;
Import org.apache.beehive.controls.Api.bean.controleXtension;
Import dbcontrol.databaseControl;
Import dbcontrol.databaseControl.ConnectionDataSource;
Import dbcontrol.databaseControl.sql;
Import forms.employee;
@ControleXtension
@ConnectionDataSource (JNDINAME = "JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = TEST; user = sa; password = sa") // Configure Database Connection Properties
Public Interface MyfirstdbControl Extends DatabaseControl
{
@SQL (Statement = "Create Table Employee (ID Int Primary Key Not Null," "Name Varchar (50), AGE INT")
Public void createtable () throws sqlexception;
@SQL (statement = "Drop Table EMPLOYEE")
Public void Droptable () throws SqlexCeption;
@SQL (statement = "INSERT INTO Employee" "(ID, NAME, AGE)
VALUES ({u.id}, {u.Name}, {u.age}) ")
Public void insertemployee (Employee U).
@SQL (statement = "Update Employee Set Age = {AGE} where id = {id}")
Public void ChangeTitle (int ID, string title).
@SQL (statement = "delete from employee where id = {id}")
Public void deleteemployee (int id) throws sqlexception;
@SQL (statement = "SELECT NAME FROM EMPLOYEE")
Public string [] selectallname () throws SqlexCeption;
@SQL (statement = "select * from Employee where id = {id}")
Public Employee SelectemPloyee (Int ID) THROWS SQLEXCEPTION;
@SQL (statement = "SELECT * from Employee Order By ID")
Public Employee [] selectemployees () throws SqlexCeption;
@SQL (statement = "Select * from Employee Order By ID", ItemElementTyPe = Employee.class)
Public iterator selectemployeeswithiterator () THROWS SQLEXCEPTION;
@SQL (statement = "SELECT * from Employee Order By ID")
Public Hashmap SelectemPloyewithHashmap () THROWS SQLEXCEPTION;
@SQL (statement = "SELECT * from Employee Order By ID", maxROWS = 1)
Public Employee [] SelectoneemPloyee () throws SqlexCeption;
@SQL (Statement = "Select Count (*) from Employee")
Public Integer getCount () throws sqlexception; // Returns an INT type must define the return type INTEGER}
MyFirstDbControl first defines the JNDI attribute of the database connection, and the interface main body defines the capable of completing the database operation. Note To introduce the persistent class of the corresponding database table, that is, the operation object. Here, the construction table, delete the table, and the recording and deduplication of the record will be defined. For queries, different return types can be defined to meet the needs. You can also increase the maxRows property in @SQL, control the number of records returned.
Employee is the data sheet used in this example, which has three fields, ID, name, and age. We need to create a lasting class Forms / Employee.java corresponding to this table, notice that Beehive needs to declare the member variables of this class as public.
EMPLOYEE.JAVA
Package Forms;
PUBLIC CLASS EMPLOYEEEE {
Public integer ID;
Public String Name;
Public int Age;
Public Employee (Integer ID, String Name, INT AGE) {
THIS.ID = ID;
THIS.NAME = Name;
THIS.AGE = AGE;
}
Public EMPLOYEE (Integer ID) {
THIS.ID = ID;
}
Public integer getId () {
Return this.id;
}
Public void setid (Integer ID) {
THIS.ID = ID;
}
Public string getname () {
Return this.name;
}
Public void setname (String name) {
THIS.NAME = Name;
}
Public int getage () {
Return this.Age;
}
Public void setage (int Age) {
THIS.AGE = AGE;
}
}
3. Call database operations in Controller.
With the support of the two files above, we can edit the core files in the Page Flow Controller.jpf.
Controller.jpf
Import javax.servlet.http.httpsession;
Import Org.apache.beehive.Netui.pageFlow.Forward;
Import org.apache.beehive.netui.pageflow.pageflowController;
Import org.apache.beehive.netui.pageflow.annotations.jpf;
IMPORT FORMS.EMPLOYEE; // The lasting class required in the app
Import dbcontroller.myfirstdbcontrol; definition file for database operations required in the application
Import org.apache.beehive.control; // Used to explain @Control, implement the MyFirstDbControl interface as bean, and complete instantiation.
@Control
Public myfirstdbcontrol mycontrol;
Public Employee U;
@ JPF.Action
FORWARDS = {
@ JPF.Forward (Name = "Success", Path = "index.jsp")}
)
protected forward begin ()
{
Return New Forward ("Success");
}
@ JPF.Action
FORWARDS = {
@ JPF.Forward (Name = "Success", Path = "DisplayData.jsp")
}
)
protected forward show () throws java.sql.sqlexception {
u = myControl.selectemPloyee (1);
GetRequest (). SetAttribute ("Data", U);
Return New Forward ("Success");
}
It can be seen that in the show method, we call the SelectemPloyee method, query the ID of the ID 1, and return the value of the Employee type. The result is then assigned to a REQUEST variable, and the query result can be used in the page, as shown below:
name:
age:
Here, the results of the query are a record that returns the type of persistent class. If it is a plurality of records, we can use Vector as a container for query results and transfer it to page display. code show as below:
............ .....
@Control
Public myfirstdbcontrol mycontrol;
Public Vector U; PUBLIC VECTOR U;
............ .....
protected forward show () throws java.sql.sqlexception {
u = new vector ();
EMPLOYEE [] es = mycontrol.selectremployees ();
For (int I = 0; i U.Add (ES [I]); } Return New Forward ("Success"); } Accordingly, the page needs to be modified to display multiple records. It can be used directly with the label DisplayData.jsp