domain:只是定义一个javabean。
dao:对于数据库的操作,都放到dao层,也就是dao里面通常是对数据库的增、删、改、查等操作。
service:完成相应的业务逻辑处理,调用dao层。
servlet:完成界面请求、对界面进行跳转等等。servlet调用service层。
一、准备
JAR包:
既然是连接数据库第一个最重要的就是数据库的驱动包mysql-connection-java-5.1.44-bin.jar
接着就是C3P0-0.9.2.1.jar以及mchange-commons-java-0.2.3.4.jar
然后还少不了dbutils 使用的是commons-dbutils-1.7.jar
一共是4个JAR包
二、配置
配置数据库连接:
创建c3p0-config.xml的配置文件,里面包含连接数据库的信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <c3p0-config> 3 <default-config> 4 <property name="driverClass">com.mysql.jdbc.Driver</property> 5 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mybatis</property> 6 <property name="user">root</property> 7 <property name="password">123</property> 8 <property name="acquireIncrement">5</property> 9 <property name="initialPoolSize">10</property> 10 <property name="minPoolSize">5</property> 11 <property name="maxPoolSize">20</property> 12 </default-config> 13 </c3p0-config>
创建C3P0Util.java 使用getDataSource()的对象获取数据库连接
1 package com.utils; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import javax.sql.DataSource; 7 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 public class C3P0Utils { 11 private static ComboPooledDataSource dataSource=new ComboPooledDataSource("itheima"); 12 public static DataSource getDataSource(){ 13 return dataSource; 14 } 15 public static Connection getConnection(){ 16 try { 17 return dataSource.getConnection(); 18 } catch (SQLException e) { 19 throw new RuntimeException(e); 20 } 21 } 22 }
由于没有使用Mybatis的逆向工程,这里需要手动创建body.java 用于获取数据库表的所有列
1 package com.Domain; 2 public class body { 3 private int id; 4 private String notifyType; 5 private String requestId; 6 private String deviceId; 7 private String gatewayId; 8 private String service; 9 10 public int getid() { 11 return id; 12 } 13 public void setid(int id) { 14 this.id = id; 15 } 16 public String getnotifyType(){ 17 return notifyType; 18 19 } 20 public void setnotifyType(String notifyType) { 21 this.notifyType = notifyType; 22 } 23 public String getrequestId(){ 24 return requestId; 25 26 } 27 public void setrequestId(String requestId) { 28 this.requestId = requestId; 29 } 30 public String getdeviceId(){ 31 return deviceId; 32 33 } 34 public void setdeviceIde(String deviceId) { 35 this.deviceId = deviceId; 36 } 37 public String getgatewayId(){ 38 return gatewayId; 39 40 } 41 public void setgatewayId(String gatewayId) { 42 this.gatewayId = gatewayId; 43 } 44 public String getservice(){ 45 return service; 46 47 } 48 public void setservice(String service) { 49 this.service = service; 50 } 51 }
dao层DBUtils简化了JDBC的开发步骤,使得我们可以用更少量的代码实现连接数据库的功能。参考连接:https://blog.csdn.net/simonforfuture/article/details/90480147
DBUtils封装了JDBC的操作,核心功能如下:
Dbutils三个核心功能介绍
1、QueryRunner中提供对sql语句操作的API.
2、ResultSetHandler接口,用于定义select操作后,怎样封装结果集.
3、DbUtils类是一个工具类,定义了关闭资源与事务处理的方法
ResultSetHandler结果集处理类
1 package cn.simon.jdbc.demo03_DBUtils的使用; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.commons.dbutils.QueryRunner; 8 import org.apache.commons.dbutils.handlers.ArrayHandler; 9 import org.apache.commons.dbutils.handlers.ArrayListHandler; 10 import org.apache.commons.dbutils.handlers.BeanHandler; 11 import org.apache.commons.dbutils.handlers.BeanListHandler; 12 import org.apache.commons.dbutils.handlers.ColumnListHandler; 13 import org.apache.commons.dbutils.handlers.MapHandler; 14 import org.apache.commons.dbutils.handlers.MapListHandler; 15 import org.apache.commons.dbutils.handlers.ScalarHandler; 16 17 import cn.simon.jdbc.domain.Category; 18 19 /** 20 * DBUtils执行数据库查询操作 21 * 1.QueryRunner(DataSource) 22 * 2.query(String sql, ResultSetHandler<T> rsh, Object... params); // 主要执行查询 23 */ 24 25 public class DBUtilsDemo { 26 public static void main(String[] args) throws SQLException { 27 // TODO Auto-generated method stub 28 29 // queryDemo01(); 30 // queryDemo02(); 31 // queryDemo03(); 32 // queryDemo04(); 33 // queryDemo05(); 34 queryDemo06(); 35 // queryDemo07(); 36 // queryDemo08(); 37 } 38 39 // ArrayHandler处理类的使用 40 public static void queryDemo01() throws SQLException{ 41 // 1.创建QueryRunner对象 42 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 43 // 2.执行查询 44 Object[] objs = qr.query("select * from category where cid = ?", new ArrayHandler(), 1); 45 for(Object o: objs){ // object[]中保存了object对象 46 System.out.println(o); 47 } 48 } 49 50 // ArrayListHandler 51 public static void queryDemo02() throws SQLException{ 52 // 1.创建QueryRunner对象 53 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 54 // 2.执行查询 55 List<Object[]> objs = qr.query("select * from category ", new ArrayListHandler()); 56 for (Object[] objects : objs) { 57 System.out.println(objects[0]+"\t"+objects[1]); 58 } 59 } 60 61 // BeanHandler处理类的使用 62 public static void queryDemo03() throws SQLException{ 63 // 1.创建QueryRunner对象 64 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 65 // 2.执行查询 66 String sql = "select * from category"; 67 Category c = qr.query(sql, new BeanHandler<Category>(Category.class)); 68 System.out.println(c); 69 } 70 71 // BeanListHandler 72 public static void queryDemo04() throws SQLException{ 73 // 1.创建QueryRunner对象 74 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 75 // 2.执行查询 76 String sql = "select * from category"; 77 List<Category> c = qr.query(sql, new BeanListHandler<Category>(Category.class)); 78 for (Category category : c) { 79 System.out.println(category); 80 } 81 } 82 83 // ColumnListHandler处理类的使用 84 public static void queryDemo05() throws SQLException{ 85 // 1.创建QueryRunner对象 86 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 87 // 2.执行查询 88 String sql = "select * from category"; 89 List<Object> c = qr.query(sql, new ColumnListHandler<Object>("cname")); 90 System.out.println(c); 91 } 92 93 // MapHandler处理类的使用 94 public static void queryDemo06() throws SQLException{ 95 // 1.创建QueryRunner对象 96 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 97 // 2.执行查询 98 String sql = "select * from category"; 99 Map<String, Object> map = qr.query(sql, new MapHandler()); 100 // 3. 101 System.out.println(map); 102 } 103 104 // MapListHandler处理类的使用 105 public static void queryDemo07() throws SQLException{ 106 // 1.创建QueryRunner对象 107 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 108 // 2.执行查询 109 String sql = "select * from category"; 110 List<Map<String, Object>> maps = qr.query(sql, new MapListHandler()); 111 // 3.List 112 System.out.println(maps); 113 } 114 115 // MapListHandler处理类的使用 116 public static void queryDemo08() throws SQLException{ 117 // 1.创建QueryRunner对象 118 QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 119 // 2.执行查询 120 String sql = "select count(*) from category"; 121 Long count = qr.query(sql, new ScalarHandler<Long>()); 122 // 3.List 123 System.out.println(count); 124 }
public String bodyvalue() throws SQLException{
// 1.创建QueryRunner对象
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
// 2.执行查询(查询表中最后一句)
String sql = "select * from body order by id desc limit 1";
Map<String, Object> map = qr.query(sql, new MapHandler());
//通过转换成字符串用get方法获得键值对
String service = (String) map.get("service");
return service; 125 }