import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class DBUtil {
/**
* 取得一个数据库连接
*
* @return
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public Connection getConnection() throws SQLException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
DBProperty pro = new DBProperty();
pro.setFilePath("/DB.properties");
Connection conn = null;
// 加载数据库驱动类
Class.forName(pro.getClassName()).newInstance();
// 数据库连接URL
String url = pro.getUrl();
// 数据库用户名
String user = pro.getName();
// 数据库密码
String password = pro.getPassword();
// 根据数据库参数取得一个数据库连接
conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* 根据传入的SQL语句返回取出的结果
*
* @param sql
* @return
* @throws Exception
*/
public ArrayList select(String sql) throws Exception {
Connection conn = null;
ArrayList result = new ArrayList();
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
int i = 0;
while (rs.next() != false) {
ArrayList row = new ArrayList();
for (i = 1; i <= cols; ++i) {
if (rs.getString(i) == null) {
row.add("");
} else {
row.add(rs.getString(i));
}
}
result.add(row);
}
} catch (SQLException e) {
throw new Exception("select data exception:" + e.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
throw new Exception("rs close exception: " + e.getMessage());
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
throw new Exception("stmt close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close exception: " + e.getMessage());
}
}
return result;
}
/**
* 根据传入的SQL语句向数据库增加一条记录
*
* @param sql
* @throws Exception
*/
public void insert(String sql) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("insert data exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("connection close exception: " + e.getMessage());
}
}
/**
* 根据传入的SQL语句更新数据库记录
*
* @param sql
* @throws Exception
*/
public void update(String sql) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("update exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("connection close exception: " + e.getMessage());
}
}
/**
* 根据传入的SQL语句删除一条数据库记录
*
* @param sql
* @throws Exception
*/
public void delete(String sql) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("delete data exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("connection close exception: " + e.getMessage());
}
}
}
//使用
@SuppressWarnings("unchecked")
public static ArrayList getAlarmByCond() {
try {
DBUtil db = new DBUtil();
String sql = "select * from test ";
ArrayList result = new ArrayList();
try {
result = db.select(sql);
} catch (Exception e) {
e.printStackTrace();
}
return result;
} catch (RuntimeException re) {
throw re;
}
}
分享到:
相关推荐
JAVA数据库操作工具类,用JAVA实现了对SQL Server数据库的增删改基本操作,对工具类的每个方法使用JUnit来测试
本资源是java开发的常用工具类,包括01.DTD约束文档,02.Schema约束文档,c3p0,commons-dbutils-1.6,commons-io-2.4,commons-pool-1.5.6,mysql-connector-java-5.1.37
数据库工具类,采用的DBCP的连接池。并且采用了Google公司的工程师Bob Lee写的新的懒汉单例模式来构造连接工厂。并将获取到的数据库连接绑定到当前线程中。这样可以有效的防止申请连接的滥用。
Java 连接数据库工具类
* 获得与数据库的连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(class...
2、该工具类可以将数据库表列字段转化为对应的Java实体类字段。生成的实体类字段格式清晰易读,且符合Java命名规范。通过使用该工具类,可以大大提高开发效率,节约时间成本。 3、该工具类使用非常简单。只需要...
自己封装的数据库工具类,实现自动插入和更新,自动查询封装对象。数据源使用jndi,支持读写分离。
NULL 博文链接:https://shulinshulinzi.iteye.com/blog/2366416
JDBC连接数据库工具类 JDBC 连接数据库 ORACLE SQLSERVER MYSQL ORACLE 已测 OK 欢迎大家反馈 我的微博是: http://weibo.com/namenode
java工具类 1、Oracle8/8i/9i数据库(thin模式) 2、DB2数据库 3、SQL Server7.0/2000数据库 4、Sybase数据库 5、Informix数据库 6、MySQL数据库 7、PostgreSQL数据库 8、access数据库直连用ODBC的
JDBC连接数据库工具类 JDBC 连接数据库 ORACLE SQLSERVER MYSQL ORACLE 已测 OK 欢迎大家反馈
java提取数据库表字段信息的工具类.docx
封装了java连接Oracle的工具类
java连接mysql工具类,连接成功后,可完成对数据的更新、查询操作
[工具类] 文件操作工具类 .java.txt [工具类] 序列化保存为XML文件的工具类 .java.txt [工具类] 一个压缩工具类.java.txt [工具类] 用java编写简单UDP网络通信程序 .java.txt [工具类] 中文验证.jsp.txt [工具类] ...
[工具类] 文件操作工具类 .java.txt [工具类] 序列化保存为XML文件的工具类 .java.txt [工具类] 一个压缩工具类.java.txt [工具类] 用java编写简单UDP网络通信程序 .java.txt [工具类] 中文验证.jsp.txt [工具类] ...
该资源包含操作sqlite数据库的jar包和工具类。只需将jar包导入你的工程,将工具类考入你的工程就可操作指定sqlite数据库,无需配置环境。
Java JDBC封装类,带增删改查例子,支持oracle,MySql,hsqldb 等,支持事务,返回数据格式 支持二维数组,MAP格式,以及javabean对象。有利于初学者DbDemo.java为demo,Connect为jdbc封装类,可以作为项目共通类使用。
java 大数据导入工具类 jdbc 大数据导入数据库工具类 java
http://blog.csdn.net/qqylc0011/article/details/44626729 是我的博客一个附件,包含自己封装的一个JAVA操作数据库类(mysql)。