码迷,mamicode.com
首页 > 数据库 > 详细

jdbc的优化

时间:2016-09-01 22:52:38      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

jdbc的优化
把经常用的属性,代码全部封装起来,不用每次用的时候都创建,比如 下面这些动作不需要重复创建,只需封装起来即可提高的效率
把属性也封装起来当作配置信息,用时读取配置文件文件即可
package com.bjsxt.util;

/**
* 存储着所有的常量信息
*
* @author Administrator
*
*/
public class SxtConstants {
//数据库的配置信息
public static final String DATABASE_CLASSNAME = "oracle.jdbc.driver.OracleDriver";
public static final String DATABASE_URL = "jdbc:oracle:thin:@192.168.1.253:1521:orcl";
public static final String DATABASE_USER = "scott";
public static final String DATABASE_PASSWORD = "tiger";
}


//获取连接
Connection connection = SxtDBUtil.createConnection();
//获取发送器
Statement statement = SxtDBUtil.createStatment(connection);
//发送SQL语句,获取结果集合
ResultSet resultSet = statement.executeQuery(sql);
//遍历结果集合

 


完整代码

package com.bjsxt.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
*
* 配置文件必须放在src根目录
*
* @author Administrator
*
*/
public final class SxtDBUtil {

//数据库的配置信息
private static String className = "";
private static String url = "";
private static String user = "";
private static String password = "";

static {
try {
//开始解析配置文件
SAXReader saxReader = new SAXReader();
//以流的方式获取获取配置文件
InputStream inputStream = SxtDBUtil.class.getClassLoader().getResourceAsStream("SxtDBUtil.xml");
//开始解析配置文件
Document document = saxReader.read(inputStream);
//获取根节点
Element rootElement = document.getRootElement();
//获取要选择数据库的类型
String databaseType = rootElement.elementText("database-type");
//判断数据库类型是否为空
if (databaseType != null) {
//遍历出数据库的配置信息
List<Element> elements = rootElement.elements("database");
for (Element element : elements) {
//判断数据库类型是否一致
if (databaseType.equals(element.attributeValue("type"))) {
//获取当前元素所有的子元素
className = element.elementText("className");
url = element.elementText("url");
user = element.elementText("user");
password = element.elementText("password");
}
}
//使用静态代码块加载驱动
Class.forName(className);
} else {
System.err.println("您的配置文件数据库类型【database-type】有误,请重新配置");
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 创建JDBC连接
* @return
*/
public static Connection createConnection() {
return createConnection(true);
}

/**
* 事务手动提交还是自动提交
* @param isAutoCommit
* @return
*/
public static Connection createConnection(boolean autoCommit) {
//声明连接
Connection connection = null;
try {
//获取连接
connection = DriverManager.getConnection(url, user, password);
//手动或者自动提交事务
connection.setAutoCommit(autoCommit);
} catch (SQLException e) {
System.err.println("您的数据库详细配置有误url【" + url + "】user【" + user + "】password【" + password + "】");
e.printStackTrace();
}
//返回连接
return connection;
}

/**
* 获取发送器
* @param connection
* @return
*/
public static Statement createStatment(Connection connection) {
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return statement;
}

/**
* 获取预处理发送器
* @param connection
* @param sql
* @return
*/
public static PreparedStatement createPreparedStatement(Connection connection, CharSequence sql) {
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql.toString());
} catch (SQLException e) {
e.printStackTrace();
}
return preparedStatement;
}

/**
* 关闭连接
* @param connection
*/
private static void closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 关闭发送器
* @param statement
*/
private static void closeStatement(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 关闭结果集
* @param resultSet
*/
private static void closeResultSet(ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 关闭所有的连接
* @param connection
* @param statement
* @param resultSet
*/
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
closeResultSet(resultSet);
closeStatement(statement);
closeConnection(connection);
}
}

jdbc的优化

标签:

原文地址:http://www.cnblogs.com/lgf428/p/5831537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!