JDBC工具类(DButil)
【摘要】
package lesson11;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Prepared...
package lesson11;
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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DBUtil {
private Connection conn = null;
private Statement st = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private String url = "jdbc:mysql://localhost:3306/filmcenter_db";
private String username = "root";
private String psw = "root";
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
//1.加载驱动
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败!!");
e.printStackTrace();
}
}
//2.获取连接
public Connection getConn(){
try {
conn = DriverManager.getConnection(url,username,psw);
} catch (SQLException e) {
System.out.println("获取连接失败!!");
e.printStackTrace();
}
return conn;
}
//3.提供更新方法
public int update(String sql, Object[] objs){
conn = getConn();
try {
ps = conn.prepareStatement(sql);
for(int i = 0 ; i < objs.length ; i ++){
ps.setObject(i + 1, objs[i]);
}
return ps.executeUpdate();
} catch (SQLException e) {
System.out.println("更新失败!!");
e.printStackTrace();
return -1;
}finally{
release();
}
}
//4.提供查询方法
public List<Map<String,Object>> query(String sql, Object[] objs){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
try {
ps = getConn().prepareStatement(sql);
for(int i = 0 ; i < objs.length ; i ++){
ps.setObject(i + 1, objs[i]);
}
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
Map<String,Object> map = new HashMap<String,Object>();
for(int i = 0 ; i < rsmd.getColumnCount() ; i ++){
String key = rsmd.getColumnName(i + 1);
Object value = rs.getObject(key);
map.put(key, value);
}
list.add(map);
}
return list;
} catch (SQLException e) {
System.out.println("查询操作失败!");
e.printStackTrace();
return list;
}finally{
release();
}
}
//5.批量更新
public void batch(String[] sqls){
try {
conn = getConn();
conn.setAutoCommit(false);
st = conn.createStatement();
for(int i = 0; i < sqls.length ; i ++){
st.addBatch(sqls[i]);
}
st.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
release();
}
}
//释放资源
public void release(){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
System.out.println("关闭结果集对象出现异常!");
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
System.out.println("关闭预处理语句对象出现异常!");
e.printStackTrace();
}
}
if(st != null){
try {
st.close();
} catch (SQLException e) {
System.out.println("关闭语句对象出现异常!");
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭连接对象出现异常!");
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
}
文章来源: jianguo.blog.csdn.net,作者:坚果前端の博客,版权归原作者所有,如需转载,请联系作者。
原文链接:jianguo.blog.csdn.net/article/details/106889464
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)