JDBC Basics-about Rowmapper,resultset

Source: Internet
Author: User
<span id="Label3"></p><p><p>Generic class<br>public class Generic<t> {<br>public void MethodOne (T) {<br><br>}<br>public void Methodtwo (list<?>) {<br><br>}<br>}<br>A declared generic class represents a new data type that is not known when it is created, so it is declared using the T placeholder to declare the data type of the newly created object when using the generic class. Any data type can be passed IN.<br>When you make a parameter in a method, the object must use the same data type as the object to pass in as a parameter when calling the Method. When a method parameter is a collection, it can only be used? The form of a question mark to pass IN.<br>Public T Methodthree () {</p></p><p><p>}<br>Public list<t> Methodfour () {</p></p><p>}<br>Returns a method of type T and List collection element type T<br>T can be decorated and restricted in a generic class or Method.<br>public class Genericclass (T extends Serializable) {<br>}<br>public class Genericclass (T extends Person) {<br>}<br>Using extends in generics to represent inheritance (extends) and implementation (implements)<br>public void Methodfive (T extends Serializable) {<br>}<br>public void Methodsix (T extends Person) {<br>}<br>T in a method is already restricted when the class has already decorated and restricted T.<br>public class Genericclass<t extends Serializable> {<br>public void method (list<t> List) {<br>}<br>}<br>public class Genericclass {<br>public void Method (list<? extends serializable> List) {<br>}<br>}<br>public class Genericclass {<br>Public <t extends serializable> void method (list<t> List) {<br>}<br>}<br>Placeholder T declaration of several ways<br>1. Declare after the class<br>2, in the method parameter declaration (? extends Serializable)<br>3, before the method return value of the decoration<br>Public <t extends serializable> void FindAll (list<t>) {<br>}<br>When the class is not generic, but the method also uses generics, you can use the latter two ways, for the collection can use 2, 3 two ways, but for a single parameter must use the third way, can not be used in the case of a single parameter? question mark to indicate the type of parameter to pass IN. That is a special placeholder that is specifically provided for the Collection.<br>When a method parameter has two generic types, you can refer to the method return value in front of the limit<br>When the method parameter is only used for generics, only &lt can be used;? Extends serializable> this one way<br> </p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>The reflection mechanism of Java is the basis for learning all the frameworks later.<br>The main work of the reflection mechanism:<br>1. Create a Class object by reflection mechanism when an object of a class cannot be new<br>2, when the object of the class is not created, get the structure of the class (class name, inheritance structure, which interfaces are implemented, what are the Methods)<br>When using, know the fully qualified name of the class (package name + Class Name)<br>A class object that is the class with the specified name is obtained by Class.forName (the fully qualified name of the class). In this case, the generic class is received, and the representative does not know what the specific type is.<br>Class<t> clazz = Class.forName ();<br>Creates an object of a specified class based on class object<br>Object obj = clazz.newinstance ();<br>The default invocation of the Class's fully qualified name class is constructed without arguments, and errors occur when the class does not have a parameterless construction method.<br>GetMethod (String Name)<br>Get this method according to the method name<br>GetMethods () Get all the methods of this class object<br>Using this method requires the introduction of the Java.lang.reflect.Method package, which is a packet of Java.lang packets that need to be introduced.<br>Get the method, you can get the name of the method<br>String methodName = Method.getname ();<br>When you get a class object, you can use the class name in addition to the fully qualified name of the Class. class to obtain a class object<br>Method execution<br>Method.invoke (obj,params);<br>The objects required to execute this method and the parameters required to execute the method are passed IN.<br>Using the invoke execution method, the return value is of type object, and you can force the type conversion</p></p><p><p>Three ways to get a class object<br>1, class Clazz = class.forname ("fully qualified name of the class");<br>2. Class Name. Classes ();<br>3. Object. GetClass ();<br>Reflection is used for personrowmapper and studentrowmapper, which allows programs to automate methods such as Obj.setxxx.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Beanpropertyrowmapper (entity attribute Mapping)</p></p><p><p>Used to resolve setxxxx methods that are repeated in personrowmapper and studentrowmapper, allowing the system to automatically generate such a method.<br>In order for the program to automatically generate setxxx such a method, first to get all the column names in the resultset result set<br>(meet this problem, direct Google)</p></p><p><p>Get all column names<br>ResultSetMetaData RSMD = Rs.getmetadata ();<br>int columnCount = Rsmd.getcolumncount ();<br>Get the name of a column based on the number of columns in the result set<br>for (int i = 1;i <= columncount;i++) {<br>String columnName = Rsmd.getcolumnlabel ();<br>}</p></p><p>The <p> Gets the name of the column in two ways <br> 1, getcolumnname () <br> 2, getcolumnlabel () <br> The first one gets the original name of the column, and the second is the alias of the obtained column (the alias of the column is obtained when there is an alias). When there is no time to get the original name of the Column) <br> Gets the value of this column based on the column name <br> for (int i = 1; i <= columnCount; i++) {<br>/<br> Gets the index of the column name <br> Getcolumnlabel (i) from 1 start <br>/<br> String columnName = Rsmd.getcolumnlabel (i); <br> Object Columnvalue = Rs.getobject (columnName); <br> setPropertyValue (obj,columnname,columnvalue); <br>} <br> Calls method <br> private void setPropertyValue (T obj,string columnname,object Columnvalue) {<br> String based on or value MethodName = "set" + columnname.substring (0, 1). touppercase () <br> + columnname.substring (1); <br> <br> method[] methods = Clazz.getmethods (); <br> for (Method method:methods) {<br> if (methodname.equals (method.getna Me ()) {<br> try {<br> method.invoke (obj, columnvalue); <br> break; <br>} catch (illegalaccessexception e) {<br> E.printsta Cktrace (); <br>} catch (illegalargumentexception e) {<br> e.printstacktrace (); <br>} catch (invocationtargetexception e) {<br> e.pri Ntstacktrace (); <br>} <br>} <br>} </p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>Maprowmapper:</p></p><p><p>public class Maprowmapper implements rowmapper<map<string, object>>{</p></p><p><p>@Override<br>Public map<string, object> Maprow (ResultSet rs) {<br>hashmap<string, object> map = new Hashmap<string,object> ();<br><br>ResultSetMetaData rsmd;<br>try {<br>RSMD = Rs.getmetadata ();<br>int columnCount = Rsmd.getcolumncount ();<br>for (int i = 1;i <= columncount;i++) {<br>String columnName = Rsmd.getcolumnlabel (i);<br>Object Columnvalue = Rs.getobject (columnName);<br>Map.put (columnName, columnvalue);<br>}<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>Return map;<br>}</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Apache---commons--dbutils: You can refer to the implementation</p></p><p><p></p></p><p><p></p></p><p><p>------------------------------------------------------------------------------</p></p><p><p>Arrayrowmapper:</p></p><p><p>A record of the query is loaded into an array of objects, and each object in the array is used to hold the corresponding Value.<br>The procedure is the same as that of the Maprowmapper Operation.<br>1. Get the number of columns in a record<br>2. Get the corresponding value for each column<br>3. Load the corresponding value into an array of objects</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>JDBC like query:</p></p><p><p>Connection conn = Drivermanager.getconnection ();</p></p><p><p>String sql = "select from book where bookname?" or Bookauthor like? or Bookpublisher like? ";<br>String keyword = "%" + keyword + "%";<br>Stat.executequery (sql,keyword);</p></p><p><p></p></p><p><p>"Y". equalsignorecase (index); about getting column names</p></p><p><p>The efficiency of the two columns is higher than the efficiency of querying a column.</p></p><p><p>When modifying the information of an entity class, some fields cannot be modified, such as an entity class for a person, the ID cannot be modified, and username cannot be modified.</p></p><p><p>Java.sql.TimeStamp is a subclass of java.util.Date, and when the value of timestamp is obtained, the class of timestamp is used to receive the value of that Type.</p></p><p><p></p></p><p><p></p></p><p><p>Entity class Properties:</p></p><p><p>The wrapper class is used uniformly when selecting the type for the attribute of the entity class.<br>Because there is a default value when the entity class attribute (global Variable) is not assigned, the default value for the integer is 0, and the default value for the decimal is false for the default value of the 0.0.boolean type. It is possible to use an int type with a property named score, and when the value is really 0, you cannot tell whether the value is a default value or an already assigned Value.<br>therefore, the use of wrapper classes can solve this ambiguity problem. When this property is not assigned a value, the default value of this property is null, which is easily distinguished from the assigned Value.</p></p><p><p></p></p><p><p>Admindao can only perform crud operations on the Admin class and cannot perform database operations on other classes<br><br></p></p><p><p></p></p><p><p>JDBC Basics-about Rowmapper,resultset</p></p></span>

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.