如何使用DBUtils高效操作数据库并实现长尾词效果?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2555个文字,预计阅读时间需要11分钟。
通过读取配置文件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
本文共计2555个文字,预计阅读时间需要11分钟。
通过读取配置文件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

