CTZLoginServer/src/cn/citycraft/CTZLoginServer/utils/MySqlHelper.java

341 lines
10 KiB
Java
Raw Normal View History

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
* 20157143: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 mapList
*/
@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 booltruefalse
*/
@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 booltruefalse
*/
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 booltruefalse
*/
public boolean dbClose() {
try {
dbconn.close();
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
return false;
}
}// end dbClose()
}