代码中关闭mysql_MySql 的联接 关闭等_mysql

mysql 的连接 关闭等

package com.zx.util;

import java.sql.*;

import java.util.*;

public class MySql {

private static String url = "jdbc:mysql://localhost:3306/library2";

private static String user = "root";

private static String password = "199212";

private MySql() {

}

static {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static Connection getCon() {

try {

return DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

public static void free(ResultSet re, Statement stat, Connection con) {

try {

if (re != null)

re.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (stat != null)

stat.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

if (con != null)

con.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

//读数据库,返回int 型

public static int readD(String sql, Object[] params) {

Connection con = null;

PreparedStatement ps = null;

ResultSet re = null;

int rows = 0;

try {

con = Tool.getCon();

ps = (PreparedStatement) con.prepareStatement(sql);

if (params != null) {

for (int i = 0; i < params.length; i++) {

ps.setObject(i + 1, params[i]);

}

}

re = ps.executeQuery();

ResultSetMetaData rsmd = (ResultSetMetaData) re.getMetaData();

int count = rsmd.getColumnCount();

while (re.next()) {

rows++;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

Tool.free(re, ps, con);

}

return rows;

}

public int writeDB(String sql) {

return writeDB(sql, null);

}

//写数据库方法

public static int writeDB(String sql, Object[] params) {

Connection con = null;

PreparedStatement ps = null;

ResultSet re = null;

int row = 0;

try {

con = MySql.getCon();

ps = (PreparedStatement) con.prepareStatement(sql);

if (params != null) {

for (int i = 0; i < params.length; i++) {

ps.setObject(i + 1, params[i]);

}

}

row = ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} finally {

MySql.free(re, ps, con);

}

return row;

}

//读数据库方法

public static List> readDB(String sql, Object[] params) {

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

ArrayList> list = new ArrayList>();

// map<>里,string 是键,就是列名,object是值,是集合,一个map得到一行记录

try {

con = Tool.getCon();

ps = con.prepareStatement(sql);

// sql语句参数化

if (params != null) {

for (int i = 0; i < params.length; i++) {

ps.setObject(i + 1, params[i]);

}

}

rs = ps.executeQuery();

ResultSetMetaData rsmd = rs.getMetaData();

int count = rsmd.getColumnCount();// 一共获得几列

String[] columName = new String[count];

for (int i = 0; i < count; i++) {

columName[i] = rsmd.getColumnName(i + 1);// 获得列名

}

while (rs.next()) {

HashMap map = new HashMap();

for (int i = 0; i < count; i++) {

map.put(columName[i], rs.getObject(i + 1));

}

list.add(map);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

Tool.free(rs, ps, con);

}

return list;

}

}

欢迎大家阅读《MySql 的联接 关闭等_mysql》,跪求各位点评,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值