自己开发的Dbutile包(源码2)
来源:互联网 发布:pc淘宝规蜜入口在哪里 编辑:程序博客网 时间:2024/05/22 15:38
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
*
*
*
* @author yenairong
*/
public class DBUtil {
//使用不用的数据库请加载不同的驱动
private static final String DBDRIVER = "com.mysql.jdbc.Driver";// 驱动类类名
private static final String DBNAME = "";// 数据库名
//数据库连接地址
private static final String DBURL = "jdbc:mysql://localhost:1521/" + DBNAME;// 连接URL
private static final String DBUSER = "root";// 数据库用户名
private static final String DBPASSWORD = "root";// 数据库密码
private static Connection conn = null;
private static PreparedStatement psmt = null;
private static ResultSet rs = null;
// 获取数据库连接
public static Connection getConnection() {
try {
Class.forName(DBDRIVER);// 注册驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 获得连接对象
} catch (ClassNotFoundException e) {// 捕获驱动类无法找到异常
e.printStackTrace();
} catch (SQLException e) {// 捕获SQL异常
e.printStackTrace();
}
return conn;
}
// 查询数据
public ResultSet select(String sql) throws Exception {
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery(sql);
return rs;
} catch (SQLException sqle) {
throw new SQLException("select data Exception: "
+ sqle.getMessage());
} catch (Exception e) {
throw new Exception("System error: " + e.getMessage());
}
}
// 插入数据
public int insert(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
num = psmt.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("insert data Exception: "
+ sqle.getMessage());
} finally {
try {
if (psmt != null) {
psmt.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close exception: " + e.getMessage());
}
}
return num;
}
// 删除数据
public int delete(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
num = psmt.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("delete data Exception: "
+ sqle.getMessage());
} finally {
try {
if (psmt != null) {
psmt.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Exception: " + e.getMessage());
}
}
return num;
}
// 修改数据
public int update(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
num = psmt.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("update data Exception: "
+ sqle.getMessage());
} finally {
try {
if (psmt != null) {
psmt.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Excepiton: " + e.getMessage());
}
}
return num;
}
}
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
*
*
*
* @author yenairong
*/
public class DBUtil {
//使用不用的数据库请加载不同的驱动
private static final String DBDRIVER = "com.mysql.jdbc.Driver";// 驱动类类名
private static final String DBNAME = "";// 数据库名
//数据库连接地址
private static final String DBURL = "jdbc:mysql://localhost:1521/" + DBNAME;// 连接URL
private static final String DBUSER = "root";// 数据库用户名
private static final String DBPASSWORD = "root";// 数据库密码
private static Connection conn = null;
private static PreparedStatement psmt = null;
private static ResultSet rs = null;
// 获取数据库连接
public static Connection getConnection() {
try {
Class.forName(DBDRIVER);// 注册驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 获得连接对象
} catch (ClassNotFoundException e) {// 捕获驱动类无法找到异常
e.printStackTrace();
} catch (SQLException e) {// 捕获SQL异常
e.printStackTrace();
}
return conn;
}
// 查询数据
public ResultSet select(String sql) throws Exception {
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery(sql);
return rs;
} catch (SQLException sqle) {
throw new SQLException("select data Exception: "
+ sqle.getMessage());
} catch (Exception e) {
throw new Exception("System error: " + e.getMessage());
}
}
// 插入数据
public int insert(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
num = psmt.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("insert data Exception: "
+ sqle.getMessage());
} finally {
try {
if (psmt != null) {
psmt.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close exception: " + e.getMessage());
}
}
return num;
}
// 删除数据
public int delete(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
num = psmt.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("delete data Exception: "
+ sqle.getMessage());
} finally {
try {
if (psmt != null) {
psmt.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Exception: " + e.getMessage());
}
}
return num;
}
// 修改数据
public int update(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
num = psmt.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("update data Exception: "
+ sqle.getMessage());
} finally {
try {
if (psmt != null) {
psmt.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Excepiton: " + e.getMessage());
}
}
return num;
}
}
0 0
- 自己开发的Dbutile包(源码2)
- 自己手工打造的DBUTIL包(源码1)
- 怎么给自己的包添加源码
- 如何开发自己的npm包管理工具
- 新手开发自己的npm包
- Maven在默认的源码包的基础上增加自己定义的源码包
- centos6.5下asterisk 13.3.2安装(分别从自己制作的rpm包和源码进行安装),支持mysql数据库
- 如何将自己开发的包打进pybundle
- 如何开发自己的HttpServer-NanoHttpd源码解读
- 如何开发自己的HttpServer-NanoHttpd源码解读
- Android系统编译总结(使用smart210开发板所提供的源码包)
- 源码里开发的应用程序引用第三方jar包
- myeclipse中无法导入servlet开发源码包的解决方法
- 自己写的归并排序(源码)
- 如何在tcp源码中分析丢包,加入自己的函数
- eclipse jar包关联源码(android 开发)
- Android二维码、条形码开发包(有源码)
- 源码包的安装
- nefu 120——梅森素数
- ARM中,总线如何连接?
- extern C的作用详解
- swift菜鸟入门视频教程-01-基础部分
- 自己手工打造的DBUTIL包(源码1)
- 自己开发的Dbutile包(源码2)
- [LeetCode4]Add Binary
- 苹果iPad Air 2机模曝光:加入指纹传感器
- CCNP路由实验之七 动态路由之BGP
- Java常用类之Calendar类——诺诺"涂鸦"记忆
- Java学习笔记二
- Swift学习笔记(一)
- SharpMap增加标注图层LabelLayer及标注样式控制
- 关于tomcat常遇到的问题