SQL Insert語句生成器簡(jiǎn)介
下面將為您介紹SQL Insert語句生成器的程序代碼,供您參考, Insert語句是SQL語句中最常用的語句之一,希望對(duì)您學(xué)習(xí)SQL能夠有所幫助。
程序輸出:
insert into User ( name, age ) values( 'BeanSoft', 27 )
import java.util.*;
import StringUtil;
/**
* 生成 Insert SQL 語句的類.
* @author beansoft
* @date 2009-4-5
*/
public class SQLInsert extends HashMap<String, Object> {
private String table;// 表格名
private String columns;// 列名表, 以逗號(hào)隔開
/** 是否檢查列名有效性 */
private boolean checkColumn = true;
/** 是否允許重復(fù)設(shè)置列值, 默認(rèn)允許 */
private boolean allowDuplicate = true;
/** 列表, 保存可能的列名 */
private List<String> columnList = new ArrayList<String>();
public SQLInsert() {}
/**
* 給定表名和列名(用來檢驗(yàn))的構(gòu)造器.
* @param table - 表名
* @param columns - 列名(用來檢驗(yàn))
*/
public SQLInsert(String table, String columns) {
setTable(table);
setColumns(columns);
}
public static void main(String[] args) {
SQLInsert insert = new SQLInsert();
insert.setTable("User");
insert.setColumns("name, age");
insert.put("name", "BeanSoft");
insert.put("age", 27);
System.out.println(insert);
} #p#
public String toString() {
if(table == null || table.length() == 0) {
throw new Error("對(duì)不起, 請(qǐng)調(diào)用 setTable() 指定表名");
}
String sql = "insert into " + table + " ( ";
String values = "values( ";
if(this.size() == 0) {
throw new Error("對(duì)不起, 沒有任何列值, 無法生成 INSERT 語句");
} else if(columnList.size() == 0){
// 嘗試從主鍵列表生成列名列表
String[] cols = this.keySet().toArray(new String[0]);
for(String col: cols) {
columnList.add(col);
}
}
// System.out.println("columnList=" + columnList.size());
if(columnList.size() > 0 ) {
for (int i = 0; i < columnList.size(); i++) {
String col = columnList.get(i);
Object value = this.get(col);
if(value == null && checkColumn) {
throw new Error("對(duì)不起, 列[" + col + "]的值為空");
}
sql += col;
if(!StringUtil.isNumeric(value + "")) {
values += "'" + value + "'";
} else {
values += value;
}
if (columnList.size() > 1 && (i != columnList.size() - 1)) {
sql += (", ");
values += (", ");
}
}
}
sql += (" ) ");
values += (" )");
// System.out.println("自動(dòng)生成的 sql = " + sql + values);
return sql + values;
}
/**
* @param columns the 列名表, 以逗號(hào)隔開 to set
*/ #p#
public void setColumns(String columns) {
this.columns = columns;
columnList.clear();
if(columns != null) {
// 替換空格等字符, 否則解析會(huì)出錯(cuò)
StringBuffer buff = new StringBuffer();
for(int i = 0; i < columns.length(); i++) {
char ch = columns.charAt(i);
if(Character.isSpaceChar(ch)) {
continue;
}
buff.append(ch);
}
String[] cols = buff.toString().split(",");
// System.out.println("cols.length=" + cols.length);
for(String col: cols) {
columnList.add(col);
}
}
}
/**
* @return the 是否允許重復(fù)設(shè)置列值, 默認(rèn)允許
*/
public boolean isAllowDuplicate() {
return allowDuplicate;
}
/**
* @param allowDuplicate the 是否允許重復(fù)設(shè)置列值, 默認(rèn)允許 to set
*/
public void setAllowDuplicate(boolean allowDuplicate) {
this.allowDuplicate = allowDuplicate;
}
/**
* @return the 是否檢查列名有效性
*/
public boolean isCheckColumn() {
return checkColumn;
}
/**
* @param checkColumn the 是否檢查列名有效性 to set
*/
public void setCheckColumn(boolean checkColumn) {
this.checkColumn = checkColumn;
}
/**
* @return the 列名表, 以逗號(hào)隔開
*/
public String getColumns() {
return columns;
} #p#
/**
* 表格名
* @return
*/
public String getTable() {
return table;
}
/**
* 表格名
* @param table
*/
public void setTable(String table) {
this.table = table;
}
}
【編輯推薦】
SQL數(shù)據(jù)庫(kù)壓縮語句簡(jiǎn)介
通過執(zhí)行多條SQL語句實(shí)現(xiàn)數(shù)據(jù)庫(kù)事務(wù)