Dbutils the use of common APIs for easy access later

Source: Internet
Author: User

  1 package com.lizhou.test; 2 3 import java.sql.sqlexception; 4  Import java.util.list; 5 import java.util.map; 6 import java.util.map.entry ;  7 import java.util.set; 8 9 import javax.sql.datasource; 10 11  import org.apache.commons.dbutils.QueryRunner; 12 import  org.apache.commons.dbutils.handlers.arrayhandler; 13 import  org.apache.commons.dbutils.handlers.arraylisthandler; 14 import  org.apache.commons.dbutils.handlers.beanhandler;15 import  org.apache.commons.dbutils.handlers.beanlisthandler; 16 import  org.apache.commons.dbutils.handlers.maphandler;17 import  org.apache.commons.dbutils.handlers.maplisthandler; 18 import  org.apache.commons.dbutils.handlers.scalarhandler;  19 20 import  Com.lizhou.domain.account; 21 import coUse of the m.lizhou.util.mysqlutil; 22 23 24 /** 25 * dbutils Framework 26 * Purpose: reduce curd operation   The core class of the 27 * dbutils Framework is the Queryrunner class, which constructs its null parameter construction and the construction with the connection pool (DataSource)  28 * 29 * 30  * 31 *  @author  bojiangzhou 32 * 33 */ 34 public  class dbutils { 35 36 public static void main (String[] args) { 37 /* * 38 * database is test, configure  39 */ 40 41 // queryrunner ();  in C3p0-config.xml 42 43 //The following is the usage of various implementation classes of the Resultsethandler interface (7 implementation classes)  44 45 // beanhandler ();  // for Javabean 46 47 //beanlisthandler ();  //for javabean 48 49 //  Arrayhandler ();//For array  50 51 // arraylisthandler ();  //for array  52 53 // Maphandler ();  //for Map 54 55 // maplisthandler ();  //for map 56 57  Scalarhandler ();  //for Long 58 59 } 60 61//queryrunner 62 public static void  Queryrunner () { 63 //Gets the connection pool  64 datasource ds = mysqlutil.getpool ();  65 // Construct a queryrunner 66 queryrunner qr = new queryrunner (DS) with a connection pool; 67 68 //  String sql = "Update account set name=?" where id=? ";  69 string sql = "INSERT into account [name, money] values (?,?)";  70 71 try { 72 //qr.update (SQL, New object[]{"O (∩_∩) o haha ~", 2});  73qr.update ( Sql, new object[]{"Life has You", 25000}); 74 } catch  (SQLException e) { 75 //  todo auto-generated catch Block76 e.printstacktrace ();  77 } 78 } 79 80  //beanhandler 81public static void beanhandler () { 82 queryrunner QR =  New queryrunner (Mysqlutil.getpool ());  83 84 string sql = "SELECT * from account where id = ?";  85 86 try { 87 account acc = (account) Qr.query (Sql, new object[]{3},  new beanhandler (Account.class));  88system.out.println (Acc.getid () + "+acc.getname () +" "+ Acc.getmoney ()); 89 } catch  (SQLException e) { 90e.printstacktrace ();  91 }  92 93 } 94 95 //BeanListHandler 96     public static  void beanlisthandler () { 97 queryrunner qr = new queryrunner (MysqlUtil.getPool ());  98 99 string sql = "SELECT * From Account"; 100 101 try { 102 list< account> accounts = (list<account>) qr.query (Sql, new beanlisthandler (Account.class));  103  for (account a:accounts) { 104 system.out.println (A.getid () + "" +a.getname () + "" +a.getmoney ());  105 } 106 } catch  (SQLException e) { 107 e.printstacktrace ();  108& NBSP;}  109 } 110      111 //arrayhandler 112 public static  void arrayhandler () { 113 queryrunner qr = new queryrunner (MysqlUtil.getPool ());  114 115 string sql = "SELECT * from account where money=?";  116 117 try { 118object[] A = (object[]) qr.query (SQL, 10000, new arrayhandler ( );  119 for (int i = 0;i < a.length;i++) { 120system.out.print (a[i]+ "");  121 }  122 system.out.println ("\ n------------------------------"); 123 } catch  ( SQLException e) { 124e.printstacktrace ();  125 } 126 } 127 128 // Arraylisthandler 129 public static void arraylisthandler () { 130 QueryRunner QR = new queryrunner (Mysqlutil.getpool ());  131 132 string sql = "SELECT * from Account";  133 134 try { 135list<object[]> accounts = (list<object[]>) qr.query (Sql, new arraylisthandler ());  136  for (object[] obj:accounts) { 137for (Int i = 0;i < obj.length;i++) { 138 system.out.print (obj[i]+ "");  139 } 140 system.out.println ("\ n------------------------------");  141  } 142 } catch  (SQLException e) { 143e.printstacktrace ();  144 } 145  } 146 147 //maphandler 148public static void maphandler () { 149  queryrunner qr = new queryrunner (Mysqlutil.getpool ());  150 151 string sql = "SELECT * From account ";  152 153 try { 154 map<object, object> Map = (Map<object, object>) Qr.query (Sql, new maphandler ());  155 set<entry<object, Object>> Entry = map.entryset ();  156 for (Entry e:entry) { 157system.out.print (E.getkey () + ""); &nbsP;158system.out.println (E.getvalue ()); 159 } 160 } catch  (SQLException e) {  161 e.printstacktrace (); 162 } 163 } 164      165 // Maplisthandler 166 public static void maplisthandler () { 167 QueryRunner QR =  new queryrunner (Mysqlutil.getpool ());  168 169 string sql = "SELECT * From Account";  170 171 try { 172 list< Map<object, object> > Maps = (list<map<object, object>>) Qr.query (Sql, new maplisthandler ())  173 for (Map<object, Object>  Map:maps) { 174 set<entry<object, object>> Entry = map.entryset ();  175 for ( Entry e:entry) { 176system.out.print (E.getkey () + "");  177system.out.println (E.getvalue ());  178  } 179system.out.println ("-------------------------");  180 } 181 182 } catch  (SQLException e) { 183 e.printstacktrace (); 184 } 185     } 186 187 188 //scalarhandler returns only one row of data  189public static void  Scalarhandler () { 190 queryrunner qr = new queryrunner (Mysqlutil.getpool ()); 191  192 string sql = "SELECT COUNT (*) from account";  193 194 try { 195 object obj = Qr.query (Sql, new scalarhandler ());  196system.out.println (obj);  197 } catch   (SQLException e) { 198e.printstacktrace ();  199 } 200 } 201 202 }

Dbutils common APIs for easy access later

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.