remove sql and FileUtils...

Signed-off-by: 502647092 <jtb1@163.com>
dev
502647092 2015-09-07 20:54:21 +08:00
parent 85d0bdf85e
commit 3a539f593f
4 changed files with 0 additions and 538 deletions

View File

@ -1,36 +0,0 @@
package cn.citycraft.Yum.sql;
// import java.sql.*;
/**
*
*
* @author 20157143:25:06
*/
public class MySQLHelper extends SQLHelper {
private static String drivername = "com.mysql.jdbc.Driver";
/**
*
*
* @param host
* -
* @param port
* -
* @param dbname
* -
* @param username
* -
* @param password
* -
*/
public MySQLHelper(String host, String port, String dbname, String username, String password) {
super(password, password, drivername, getUrl(host, port, dbname));
}
public static String getUrl(String host, String port, String dbaName) {
String Encode = "?&useUnicode=true&characterEncoding=utf-8";
return "jdbc:mysql://" + host + ":" + port + "/" + dbaName + Encode;
}
}

View File

@ -1,416 +0,0 @@
package cn.citycraft.Yum.sql;
/*
*
*/
// import java.sql.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
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 abstract class SQLHelper {
// ///////////////////////////////////////———–>>>数据成员 and 构造函数
protected Connection dbconn = null;
protected Statement dbstate = null;
protected ResultSet dbresult = null;
protected String username;
protected String password;
protected String url;
protected String drivername;
/**
*
*
* @param username
* -
* @param password
* -
* @param drivername
* -
* @param url
* -
*/
public SQLHelper(String username, String password, String drivername, String url) {
try {
Class.forName(drivername).newInstance();
} catch (Exception e) {
}
this.username = username;
this.password = password;
this.drivername = drivername;
this.url = url;
}
/**
*
*
* @param tableName
* -
* @param fields
* -
* @param Conditions
* -
* @return
*/
public boolean createTables(String tableName, HashMap<String, String> fields, String Conditions) {
if (!dbConnection())
return false;
String kv = "";
for (Entry<String, String> kvs : fields.entrySet())
kv += "`" + kvs.getKey() + "` " + kvs.getValue() + " NOT NULL , ";
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;
}
/**
*
*
* @return booltruefalse
*/
public boolean dbClose() {
try {
dbconn.close();
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
return false;
}
}// end dbClose()
/**
*
*
* @return
*/
public boolean dbConnection() {
try {
dbconn = DriverManager.getConnection(url, username, password);
dbstate = dbconn.createStatement();
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());// 得到出错信息
print("登录URL: " + url); // 发生错误时,将连接数据库信息打印出来
print("登录账户: " + username);
print("登录密码: " + password);
return false;
}
}
/**
*
*
* @param tableName
* @param condition
* @return bool
*/
public boolean dbDelete(String tableName, HashMap<String, String> selConditions) {// ——–>>>删除操作
if (!dbConnection())
return false;
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 tableName
*
* @param fieles
*
* @param selCondition
*
* @return
*/
public boolean dbExist(String tableName, HashMap<String, String> selConditions) {
if (!dbConnection())
return false;
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 {
dbresult = dbstate.executeQuery(sql);
return dbresult.next();
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
return false;
}
/**
*
*
* @param tabName
* -
* @param values
* - HashMap
* @return booltruefalse
*/
public boolean dbInsert(String tabName, HashMap<String, String> values) {
if (!dbConnection())
return false;
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(…)
/**
*
*
* @param tableName
*
* @param fieles
*
* @param selCondition
*
* @return mapList
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public List dbSelect(String tableName, List<String> fields, String selCondition) {
if (!dbConnection())
return null;
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 fieles
*
* @param selCondition
*
* @return
*/
public String dbSelectFirst(String tableName, String fields, HashMap<String, String> selConditions) {
if (!dbConnection())
return null;
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 {
dbresult = dbstate.executeQuery(sql);
if (dbresult.next())
return dbresult.getString(fields);
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
return null;
}
/**
*
*
* @param tabName
* @param reCount
* @return booltruefalse
*/
@SuppressWarnings({ "rawtypes" })
public boolean dbUpdate(String tabName, HashMap reCount, String upCondition) {
if (!dbConnection())
return false;
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 table
* -
* @return
*/
public boolean isTableExists(final String table) {
try {
if (!dbConnection())
return false;
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 void print(String str)// 简化输出
{
System.out.println(str);
}
/**
* SQL
*
* @param sql
* - SQL
* @return
*/
public boolean runSql(String sql) {
if (!dbConnection())
return false;
try {
PreparedStatement state = dbconn.prepareStatement(sql);
state.executeUpdate();
state.close();
return true;
} catch (final Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL语句: " + sql);
}
return false;
}
/**
* SQL
*
* @param sql
* - SQL
* @return
*/
public boolean runSqlfile(File file) {
BufferedReader br = null;
Statement state = null;
String sql = null;
if (!file.exists())
return false;
if (!dbConnection())
return false;
try {
print("执行SQL文件: " + file.getName() + " ...");
br = new BufferedReader(new FileReader(file));
state = dbconn.createStatement();
while ((sql = br.readLine()) != null)
if (sql != "")
try {
state.executeUpdate(sql);
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL语句: " + sql);
}
return true;
} catch (Exception e) {
print("执行SQL文件 " + file.getName() + "出错: " + e.getMessage());
return false;
} finally {
try {
if (state != null)
state.close();
if (br != null)
br.close();
} catch (Exception e) {
}
}
}
}

View File

@ -1,39 +0,0 @@
package cn.citycraft.Yum.sql;
// import java.sql.*;
/**
*
*
* @author 20157143:25:06
*/
public class SQLiteHelper extends SQLHelper {
private static String drivername = "org.sqlite.JDBC";
/**
*
*
* @param host
* -
* @param port
* -
* @param dbname
* -
* @param username
* -
* @param password
* -
*/
public SQLiteHelper(String filepath, String username, String password) {
super(password, password, drivername, getUrl(filepath));
}
public static String getUrl(String filepath) {
return "jdbc:sqlite:" + filepath;
}
//
// Class.forName("org.sqlite.JDBC");
// Connection conn =
// DriverManager.getConnection("jdbc:sqlite:filename");//filename为你的SQLite数据名称
}

View File

@ -1,47 +0,0 @@
/**
*
*/
package cn.citycraft.Yum.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
*
*
* @author
* 20158319:09:54
*/
public class FileUtil {
/**
*
*
* @param src
* -
* @param des
* -
* @return
*/
public static boolean copyFile(File src, File des) {
InputStream inStream = null;
FileOutputStream fs = null;
try {
int byteread = 0;
if (!src.exists())
return false;
inStream = new FileInputStream(src); // 读入原文件
fs = new FileOutputStream(des);
byte[] buffer = new byte[1024];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread); // 写入到目标文件
}
inStream.close();
fs.close();
return true;
} catch (Exception e) {
return false;
}
}
}