341 lines
10 KiB
Java
341 lines
10 KiB
Java
package cn.citycraft.CTZLoginServer.utils;
|
||
|
||
/*
|
||
* 数据库连接、选择、更新、删除演示
|
||
*/
|
||
// import java.sql.*;
|
||
import java.sql.Connection;
|
||
import java.sql.DatabaseMetaData;
|
||
import java.sql.DriverManager;
|
||
import java.sql.PreparedStatement;
|
||
import java.sql.ResultSet;
|
||
import java.sql.SQLException;
|
||
import java.sql.Statement;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.Iterator;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Map.Entry;
|
||
|
||
/**
|
||
* @author 蒋天蓓
|
||
* 2015年7月14日下午3:25:06
|
||
* 数据库操作类
|
||
*/
|
||
public class MySqlHelper {
|
||
|
||
// ///////////////////////////////////////———–>>>数据成员 and 构造函数
|
||
private Connection dbconn;
|
||
private Statement dbstate;
|
||
private ResultSet dbresult;
|
||
|
||
protected String username;
|
||
protected String password;
|
||
protected String url;
|
||
|
||
private String driverName = "com.mysql.jdbc.Driver";
|
||
|
||
public MySqlHelper() {
|
||
dbconn = null;
|
||
dbstate = null;
|
||
dbresult = null;
|
||
}
|
||
|
||
public void print(String str)// 简化输出
|
||
{
|
||
System.out.println(str);
|
||
}
|
||
|
||
/**
|
||
* 连接MySql数据库
|
||
*
|
||
* @param host
|
||
* @param port
|
||
* @param dbaName
|
||
* @param usName
|
||
* @param psw
|
||
* @return bool值,连接成功返回真,失败返回假
|
||
*/
|
||
public MySqlHelper(String host, String port, String dbaName, String usName, String password) {
|
||
try {
|
||
Class.forName(driverName).newInstance();
|
||
} catch (Exception e) {
|
||
}// "org.gjt.mm.mysql.Driver"两个驱动都可以用
|
||
String dbHost = host;// 数据库的一些信息
|
||
String dbPort = port;
|
||
String dbName = dbaName;
|
||
String Encode = "?&useUnicode=true&characterEncoding=utf-8"; // 解决MySql中文问题,要连续写不能空格
|
||
this.username = usName;
|
||
this.password = password;
|
||
this.url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + Encode;
|
||
}// end boolean dbConnection(…)
|
||
|
||
public boolean dbConnection() {
|
||
try {
|
||
dbconn = DriverManager.getConnection(url, username, password);
|
||
return true;
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());// 得到出错信息
|
||
print("登录URL: " + url); // 发生错误时,将连接数据库信息打印出来
|
||
print("登录账户: " + username);
|
||
print("登录密码: " + password);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public boolean isTableExists(final String table) {
|
||
try {
|
||
dbConnection();
|
||
final DatabaseMetaData dbm = dbconn.getMetaData();
|
||
final ResultSet tables = dbm.getTables(null, null, table, null);
|
||
return tables.next();
|
||
} catch (final SQLException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public boolean createTables(String tableName, HashMap<String, String> fields, String Conditions) {
|
||
dbConnection();
|
||
String kv = "";
|
||
for (Entry<String, String> kvs : fields.entrySet()) {
|
||
kv += "`" + kvs.getKey() + "` " + kvs.getValue() + ", ";
|
||
}
|
||
if (kv.length() == 0)
|
||
return false;
|
||
kv = kv.substring(0, kv.length() - 2);// 根据String的索引提取子串
|
||
String sql = "CREATE TABLE `" + tableName + "` ( " + kv + (Conditions == "" ? "" : " , " + Conditions)
|
||
+ " ) ENGINE = InnoDB DEFAULT CHARSET=UTF8";
|
||
try {
|
||
PreparedStatement state = dbconn.prepareStatement(sql);
|
||
state.executeUpdate();
|
||
state.close();
|
||
return true;
|
||
} catch (final Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 判断数据库某个值是否存在!
|
||
*
|
||
* @param tableName
|
||
* 数据库表名
|
||
* @param fieles
|
||
* 字段名
|
||
* @param selCondition
|
||
* 选择条件
|
||
* @return 首个符合条件的结果
|
||
*/
|
||
public boolean dbExist(String tableName, HashMap<String, String> selConditions) {
|
||
dbConnection();
|
||
String selCondition = "";
|
||
if (selConditions != null && !selConditions.isEmpty()) {
|
||
for (Entry<String, String> kvs : selConditions.entrySet()) {
|
||
selCondition += kvs.getKey() + "='" + kvs.getValue() + "', ";
|
||
}
|
||
selCondition = " WHERE " + selCondition.substring(0, selCondition.length() - 2);// 根据String的索引提取子串
|
||
}
|
||
String sql = "SELECT * FROM " + tableName + selCondition;
|
||
try {
|
||
dbstate = dbconn.createStatement();
|
||
dbresult = dbstate.executeQuery(sql);
|
||
return dbresult.next();
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 对数据库表进行选择操作!
|
||
*
|
||
* @param tableName
|
||
* 数据库表名
|
||
* @param fieles
|
||
* 字段名
|
||
* @param selCondition
|
||
* 选择条件
|
||
* @return 首个符合条件的结果
|
||
*/
|
||
public String dbSelectFirst(String tableName, String fields, HashMap<String, String> selConditions) {
|
||
dbConnection();
|
||
String selFieldsTem = fields;
|
||
String selCondition = "";
|
||
if (selConditions != null && !selConditions.isEmpty()) {
|
||
for (Entry<String, String> kvs : selConditions.entrySet()) {
|
||
selCondition += kvs.getKey() + "='" + kvs.getValue() + "', ";
|
||
}
|
||
selCondition = " WHERE " + selCondition.substring(0, selCondition.length() - 2);// 根据String的索引提取子串
|
||
}
|
||
String sql = "SELECT " + selFieldsTem + " FROM " + tableName + selCondition + " limit 1";
|
||
try {
|
||
dbstate = dbconn.createStatement();
|
||
dbresult = dbstate.executeQuery(sql);
|
||
if (dbresult.next()) {
|
||
return dbresult.getString(fields);
|
||
}
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 对数据库表进行选择操作!
|
||
*
|
||
* @param tableName
|
||
* 数据库表名
|
||
* @param fieles
|
||
* 字段名
|
||
* @param selCondition
|
||
* 选择条件
|
||
* @return 一个含有map的List(列表)
|
||
*/
|
||
@SuppressWarnings({
|
||
"rawtypes",
|
||
"unchecked"
|
||
})
|
||
public List dbSelect(String tableName, List<String> fields, String selCondition) {
|
||
dbConnection();
|
||
List mapInList = new ArrayList();
|
||
String selFields = "";
|
||
for (int i = 0; i < fields.size(); ++i)
|
||
selFields += fields.get(i) + ", ";
|
||
String selFieldsTem = selFields.substring(0, selFields.length() - 2);// 根据String的索引提取子串
|
||
String sql = "SELECT " + selFieldsTem + " FROM `" + tableName + "`" + selCondition == "" ? "" : " WHERE " + selCondition;
|
||
try {
|
||
dbstate = dbconn.createStatement();
|
||
try {
|
||
dbresult = dbstate.executeQuery(sql);
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
}
|
||
while (dbresult.next()) {
|
||
Map selResult = new HashMap();
|
||
for (String col : fields) {
|
||
selResult.put(col, dbresult.getString(col));
|
||
}
|
||
mapInList.add(selResult);
|
||
}
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
}
|
||
return mapInList;
|
||
}// end String dbSelect(…)
|
||
|
||
/**
|
||
* 对数据库表中的记录进行删除操作
|
||
*
|
||
* @param tableName
|
||
* @param condition
|
||
* @return bool值,表示删除成功或者失败。
|
||
*/
|
||
public boolean dbDelete(String tableName, HashMap<String, String> selConditions) {// ——–>>>删除操作
|
||
dbConnection();
|
||
String selCondition = "";
|
||
if (selConditions != null && !selConditions.isEmpty()) {
|
||
for (Entry<String, String> kvs : selConditions.entrySet()) {
|
||
selCondition += kvs.getKey() + "='" + kvs.getValue() + "', ";
|
||
}
|
||
selCondition = " WHERE " + selCondition.substring(0, selCondition.length() - 2);// 根据String的索引提取子串
|
||
}
|
||
String sql = "DELETE FROM `" + tableName + "` " + selCondition;
|
||
try {
|
||
dbstate.executeUpdate(sql);
|
||
return true;
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
return false;
|
||
}
|
||
}// end dbDelete(…)
|
||
|
||
/**
|
||
* 对数据库表中记录进行更新操作
|
||
*
|
||
* @param tabName
|
||
* @param reCount
|
||
* @return bool值,成功返回true,失败返回false
|
||
*/
|
||
@SuppressWarnings({
|
||
"rawtypes"
|
||
})
|
||
public boolean dbUpdate(String tabName, HashMap reCount, String upCondition) {
|
||
dbConnection();
|
||
String Values = "";
|
||
Iterator keyValues = reCount.entrySet().iterator();
|
||
for (int i = 0; i < reCount.size(); ++i) {
|
||
Map.Entry entry = (Map.Entry) keyValues.next();
|
||
Object key = entry.getKey();
|
||
Object value = entry.getValue();
|
||
Values += key + "='" + value + "'" + ", ";
|
||
}
|
||
String updateValues = Values.substring(0, Values.length() - 2);
|
||
String sql = "UPDATE `" + tabName + "` SET " + updateValues + " " + upCondition;
|
||
try {
|
||
dbstate.executeUpdate(sql);
|
||
return true;
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
return false;
|
||
}
|
||
}// end dbUpdate(…)
|
||
|
||
/**
|
||
* 对数据库表进行插入操作
|
||
*
|
||
* @param tabName
|
||
* - 表名
|
||
* @param values
|
||
* - 带键值的HashMap
|
||
* @return bool值,成功返回true,失败返回false
|
||
*/
|
||
public boolean dbInsert(String tabName, HashMap<String, String> values) {
|
||
dbConnection();
|
||
String sql = "";
|
||
String insertFields = "";
|
||
String insertValues = "";
|
||
for (Entry<String, String> kvs : values.entrySet()) {
|
||
insertFields += "`" + kvs.getKey() + "`, ";
|
||
insertValues += "'" + kvs.getValue() + "', ";
|
||
}
|
||
insertFields = insertFields.substring(0, insertFields.length() - 2);
|
||
insertValues = insertValues.substring(0, insertValues.length() - 2);
|
||
sql += "INSERT INTO `" + tabName + "` (" + insertFields + ") VALUES" + "(" + insertValues + ")";
|
||
try {
|
||
dbstate.executeUpdate(sql);
|
||
return true;
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
print("SQL查询语句: " + sql);
|
||
return false;
|
||
}
|
||
|
||
}// end dbInsert(…)
|
||
|
||
/**
|
||
* 断开数据库
|
||
*
|
||
* @return bool值,成功返回true,失败返回false
|
||
*/
|
||
public boolean dbClose() {
|
||
try {
|
||
dbconn.close();
|
||
return true;
|
||
} catch (Exception e) {
|
||
print("数据库操作出错: " + e.getMessage());
|
||
return false;
|
||
}
|
||
}// end dbClose()
|
||
|
||
}
|