当前位置:首页 » 数据仓库 » 配置表如何做增删改
扩展阅读
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连接进行查询时,依然会查询到这条空数据。