Simplify the use of JDBC (2)

zhaozj2021-02-16  54

No way, the article exceeds 64K. I have to split, I hope everyone will forgive me.

1. Custom data format

Date types are not the same in the SQL format in different databases. In order to solve this problem, we can use custom objects to implement Format to do this. Take String as an example, show the application of Format:

class StringFormat2 implements Format {public String name () {return "DB_STRING2";} public String format (String data) {return " '" data ":'";}} ...... Connection conn = ConnectionFactory.getConnectionByParams ( "org. gjt.mm.mysql.driver "," JDBC: MySQL: // LocalHost / DBFormstest "," SA "," "); dbcommand cmd = new dbcommand (conn); // Automatically acquire metadata (of course, can also be added manually DataSet DS = Cmd.Query ("Select * from service where 1 = 2"); // Setting Change Condition String [] conditions = {"ID = 100"}; string [] [] data1 = {{"301" , "Dog Walking", "Talking The Dog for A Walk"}; // Settings Table Name

DS.getMetadata (). settablename ("service"); // Sets the last column data format ds.getMetadata (). getColumnMetadata (2) .setFormat (new stringformat2 ());

// Setting data

DS.SETDATA (DATA1); cmd.Update (DS, conditions); conn.close ();

Use stored procedures

Although the stored procedure is not conducive to the program's transplant, it is still unable to avoid it in practical applications. Support for calling the stored procedure is also provided in the kit. Store procedure object UML diagram:

DBSPArameter, the form of dbstoredProcess is very similar to DataSet and components, only given specific applications.

Connection conn = ConnectionFactory.getConnectionByParams ( "org.gjt.mm.mysql.Driver", "jdbc: mysql: // localhost / dbformstest", "sa", ""); DbCommand cmd = new DbCommand (conn); DbStoredProcess sp = new DbStoredProcess (); sp.setName ( "sp1"); DbSpParameter param1 = new StringParameter (); param1.setValue ( "Tod"); DbSpParameter param2 = new NumbericParameter (); param2.setValue ( "123.4"); DbSpParameter PARAM3 = New DateTimeParameter (); param3.SetValue ("2001-11-11"); sp.add (param1); sp.Add (param2); cmd.callstoredProcess (sp); conn. Close (); browsing data

Browsing data is the most common requirements of database applications, especially web database applications. It is impossible to return a large amount of data in a time in a web environment (because the consumption server is large), usually the data is organized in the form of the page, and each page contains several data, and the request only returns only one page.

This part of this part of the tool is done by DBReader, the main function:

2 Support for paging, allowing the number of records per page to be specified;

2 Implement the Iterator interface, allowing users to traverse data;

2 Implement First, Prev, After, Last method;

2 Allow users to specify page number, fast positioning;

Application examples

Connection conn = connectionByParams ("Org.gjt.mm.mysql.driver", "JDBC: MySQL: // LocalHost / DBFormstest", "SA", ""); // Set 5 data DBReader DR = DBReader.createInstance (CONN, 5); // Query Dr.Query ("Select ID, Name from Service"); // Browse while (Dr.hasNext ()) {DataSet DS = (Dataset) Dr.Next () System.out.print (ds.tostring ());} conn.close ();

Format output

Get data and convert to a specified format, which is the most common function in database development applications. In the toolkit, XML, CSV is currently supported. This part of the function is done by DBUTIL. For XML, the currently supported format is very simple, missing support for DTD. The form is as follows:

data

Application examples

Dbcommand com = new dbcommand (conn); DataSet DS = com.query ("SELECT * from T_JC_DWLB"); DBUTIL.GETXML ​​(DS, "GB2312"); end è

This article gives a custom toolkit design, an application example. It is to be reminded that the purpose of the toolkit is not to replace JDBC usage, but just simplify its use. Since the author is deeply affected by the "container algorithm phase separation" of C STL, this idea is not known when designing. Perhaps, there is a better implementation, I hope the reader can give the author some suggestions.

The UML map of the entire toolkit is as follows:

Toolkit implementation, test environment:

2 JDK1.4.1;

2 mysql 4.01;

2 Oracle8i; (where 816 driver has problems with Chinese support in scrolling results, please change 817 driver)

2 DB2;

Reference

Design Patterns, an epochial work, a summary of predecessors, OO actual applications;

Patterns of Enterprise Application Architecture, the latest works of Martin Fowler, comprehensive introduction to corporate development

Modular mode

About author

Hulu, Master of Engineering, Xi'an Jiaotong University, is keen on J2EE / .NET technology, is the loyal advocator of OpenSource. At present, Xi'an Software Co., Ltd. is currently created with friends. The company is mainly committed to the development of Java web applications, and many projects have been applied in energy, electricity and transportation industries. You can contact him through James.hu@chinacodeline.com.

转载请注明原文地址:https://www.9cbs.com/read-24843.html

New Post(0)