Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to associate sql operation with object-oriented programming with java reflection technology

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge points of this article "how to use java reflection technology to associate sql operations with object-oriented programming" are not quite understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article entitled "how to associate sql operations with object-oriented programming using java reflection technology."

Example code:

Public class SqlUtil extends BaseApplogic {

Public List excuteQuery (String sql, Object [] paras, Object voo)

Throws AppException {

DBPersistenceManager dbpm = this.getFnmsDBPM ()

List list=new ArrayList ()

Try {

DataSet ds = (DataSet) dbpm.executeQuery (sql, paras)

DataSetMetaData dsmd = ds.getDataSetMetaData ()

Field [] fd = voo.getClass () .getDeclaredFields

String className = voo.getClass () .getName ()

Int size = fd.length

Method md [] = new Method [size]

/ / construct method []

For (int I = 0; I < size; iTunes +) {

Attribute attr=dsmd.getAttribute (FDI] .getName () .toUpperCase ()

If (null! = attr) {

Field f = voo.getClass () .getDeclaredField (FDI [I] .getName ())

String type = f.getType () .getName ()

Class [] types=getTypes (type)

String methodName=getSetterName (TD [I] .getName ())

Md [I] = voo.getClass () .getMethod (

MethodName,types)

}

}

While (ds.next ()) {

Object o = Class.forName (className) .newInstance ()

For (int I = 0; I < size; iTunes +) {

If (nullroomroommd [I]) {

/ / call

Attribute attr=dsmd.getAttribute (FDI] .getName () .toUpperCase ()

If (null==attr) continue

Object [] pa=new Object [] {ds.getString (attr.getAttrName ())}

MD [I] .invoke (orecom pa)

}

}

List.add (o)

}

} catch (DrmException drme) {

This.handleException (drme)

} catch (Exception e) {

This.handleException (e); / / New exception handling

} finally {

If (dbpm! = null) {

Dbpm.close ()

}

}

Return list

}

/ / the set method is called by property

Public static String getSetterName (String propName) {

Return "set" + propName.substring (0,1). ToUpperCase ()

+ propName.substring (1, propName.length ())

}

/ / take the type

Public static Class [] getTypes (String type) {

If (type.equals ("java.lang.String")) {

Return new Class [] {String.class}

} else if (type.equals ("int")) {

Return new Class [] {Integer.TYPE}

} else if (type.equals ("long")) {

Return new Class [] {Long.TYPE}

} else if (type.equals ("float")) {

Return new Class [] {Float.TYPE}

} else {

System.out.println ("no such type!")

Return null

}

}

}

The excuteQuery method passes in three parameters, the first is the sql statement to query, the second is the parameter array, and the third is the type of object to be returned.

The return value is that each object in a list,list is the object type you passed in.

After such a wrapper, the sql and the object are encapsulated naturally, without having to check out every query, then resultset.next (), getString (), and then setXxx ()

Of course, this is just the tip of the iceberg that metadata and java object reflection technology takes advantage of.

The above is the content of this article on "how to use java reflection technology to associate sql operations with object-oriented programming". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report