package chelian.util;
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.util.ArrayList;
import java.util.List;
public class JDBCUtil {
private static final String Url = "jdbc:mysql://localhost:3306/db_address?characterEncoding=UTF-8";
private static final String User = "root";
private static final String Password = "12344";
private static final String Driver = "com.mysql.jdbc.Driver";
private static Connection con = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
public static int IDentity;// 新增操作时的id
static {
try {
Class.forName(Driver);// 注册驱动
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(Url, User, Password);
}
/**
* 更新或者保存数据
*
* @param sql
* @return
*/
public static boolean updateOrSave(String sqlStr, List<Object> values) {
System.out.println("JDBCUtil SQL: " + sqlStr);
try {
con = getConnection();
ps = con.prepareStatement(sqlStr);
int size = values.size();
for (int i = 0; i < size; i++) {
ps.setObject((i + 1), values.get(i));
}
int result = ps.executeUpdate();
ps = con.prepareStatement("select @@identity");
rs = ps.executeQuery();
if (rs.next()) {
JDBCUtil.IDentity = (Integer) rs.getInt(1);
}
return result > 0 ? true : false;
} catch (Exception e) {
e.printStackTrace();
} finally {
free(rs, ps, con);
}
return false;
}
public static boolean delete(String sqlStr, int id) {
System.out.println("JDBCUtil SQL: " + sqlStr);
try {
con = getConnection();
ps = con.prepareStatement(sqlStr);
ps.setInt(1, id);
int result = ps.executeUpdate();
return result > 0 ? true : false;
} catch (Exception e) {
e.printStackTrace();
} finally {
free(null, ps, con);
}
return false;
}
/**
* 释放数据库的连接等资源
*
* @param rs
* @param ps
* @param conn
*/
public static void free(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 这个函数查询出来的将查出来的所有列的值放在list里面 每一条list记录有很多Object,用数组存储
*
* @param sqlStr
* @param values
* 参数值
* @return
*/
public static List<Object[]> queryObjectList(String sqlStr,
List<Object> values) {
System.out.println("JDBCUtil SQL: " + sqlStr);
List<Object[]> objs = new ArrayList<Object[]>();
try {
con = getConnection();
ps = con.prepareStatement(sqlStr);
int size = values.size();
for (int i = 0; i < size; i++) {
ps.setObject((i + 1), values.get(i));
}
rs = ps.executeQuery();
ResultSetMetaData rsd = null;
while (rs.next()) {
rsd = rs.getMetaData();
int cols = rsd.getColumnCount();// 所有列数
Object[] obj = new Object[cols];
for (int i = 0; i < cols; i++) {
obj[i] = rs.getObject(i + 1);
}
objs.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
free(rs, ps, con);
}
return objs;
}
/**
* 这个函数查询出来的将查出来的所有列的值放在list里面 每一条list记录有很多Object,用数组存储
*
* @param sqlStr
* @param value 一个参数
* @return
*/
public static List<Object[]> queryObjectList(String sqlStr, Object value) {
List<Object> values = new ArrayList<Object>();
values.add(value);
return queryObjectList(sqlStr, values);
}
/**
* 这个函数查询出来的将查出来的所有列的值放在list里面 每一条list记录有很多Object,用数组存储
*
* @param sqlStr
* @return
*/
public static List<Object[]> queryObjectList(String sqlStr) {
List<Object> values = new ArrayList<Object>();
return queryObjectList(sqlStr, values);
}
/**
* 查询一个对象,而这个对象的所有属性值存放在数组中
*
* @param sqlStr
* @param values 参数值
* @return
*/
public static Object[] queryObject(String sqlStr, List<Object> values) {
System.out.println("JDBCUtil SQL: " + sqlStr);
Object[] objs = null;
try {
con = getConnection();
ps = con.prepareStatement(sqlStr);
int size = values.size();
for (int i = 0; i < size; i++) {
ps.setObject((i + 1), values.get(i));
}
rs = ps.executeQuery();
ResultSetMetaData rsd = null;
if (rs.next()) {
rsd = rs.getMetaData();
int cols = rsd.getColumnCount();// 所有列数
objs = new Object[cols];
for (int i = 0; i < cols; i++) {
objs[i] = rs.getObject(i + 1);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
free(rs, ps, con);
}
return objs;
}
/**
* 查询一个对象,而这个对象的所有属性值存放在数组中
*
* @param sqlStr
* @param value 一个参数值
* @return
*/
public static Object[] queryObject(String sqlStr, Object value) {
List<Object> values = new ArrayList<Object>();
values.add(value);
return queryObject(sqlStr, values);
}
/**
* 查询一个对象,而这个对象的所有属性值存放在数组中
*
* @param sqlStr
* @return
*/
public static Object[] queryObject(String sqlStr) {
List<Object> values = new ArrayList<Object>();
return queryObject(sqlStr, values);
}
/* public static void main(String[] args) {
List<Object[]> list = queryObjectList("select * from user", new ArrayList<Object>());
//List<Object[]> list = queryObjectList("select u.*, q.name from user u, question q where q.userId=u.id", new ArrayList<Object>());
for (Object[] o : list) {
for (Object oo : o) {
System.out.print(oo);
}
System.out.println();
}
}*/
}