For more information about DbUtils APIs, see dbutilsapi.

Source: Internet
Author: User

For more information about DbUtils APIs, see dbutilsapi.

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. common S. 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 com. lizhou. util. mysqlUtil; 22 23 24/** 25 * use of the DbUtils framework 26 * objective: to reduce CURD operations 27 * The core class of the DbUtils framework is the QueryRunner class, which constructs its free parameter construction and a connection pool (DataSource) construction 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 in c3p0-config.xml // queryRunner (); 42 43 // The following are 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 // arrayH Andler (); // 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 // get the connection pool 64 DataSource ds = MysqlUtil. getPool (); 65 // use the connection pool to construct a QueryRunner 66 QueryRunner qr = new QueryRunner (ds); 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 (empty _ blank) O Haha ~ ", 2}); 73 qr. update (SQL, new Object [] {"", 25000}); 74} catch (SQLException e) {75 // TODO Auto-generated catch block 76 e. printStackTrace (); 77} 78} 79 80 // BeanHandler 81 public 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); 88 System. out. println (acc. getId () + "" + acc. getName () + "" + acc. getMoney (); 89} catch (SQLException e) {90 e. printStackTrace (); 91} 92 93} 94 95 // BeanListHandler 96 public static void beanListHandler () {97 QueryRunner qr = new QueryRunner (MysqlUtil. getPool (); 98 99 String SQL = "select * fro M 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 (. getId () + "" +. getName () + "" +. getMoney (); 105} 106} catch (SQLException e) {107 e. printStackTrace (); 108} 109} 110 111 // ArrayHandler112 public static void arrayHandler () {113 QueryRunner qr = new QueryRunner (MysqlUtil. getPool (); 114 115 String SQL = "select * from account where money =? "; 116 117 try {118 Object [] a = (Object []) qr. query (SQL, 10000, new ArrayHandler (); 119 for (int I = 0; I <. length; I ++) {120 System. out. print (a [I] + ""); 121} 122 System. out. println ("\ n ------------------------------"); 123} catch (SQLException e) {124 e. printStackTrace (); 125} 126} 127 128 // ArrayListHandler129 public static void arrayListHandler () {130 QueryRunner qr = new QueryRunner (MysqlUtil. getPool (); 131 132 String SQL = "select * from account"; 133 134 try {135 List <Object []> accounts = (List <Object []>) qr. query (SQL, new ArrayListHandler (); 136 for (Object [] obj: accounts) {137 for (int I = 0; I <obj. length; I ++) {138 System. out. print (obj [I] + ""); 139} 140 System. out. println ("\ n ----------------------------"); 141} 142} catch (SQLException e) {143 e. printStackTrace (); 144} 145} 146 147 // MapHandler148 public 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) {157 System. out. print (e. getKey () + ""); 158 System. out. println (e. getValue (); 159} 160} catch (SQLException e) {161 e. printStackTrace (); 162} 163} 164 165 // MapListHandler166 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) {176 System. out. print (e. getKey () + ""); 177 System. out. println (e. getValue (); 178} 179 System. out. println ("-------------------------"); 180} 181 182} catch (SQLException e) {183 e. printStackTrace (); 184} 185 186 187 // ScalarHandler returns only one row and one column of Data 188 public static void scalarHandler () {189 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 (); 196 System. out. println (obj); 197} catch (SQLException e) {198 e. printStackTrace (); 199} 200} 201 202}

 

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.