當前位置:首頁 » 數據倉庫 » 配置表如何做增刪改
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

配置表如何做增刪改

發布時間: 2022-11-23 04:07:21

㈠ java對所有mongodb表進行增刪改查表名怎麼設置

一、MongoDB資料庫參數配置

1、推薦使用mongodb.cfg.properties配置,則在構造MongoDBService對象的時候只需調用無參構造方法即可自動完成配置。

源代碼:(完整項目文件下載鏈接:點擊打開鏈接)

MongoDBServiceImpl.java

public class MongoDBServiceImpl implements MongoDBService {private String dbName;private String collName;private DB db;//有參構造方法,指定資料庫名與集合名public MongoDBServiceImpl(String dbName, String collName) {this.dbName = dbName;this.collName = collName;try {db = getDb();} catch (Throwable e) {e.printStackTrace();}}//無參構造方法,返回配置文件配置的資料庫對象引用,如果配置文件中沒有設置則返回默認資料庫對象引用public MongoDBServiceImpl() {getDb();}/** 獲取資料庫對象,3種情況(優先順序從高到低):*1、構造方法指定2、配置文件指定3、默認資料庫*(情況2、3在MongoDButil中設置)*/public DB getDb() {if (this.db == null) {if (this.dbName == null) {this.db = MongoDBUtil.getDB();} else {this.db = MongoDBUtil.getDBByName(this.dbName);}}return this.db;}/** 獲取集合對象,3種情況(優先順序從高到低):*1、構造方法指定2、配置文件指定3、默認資料庫*(情況2、3在MongoDButil中設置)*/public DBCollection getCollection() {if(this.collName != null){return db.getCollection(this.collName);}else {return MongoDBUtil.getDBCollection();}}public DBObject map2Obj(Map<string, object=""> map) {DBObject obj = new BasicDBObject();if (map.containsKey("class") && map.get("class") instanceof Class)map.remove("class");obj.putAll(map);return obj;}//插入數據public void insert(DBObject obj) {getCollection().insert(obj);}//插入多條數據public void insertBatch(List<dbobject> list) {if (list == null || list.isEmpty()) {return;}List<dbobject> listDB = new ArrayList<dbobject>();for (int i = 0; i < list.size(); i++) {listDB.add(list.get(i));}getCollection().insert(listDB);}//刪除數據public void delete(DBObject obj) {getCollection().remove(obj);}//刪除多條數據public void deleteBatch(List<dbobject> list) {if (list == null || list.isEmpty()) {return;}for (int i = 0; i < list.size(); i++) {getCollection().remove(list.get(i));}}//獲取集合中的數據數量public long getCollectionCount() {return getCollection().getCount();}//查找符合條件的數據數量public long getCount(DBObject obj) {if (obj != null)return getCollection().getCount(obj);return getCollectionCount();}//查找符合條件的數據public List<dbobject> find(DBObject obj) {DBCursor cur = getCollection().find(obj);return DBCursor2list(cur);}//查找符合條件的數據並排序@Overridepublic List<dbobject> find(DBObject query, DBObject sort) {DBCursor cur;if (query != null) {cur = getCollection().find(query);} else {cur = getCollection().find();}if (sort != null) {cur.sort(sort);}return DBCursor2list(cur);}//查找符合條件的數據並排序,規定數據個數@Overridepublic List<dbobject> find(DBObject query, DBObject sort, int start,int limit) {DBCursor cur;if (query != null) {cur = getCollection().find(query);} else {cur = getCollection().find();}if (sort != null) {cur.sort(sort);}if (start == 0) {cur.batchSize(limit);} else {cur.skip(start).limit(limit);}return DBCursor2list(cur);}//將DBCursor轉化為list<dbobject>private List<dbobject> DBCursor2list(DBCursor cur) {List<dbobject> list = new ArrayList<dbobject>();if (cur != null) {list = cur.toArray();}return list;}//更新數據public void update(DBObject setFields, DBObject whereFields) {getCollection().updateMulti(whereFields, setFields);}//查詢集合中所有數據public List<dbobject> findAll() {DBCursor cur = getCollection().find();List<dbobject> list = new ArrayList<dbobject>();if (cur != null) {list = cur.toArray();}return list;}//由ID獲取數據public DBObject getById(String id) {DBObject obj = new BasicDBObject();obj.put("_id", new ObjectId(id));DBObject result = getCollection().findOne(obj);return result;}public String getDbName() {return dbName;}public void setDbName(String dbName) {this.dbName = dbName;this.db = MongoDBUtil.getDBByName(this.dbName);}public String getCollName() {return collName;}public void setCollName(String collName) {this.collName = collName;}@Overridepublic void printListDBObj(List<dbobject> list) {// TODO Auto-generated method stubfor(DBObject dbObject: list){System.out.println(dbObject);}}}</dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></string,>

MongoDBUtil.java

public class MongoDBUtil {// 定義默認配置,1、IP地址 2、埠號 3、用戶名 4、密碼 5、配置文件位置名 6、資料庫名private static final String MONGODB_ADDRESS = "127.0.0.1";private static final int MONGODB_PORT = 27017;private static final String MONGODB_USERNAME = "root";private static final String MONGODB_PASSWORD = "";private static final String MONGODB_RESOURCE_FILE = "mongodb.cfg.properties";private static final String MONGODB_DBNAME = "test";private static final String MONGODB_COLLECTIONNAME = "test";// 定義靜態變數,1、Mongo對象(代表資料庫連接)2、DB對象(代表資料庫)3、集合名4、資料庫相關配置映射集合5、已獲取的資料庫連接private static Mongo mongo;private static DB db;private static DBCollection collection;private static Map<string, string=""> cfgMap = new HashMap<string, string="">();private static Hashtable<string, db=""> mongoDBs = new Hashtable<string, db="">();/*** 初始化Mongo的資料庫*/static {init();}/*** 獲取配置文件中配置的DB對象*/public static DB getDB() {return db;}/*** 獲取配置文件中配置的DBCollection對象*/public static DBCollection getDBCollection() {return collection;}/*** 根據資料庫名稱,得到資料庫 如果不存在,則創建一個該名稱的資料庫,並設置用戶名和密碼為配置文件中的參數值** @param dbName* @return DB*/@SuppressWarnings("deprecation")public static DB getDBByName(String dbName) {DB db = mongo.getDB(dbName);if (!mongoDBs.contains(db)) {System.out.println("add");db.addUser(cfgMap.get("mongo.db.username"),cfgMap.get("mongo.db.password").toCharArray());mongoDBs.put(dbName, db);}return db;}// ————————————————————————————————————初始化過程————————————————————————————————————/*** 獲取配置文件mongedb.cfg.properties的文件對象*/public static File getConfigFile() {String path = MongoDBUtil.class.getResource("/").getPath();String fileName = path + MONGODB_RESOURCE_FILE;System.out.println(fileName);File file = new File(fileName);if (file.exists()) {return file;}return null;}/*** 通過mongedb.cfg.properties配置文件初始化配置映射集合,如果沒有編寫配置文件,則載入程序指定的默認配置*/@SuppressWarnings("unchecked")private static void initCfgMap() {File file = getConfigFile();if (file != null) {Properties p = new Properties();try {p.load(new FileInputStream(file));for (Enumeration enu = p.propertyNames(); enu.hasMoreElements();) {String key = (String) enu.nextElement();String value = (String) p.getProperty(key);cfgMap.put(key, value);}} catch (IOException e) {System.out.println("載入Mongo配置文件失敗!");e.printStackTrace();}} else { // 如果沒有編寫配置文件,則載入默認配置cfgMap.put("mongo.db.address", MONGODB_ADDRESS);cfgMap.put("mongo.db.port", String.valueOf(MONGODB_PORT));cfgMap.put("mongo.db.username", MONGODB_USERNAME);cfgMap.put("mongo.db.password", MONGODB_PASSWORD);cfgMap.put("mongo.db.dbname", MONGODB_DBNAME);cfgMap.put("mongo.db.collectionname", MONGODB_COLLECTIONNAME);}}/*** 初始化Mongo的資料庫(將db指向相應對象引用,將collection指向相應對象引用,通過mongoDBs記錄現有資料庫對象)*/@SuppressWarnings("deprecation")private static void init() {initCfgMap();try {String address = cfgMap.get("mongo.db.address");int port = Integer.parseInt(cfgMap.get("mongo.db.port").toString());String dbName = cfgMap.get("mongo.db.dbname");String username = cfgMap.get("mongo.db.username");String password = cfgMap.get("mongo.db.password");String collectionName = cfgMap.get("mongo.db.collectionname");mongo = new Mongo(address, port);if (dbName != null && !"".equals(dbName)) {db = mongo.getDB(dbName);if (username != null && !"".equals(username)) {db.addUser(username, password.toCharArray());if (collectionName != null && !"".equals(collectionName)) {collection = db.getCollection(collectionName);}}mongoDBs.put(dbName, db);}} catch (Exception e) {e.printStackTrace();}}}

㈡ 資料庫增刪改查基本語句

1、「INSERTINTO」語句,用於向表格中增加新的行。

2、「DELETE」語句,用於刪除表中的行。

3、「Update」語句,用於修改表中的數據。

4、「SELECT」語句,用於從表中選取數據。

sql語言特點:

SQL可以獨立完成資料庫生命周期中的全部活動,包括定義關系模式、錄入數據、建立資料庫、查詢、更新、維護、資料庫重構、資料庫安全性控制等一系列操作,這就為資料庫應用系統開發提供了良好的環境,在資料庫投入運行後,還可根據需要隨時逐步修改模式,且不影響資料庫的運行,從而使系統具有良好的可擴充性。

㈢ c#如何實現對表格(excel)的增刪改查

一、首先處理好資料庫連接字串

Excel2000-2003: string connStr = "Microsoft.Jet.Oledb.4.0;Data Source='c:\test.xls';Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";

Excel2007: string connStr = "Microsoft.Ace.OleDb.12.0;Data Source='c:\test.xlsx';Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

其中:

HDR ( Header Row )設置:

若指定值為Yes,代表 Excel 檔中的工作表第一行是欄位名稱

若指定值為 No,代表 Excel 檔中的工作表第一行就是資料了,沒有欄位名稱

IMEX ( IMport EXport mode )設置

當 IMEX=0 時為"匯出模式",這個模式開啟的 Excel 檔案只能用來做"寫入"用途。

當 IMEX=1 時為"匯入模式",這個模式開啟的 Excel 檔案只能用來做"讀取"用途。

當 IMEX=2 時為"連結模式",這個模式開啟的 Excel 檔案可同時支援"讀取"與"寫入"用途。

二、進行表格數據的查詢、插入和更新:

(假設Excel文件text.xls中存在Excel表單tree,有2列分別為id,name)

1、查詢

String sql = "select id, name from [tree$]";



String sql = "select id, name from `tree$`;

2、插入

String sql = "insert into [tree$] (id,name) values(1,'testname');

3、更新

String sql = "update [tree$] set name='name2' where id=1;

4、數據的刪除

在OleDB的連接方式下,不可以使用delete from 語句來刪除某表中的某一條記錄。確切的說,在此模式下,將無法刪除表中的記錄。即使用update語句將所有的欄位寫成null,打開excel文件後依然會發現保留了該空行,而且在使用oleDB連接進行查詢時,依然會查詢到這條空數據。