如何使用DBUtils高效操作数据库并实现长尾词效果?

2026-04-16 13:382阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2555个文字,预计阅读时间需要11分钟。

如何使用DBUtils高效操作数据库并实现长尾词效果?

通过读取配置文件database.properties获取数据库连接,使用DBUtils工具类操作数据库。

javaimport com.mysql.jdbc.Connection;import java.io.InputStream;import java.sql.SQLException;

public class DatabaseUtil { public Connection getConnection() throws SQLException, ClassNotFoundException { Class.forName(com.mysql.jdbc.Driver); InputStream is=this.getClass().getClassLoader().getResourceAsStream(database.properties); Properties prop=new Properties(); prop.load(is); String url=prop.getProperty(url); String user=prop.getProperty(user); String password=prop.getProperty(password); return (Connection) DriverManager.getConnection(url, user, password); }}

通过读取配置文件database.properties获得数据库连接,使用DBUtils工具类操作数据库

package com.mysql.jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.mysql.test.Admins; /** * @ProjectName: JDBCUtils * @PackageName: com.mysql.jdbc * @ClassName: DBUtils * @Description: 使用DBUtils工具类,对数据库操作 * @Author: 伏永正 * @Date: 2017年9月25日 下午5:47:53 */ public class DBUtils { private static Connection connection; //连接对象 private static String driverClass; //数据库驱动 private static String url; //数据库地址jdbc:mysql://连接主机ip:端口号/数据库名 private static String username; //数据库登录名 private static String password; //数据库密码 static{ try { //读取配置文件 readConfig(); //加载驱动 Class.forName(driverClass); connection=DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: readConfig * @Description: 读取database.properties配置文件,获得连接属性:driverClass url username password * @Return: void * @Author: 伏永正 * @Date: 2017年9月25日下午5:48:26 * @throws */ private static void readConfig() { try { //使用类的加载器 InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("database.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取集合中的键值对 driverClass=properties.getProperty("driverClass"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: SQLUpdate * @Description: 封装SQLUpdate方法,调用QueryRunner类的方法update完成对数据库的:增 删 改功能 * @Params: @param sql 构建的SQL语句,?占位符必不可少 * @Params: @param params 以Object[]数组方式 传递参数 参数个数与sql语句占位符相等 * @Params: @return * @Return: boolean * @Author: 伏永正 * @Date: 2017年9月25日下午6:10:44 * @throws */ public static boolean SQLUpdate(String sql,Object[] params) { try { //创建QueryRunner类对象 QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner类的方法update执行SQL语句,且返回int int row = queryRunner.update(connection, sql, params); //释放连接对象 DbUtils.closeQuietly(connection); if (row == 0) { return false; } else { return true; } } catch (SQLException e) { throw new RuntimeException("SQL语句执行失败"); } } /** * @MethodName: arrayHandler * @Description: 结果集第一种处理方式ArrayHandler 将结果集的第一行数据存储到对象数组中Object[] * @Params: @param sql 查询语句 * @Return: Object[] * @Author: 伏永正 * @Date: 2017年9月25日下午6:29:53 * @throws */ public static Object[] arrayHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的是Object[] Object[] result = queryRunner.query(connection, sql, new ArrayHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(); } } /** * @MethodName: ArrayListHandler * @Description: 结果集的第二种处理方式ArraySetHandler 将结果集的每一行数据,将数据封装到对象数组中Object[] * 多个对象数组存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:30:57 * @throws */ public static List ArrayListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的每一行是Object[],并且存储在List集合中 List result = queryRunner.query(connection, sql, new ArrayListHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanHandler * @Description: 结果集的第三种处理方式BeanHandler 将结集果的第一行数据,封装成JavaBean对象 * @Params: @param sql * @Return: Admins * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:22 * @throws */ public static Admins BeanHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Admins admins = queryRunner.query(connection, sql, new BeanHandler (Admins.class)); DbUtils.closeQuietly(connection); return admins; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanListHandler * @Description: 结果集的第四种处理方式BeanListHandler 将结果集的每一行数据,封装到JavaBean对象 * 多个JavaBean的对象存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:52 * @throws */ public static List BeanListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result =queryRunner.query(connection, sql, new BeanListHandler (Admins.class)); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapHandler * @Description: 结果集的第五种处理方式MapHandler 将结果集的第一行数据,封装到Map集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: Map * @Author: 伏永正 * @Date: 2017年9月25日下午6:33:35 * @throws */ public static Map MapHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Map map = queryRunner.query(connection, sql, new MapHandler()); DbUtils.closeQuietly(connection); return map; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapListHandler * @Description: 结果集的第六种处理方式MapListHandler 将结果集的每一行数据,存储到Map集合中 多个Map集合存储到List集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: List > * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:04 * @throws */ public static List > MapListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List > list = queryRunner.query(connection, sql,new MapListHandler()); DbUtils.closeQuietly(connection); return list; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ColumnListHandler * @Description: 结果集第七种处理方式ColumnListHandler 结果集是指定列的数据,存储到List集合 * 数据类型用List 每列的每个数据类都不同 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:28 * @throws */ public static List ColumnListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result = queryRunner.query(connection, sql, new ColumnListHandler("aname")); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ScalarHandler * @Description: 结果集第八种处理方式ScalarHandler 查询后只有一个结果 * @Params: @param sql * @Return: long * @Author: 伏永正 * @Date: 2017年9月25日下午6:36:16 * @throws */ public static long ScalarHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); long count = queryRunner.query(connection, sql, new ScalarHandler ()); DbUtils.closeQuietly(connection); return count; } catch (SQLException e) { throw new RuntimeException(e); } } } DBUtils.java

package com.mysql.jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.mysql.test.Admins; /** * @ProjectName: JDBCUtils * @PackageName: com.mysql.jdbc * @ClassName: DBUtils * @Description: 使用DBUtils工具类,对数据库操作 * @Author: 伏永正 * @Date: 2017年9月25日 下午5:47:53 */ public class DBUtils { private static Connection connection; //连接对象 private static String driverClass; //数据库驱动 private static String url; //数据库地址jdbc:mysql://连接主机ip:端口号/数据库名 private static String username; //数据库登录名 private static String password; //数据库密码 static{ try { //读取配置文件 readConfig(); //加载驱动 Class.forName(driverClass); connection=DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: readConfig * @Description: 读取database.properties配置文件,获得连接属性:driverClass url username password * @Return: void * @Author: 伏永正 * @Date: 2017年9月25日下午5:48:26 * @throws */ private static void readConfig() { try { //使用类的加载器 InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("database.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取集合中的键值对 driverClass=properties.getProperty("driverClass"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: SQLUpdate * @Description: 封装SQLUpdate方法,调用QueryRunner类的方法update完成对数据库的:增 删 改功能 * @Params: @param sql 构建的SQL语句,?占位符必不可少 * @Params: @param params 以Object[]数组方式 传递参数 参数个数与sql语句占位符相等 * @Params: @return * @Return: boolean * @Author: 伏永正 * @Date: 2017年9月25日下午6:10:44 * @throws */ public static boolean SQLUpdate(String sql,Object[] params) { try { //创建QueryRunner类对象 QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner类的方法update执行SQL语句,且返回int int row = queryRunner.update(connection, sql, params); //释放连接对象 DbUtils.closeQuietly(connection); if (row == 0) { return false; } else { return true; } } catch (SQLException e) { throw new RuntimeException("SQL语句执行失败"); } } /** * @MethodName: arrayHandler * @Description: 结果集第一种处理方式ArrayHandler 将结果集的第一行数据存储到对象数组中Object[] * @Params: @param sql 查询语句 * @Return: Object[] * @Author: 伏永正 * @Date: 2017年9月25日下午6:29:53 * @throws */ public static Object[] arrayHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的是Object[] Object[] result = queryRunner.query(connection, sql, new ArrayHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(); } } /** * @MethodName: ArrayListHandler * @Description: 结果集的第二种处理方式ArraySetHandler 将结果集的每一行数据,将数据封装到对象数组中Object[] * 多个对象数组存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:30:57 * @throws */ public static List ArrayListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的每一行是Object[],并且存储在List集合中 List result = queryRunner.query(connection, sql, new ArrayListHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanHandler * @Description: 结果集的第三种处理方式BeanHandler 将结集果的第一行数据,封装成JavaBean对象 * @Params: @param sql * @Return: Admins * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:22 * @throws */ public static Admins BeanHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Admins admins = queryRunner.query(connection, sql, new BeanHandler (Admins.class)); DbUtils.closeQuietly(connection); return admins; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanListHandler * @Description: 结果集的第四种处理方式BeanListHandler 将结果集的每一行数据,封装到JavaBean对象 * 多个JavaBean的对象存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:52 * @throws */ public static List BeanListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result =queryRunner.query(connection, sql, new BeanListHandler (Admins.class)); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapHandler * @Description: 结果集的第五种处理方式MapHandler 将结果集的第一行数据,封装到Map集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: Map * @Author: 伏永正 * @Date: 2017年9月25日下午6:33:35 * @throws */ public static Map MapHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Map map = queryRunner.query(connection, sql, new MapHandler()); DbUtils.closeQuietly(connection); return map; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapListHandler * @Description: 结果集的第六种处理方式MapListHandler 将结果集的每一行数据,存储到Map集合中 多个Map集合存储到List集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: List > * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:04 * @throws */ public static List > MapListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List > list = queryRunner.query(connection, sql,new MapListHandler()); DbUtils.closeQuietly(connection); return list; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ColumnListHandler * @Description: 结果集第七种处理方式ColumnListHandler 结果集是指定列的数据,存储到List集合 * 数据类型用List 每列的每个数据类都不同 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:28 * @throws */ public static List ColumnListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result = queryRunner.query(connection, sql, new ColumnListHandler("aname")); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ScalarHandler * @Description: 结果集第八种处理方式ScalarHandler 查询后只有一个结果 * @Params: @param sql * @Return: long * @Author: 伏永正 * @Date: 2017年9月25日下午6:36:16 * @throws */ public static long ScalarHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); long count = queryRunner.query(connection, sql, new ScalarHandler ()); DbUtils.closeQuietly(connection); return count; } catch (SQLException e) { throw new RuntimeException(e); } } } mysql-connector-java-5.1.39-bin.jar mysql-connector-java-5.1.39-bin.jar commons-dbutils-1.6.jar commons-dbutils-1.6.jar database.properties

driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=123456

如何使用DBUtils高效操作数据库并实现长尾词效果?

本文共计2555个文字,预计阅读时间需要11分钟。

如何使用DBUtils高效操作数据库并实现长尾词效果?

通过读取配置文件database.properties获取数据库连接,使用DBUtils工具类操作数据库。

javaimport com.mysql.jdbc.Connection;import java.io.InputStream;import java.sql.SQLException;

public class DatabaseUtil { public Connection getConnection() throws SQLException, ClassNotFoundException { Class.forName(com.mysql.jdbc.Driver); InputStream is=this.getClass().getClassLoader().getResourceAsStream(database.properties); Properties prop=new Properties(); prop.load(is); String url=prop.getProperty(url); String user=prop.getProperty(user); String password=prop.getProperty(password); return (Connection) DriverManager.getConnection(url, user, password); }}

通过读取配置文件database.properties获得数据库连接,使用DBUtils工具类操作数据库

package com.mysql.jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.mysql.test.Admins; /** * @ProjectName: JDBCUtils * @PackageName: com.mysql.jdbc * @ClassName: DBUtils * @Description: 使用DBUtils工具类,对数据库操作 * @Author: 伏永正 * @Date: 2017年9月25日 下午5:47:53 */ public class DBUtils { private static Connection connection; //连接对象 private static String driverClass; //数据库驱动 private static String url; //数据库地址jdbc:mysql://连接主机ip:端口号/数据库名 private static String username; //数据库登录名 private static String password; //数据库密码 static{ try { //读取配置文件 readConfig(); //加载驱动 Class.forName(driverClass); connection=DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: readConfig * @Description: 读取database.properties配置文件,获得连接属性:driverClass url username password * @Return: void * @Author: 伏永正 * @Date: 2017年9月25日下午5:48:26 * @throws */ private static void readConfig() { try { //使用类的加载器 InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("database.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取集合中的键值对 driverClass=properties.getProperty("driverClass"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: SQLUpdate * @Description: 封装SQLUpdate方法,调用QueryRunner类的方法update完成对数据库的:增 删 改功能 * @Params: @param sql 构建的SQL语句,?占位符必不可少 * @Params: @param params 以Object[]数组方式 传递参数 参数个数与sql语句占位符相等 * @Params: @return * @Return: boolean * @Author: 伏永正 * @Date: 2017年9月25日下午6:10:44 * @throws */ public static boolean SQLUpdate(String sql,Object[] params) { try { //创建QueryRunner类对象 QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner类的方法update执行SQL语句,且返回int int row = queryRunner.update(connection, sql, params); //释放连接对象 DbUtils.closeQuietly(connection); if (row == 0) { return false; } else { return true; } } catch (SQLException e) { throw new RuntimeException("SQL语句执行失败"); } } /** * @MethodName: arrayHandler * @Description: 结果集第一种处理方式ArrayHandler 将结果集的第一行数据存储到对象数组中Object[] * @Params: @param sql 查询语句 * @Return: Object[] * @Author: 伏永正 * @Date: 2017年9月25日下午6:29:53 * @throws */ public static Object[] arrayHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的是Object[] Object[] result = queryRunner.query(connection, sql, new ArrayHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(); } } /** * @MethodName: ArrayListHandler * @Description: 结果集的第二种处理方式ArraySetHandler 将结果集的每一行数据,将数据封装到对象数组中Object[] * 多个对象数组存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:30:57 * @throws */ public static List ArrayListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的每一行是Object[],并且存储在List集合中 List result = queryRunner.query(connection, sql, new ArrayListHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanHandler * @Description: 结果集的第三种处理方式BeanHandler 将结集果的第一行数据,封装成JavaBean对象 * @Params: @param sql * @Return: Admins * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:22 * @throws */ public static Admins BeanHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Admins admins = queryRunner.query(connection, sql, new BeanHandler (Admins.class)); DbUtils.closeQuietly(connection); return admins; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanListHandler * @Description: 结果集的第四种处理方式BeanListHandler 将结果集的每一行数据,封装到JavaBean对象 * 多个JavaBean的对象存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:52 * @throws */ public static List BeanListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result =queryRunner.query(connection, sql, new BeanListHandler (Admins.class)); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapHandler * @Description: 结果集的第五种处理方式MapHandler 将结果集的第一行数据,封装到Map集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: Map * @Author: 伏永正 * @Date: 2017年9月25日下午6:33:35 * @throws */ public static Map MapHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Map map = queryRunner.query(connection, sql, new MapHandler()); DbUtils.closeQuietly(connection); return map; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapListHandler * @Description: 结果集的第六种处理方式MapListHandler 将结果集的每一行数据,存储到Map集合中 多个Map集合存储到List集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: List > * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:04 * @throws */ public static List > MapListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List > list = queryRunner.query(connection, sql,new MapListHandler()); DbUtils.closeQuietly(connection); return list; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ColumnListHandler * @Description: 结果集第七种处理方式ColumnListHandler 结果集是指定列的数据,存储到List集合 * 数据类型用List 每列的每个数据类都不同 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:28 * @throws */ public static List ColumnListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result = queryRunner.query(connection, sql, new ColumnListHandler("aname")); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ScalarHandler * @Description: 结果集第八种处理方式ScalarHandler 查询后只有一个结果 * @Params: @param sql * @Return: long * @Author: 伏永正 * @Date: 2017年9月25日下午6:36:16 * @throws */ public static long ScalarHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); long count = queryRunner.query(connection, sql, new ScalarHandler ()); DbUtils.closeQuietly(connection); return count; } catch (SQLException e) { throw new RuntimeException(e); } } } DBUtils.java

package com.mysql.jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.mysql.test.Admins; /** * @ProjectName: JDBCUtils * @PackageName: com.mysql.jdbc * @ClassName: DBUtils * @Description: 使用DBUtils工具类,对数据库操作 * @Author: 伏永正 * @Date: 2017年9月25日 下午5:47:53 */ public class DBUtils { private static Connection connection; //连接对象 private static String driverClass; //数据库驱动 private static String url; //数据库地址jdbc:mysql://连接主机ip:端口号/数据库名 private static String username; //数据库登录名 private static String password; //数据库密码 static{ try { //读取配置文件 readConfig(); //加载驱动 Class.forName(driverClass); connection=DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: readConfig * @Description: 读取database.properties配置文件,获得连接属性:driverClass url username password * @Return: void * @Author: 伏永正 * @Date: 2017年9月25日下午5:48:26 * @throws */ private static void readConfig() { try { //使用类的加载器 InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("database.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取集合中的键值对 driverClass=properties.getProperty("driverClass"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } } /** * @MethodName: SQLUpdate * @Description: 封装SQLUpdate方法,调用QueryRunner类的方法update完成对数据库的:增 删 改功能 * @Params: @param sql 构建的SQL语句,?占位符必不可少 * @Params: @param params 以Object[]数组方式 传递参数 参数个数与sql语句占位符相等 * @Params: @return * @Return: boolean * @Author: 伏永正 * @Date: 2017年9月25日下午6:10:44 * @throws */ public static boolean SQLUpdate(String sql,Object[] params) { try { //创建QueryRunner类对象 QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner类的方法update执行SQL语句,且返回int int row = queryRunner.update(connection, sql, params); //释放连接对象 DbUtils.closeQuietly(connection); if (row == 0) { return false; } else { return true; } } catch (SQLException e) { throw new RuntimeException("SQL语句执行失败"); } } /** * @MethodName: arrayHandler * @Description: 结果集第一种处理方式ArrayHandler 将结果集的第一行数据存储到对象数组中Object[] * @Params: @param sql 查询语句 * @Return: Object[] * @Author: 伏永正 * @Date: 2017年9月25日下午6:29:53 * @throws */ public static Object[] arrayHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的是Object[] Object[] result = queryRunner.query(connection, sql, new ArrayHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(); } } /** * @MethodName: ArrayListHandler * @Description: 结果集的第二种处理方式ArraySetHandler 将结果集的每一行数据,将数据封装到对象数组中Object[] * 多个对象数组存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:30:57 * @throws */ public static List ArrayListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); //调用QueryRunner的quert方法执行查询,返回的每一行是Object[],并且存储在List集合中 List result = queryRunner.query(connection, sql, new ArrayListHandler()); //释放资源 DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanHandler * @Description: 结果集的第三种处理方式BeanHandler 将结集果的第一行数据,封装成JavaBean对象 * @Params: @param sql * @Return: Admins * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:22 * @throws */ public static Admins BeanHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Admins admins = queryRunner.query(connection, sql, new BeanHandler (Admins.class)); DbUtils.closeQuietly(connection); return admins; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: BeanListHandler * @Description: 结果集的第四种处理方式BeanListHandler 将结果集的每一行数据,封装到JavaBean对象 * 多个JavaBean的对象存储到List集合 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:32:52 * @throws */ public static List BeanListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result =queryRunner.query(connection, sql, new BeanListHandler (Admins.class)); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapHandler * @Description: 结果集的第五种处理方式MapHandler 将结果集的第一行数据,封装到Map集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: Map * @Author: 伏永正 * @Date: 2017年9月25日下午6:33:35 * @throws */ public static Map MapHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); Map map = queryRunner.query(connection, sql, new MapHandler()); DbUtils.closeQuietly(connection); return map; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: MapListHandler * @Description: 结果集的第六种处理方式MapListHandler 将结果集的每一行数据,存储到Map集合中 多个Map集合存储到List集合中 * Map<键,值> 键:列名 值:列的数据 * @Params: @param sql * @Params: @return * @Return: List > * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:04 * @throws */ public static List > MapListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List > list = queryRunner.query(connection, sql,new MapListHandler()); DbUtils.closeQuietly(connection); return list; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ColumnListHandler * @Description: 结果集第七种处理方式ColumnListHandler 结果集是指定列的数据,存储到List集合 * 数据类型用List 每列的每个数据类都不同 * @Params: @param sql * @Return: List * @Author: 伏永正 * @Date: 2017年9月25日下午6:34:28 * @throws */ public static List ColumnListHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); List result = queryRunner.query(connection, sql, new ColumnListHandler("aname")); DbUtils.closeQuietly(connection); return result; } catch (SQLException e) { throw new RuntimeException(e); } } /** * @MethodName: ScalarHandler * @Description: 结果集第八种处理方式ScalarHandler 查询后只有一个结果 * @Params: @param sql * @Return: long * @Author: 伏永正 * @Date: 2017年9月25日下午6:36:16 * @throws */ public static long ScalarHandler(String sql) { try { QueryRunner queryRunner = new QueryRunner(); long count = queryRunner.query(connection, sql, new ScalarHandler ()); DbUtils.closeQuietly(connection); return count; } catch (SQLException e) { throw new RuntimeException(e); } } } mysql-connector-java-5.1.39-bin.jar mysql-connector-java-5.1.39-bin.jar commons-dbutils-1.6.jar commons-dbutils-1.6.jar database.properties

driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=123456

如何使用DBUtils高效操作数据库并实现长尾词效果?