MYSQL数据访问工具类DBUtils
来源:互联网 发布:大数据基金净值 编辑:程序博客网 时间:2024/05/24 01:40
package com.msun.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/learnjsp";
private static String user = "root";
private static String pwd = "";
private static Connection conn;
private static PreparedStatement pstmt;
private static ResultSet rs;
// 打开数据库连接
public static Connection getCon() {
if (conn == null) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
//查询数据库
public static ResultSet query(String sql){
conn = getCon();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//增加信息
public static boolean add(String sql){
boolean flag = false;
try{
conn = getCon();
pstmt = conn.prepareStatement(sql);
int count = pstmt.executeUpdate();
if(count > 0){
flag = true;
}
}catch(SQLException e){
e.printStackTrace();
}
return flag;
}
//更新数据库信息
public static boolean update(String sql){
boolean flag = false;
try{
conn = getCon();
pstmt = conn.prepareStatement(sql);
int count = pstmt.executeUpdate();
if(count > 0){
flag = true;
}
}catch(SQLException e){
e.printStackTrace();
}
return flag;
}
//删除一条数据库信息
public static boolean delete(String sql){
boolean flag = false;
try{
conn = getCon();
pstmt = conn.prepareStatement(sql);
int count = pstmt.executeUpdate();
if(count > 0){
flag = true;
}
}catch(SQLException e){
e.printStackTrace();
}
return flag;
}
// 关闭数据库
public static void closeConn() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null){
try{
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
阅读全文
0 0
- MYSQL数据访问工具类DBUtils
- 测试 DBUtils 工具类
- JDBC-DBUtils工具类
- DBUtils工具类
- DBUtils数据库工具类
- dbutils工具类:QueryRunner
- 数据访问工具类
- 笔记--元数据和dbutils工具
- JDBC : 使用DBUtils 工具类
- 【Android】原生DBUtils工具类
- dbutils工具
- dbUtils工具
- 使用DBUtils第三方工具类,实现MySQL数据库的增删改查
- JDBC-DBUtils开源工具类库(简化查询封装数据过程)
- C3P0+反射注释+dbutils搭建数据访问层
- DButils工具类库的使用
- 基于DBUtils的数据库工具类实现
- 开源JDBC工具类之DbUtils解析
- Oracle问题——排名函数(rank与dense_rank)
- 服务器使用环形缓冲区处理接收数据
- JAVA存储数据的五个区域
- Android Studio转换Project成AAR
- @AutoWired与@Resource转载自<叶德华博客>
- MYSQL数据访问工具类DBUtils
- Spring Cloud 微服务框架技术标准分析
- jquery验证两次密码是否一致并阻止表单提交
- java多线程
- 页面跳转和定时刷新
- 算法设计期末作业04-8.12
- python之路——python简介
- myeclipse大小写转换键
- one hot 编码及数据归一化