真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java刪除impl代碼 java中impl

java對所有mongodb表進(jìn)行增刪改查表名怎么設(shè)置

一、MongoDB數(shù)據(jù)庫參數(shù)配置

晉江網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)公司于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

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

2、如果沒有通過mongodb.cfg.properties,會采用程序指定的默認(rèn)配置。

// 定義默認(rèn)配置,1、IP地址 2、端口號 3、用戶名 4、密碼 5、配置文件位置名 6、數(shù)據(jù)庫名????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";

3、通過有參構(gòu)造方法構(gòu)造MongoDBService對象或通過get/set方法,指定數(shù)據(jù)庫及集合,優(yōu)先級最高。

//有參構(gòu)造方法,指定數(shù)據(jù)庫名與集合名????public MongoDBServiceImpl(String dbName, String collName) {????????this.dbName = dbName;????????this.collName = collName;????????try {????????????db = getDb();????????} catch (Throwable e) {????????????e.printStackTrace();????????}????}????//無參構(gòu)造方法,返回配置文件配置的數(shù)據(jù)庫對象引用,如果配置文件中沒有設(shè)置則返回默認(rèn)數(shù)據(jù)庫對象引用????public MongoDBServiceImpl() {????????getDb();????}????/*?????* 獲取數(shù)據(jù)庫對象,3種情況(優(yōu)先級從高到低):span style="white-space: pre" /span *1、構(gòu)造方法指定2、配置文件指定3、默認(rèn)數(shù)據(jù)庫span style="white-space: pre" /span *(情況2、3在MongoDButil中設(shè)置)?????*/????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種情況(優(yōu)先級從高到低):?????????*1、構(gòu)造方法指定2、配置文件指定3、默認(rèn)數(shù)據(jù)庫?????*(情況2、3在MongoDButil中設(shè)置)?????*/????public DBCollection getCollection() {????????if(this.collName != null){????????????return db.getCollection(this.collName);????????}????????else {????????????return MongoDBUtil.getDBCollection();????????}????}

二、方法簡介(具體實現(xiàn)參看MongoDBServiceImpl接口實現(xiàn)類)

1、獲取基本信息或?qū)ο?

(1)、獲取數(shù)據(jù)庫名: getDbName()

(2)、設(shè)置數(shù)據(jù)庫名(指定數(shù)據(jù)庫): setDbName(String dbName)

(3)、獲取集合名: getCollName()

(4)、設(shè)置集合名(指定集合): setCollName(String collName)

(5)、獲取數(shù)據(jù)庫對象: getDb()

2、數(shù)據(jù)插入方式:

(1)、插入單條數(shù)據(jù): insert(DBObject obj)

(2)、插入多條數(shù)據(jù): insertBatch(List?list)void

3、數(shù)據(jù)刪除方式:

(1)、刪除單條數(shù)據(jù): delete(DBObject obj)

(2)、刪除多條數(shù)據(jù): deleteBatch(List?list)

4、數(shù)量統(tǒng)計方式:

(1)、獲取集合中數(shù)據(jù)數(shù)量: getCollectionCount()

(2)、獲取符合條件的數(shù)據(jù)數(shù)量: getCount(DBObject obj)

5、查找數(shù)據(jù):

(1)、查找所有數(shù)據(jù): findAll()

(2)、查找符合條件的數(shù)據(jù): find(DBObject obj)

(3)、查找符合條件的數(shù)據(jù)并排序: find(DBObject query, DBObject sort)

(4)、查找符合條件的指定數(shù)量的數(shù)據(jù)并排序:find(DBObject query, DBObject sort, int start, int limit)

(5)、由ID查找數(shù)據(jù): getById(String id)

6、更新數(shù)據(jù)?:update(DBObject setFields, DBObject whereFields) void

7、打印List:?printListDBObj(List?list)

測試代碼:(@Test)

public class testMongoService {????//使用mongodb.cfg.properties中配置的數(shù)據(jù)庫與集合,如未指定,使用MongoDBUtil中默認(rèn)的數(shù)據(jù)庫與集合????MongoDBService mongoDBService1 = new MongoDBServiceImpl();?????????//測試插入數(shù)據(jù)????@Test????public void testInsert(){????????//數(shù)據(jù)一,包括用戶名、密碼,地址信息(省份、城市),愛好[…]????????BasicDBList dbList1 = new BasicDBList();????????dbList1.add("basketball");????????dbList1.add("music");????????dbList1.add("web");????????DBObject dbObject1 = new BasicDBObject("username","insert1")????????????.append("age", 18)????????????.append("address", new BasicDBObject("province","廣東").append("city", "廣州"))????????????.append("favourite", dbList1);????????//數(shù)據(jù)二????????BasicDBList dbList2 = new BasicDBList();????????dbList2.add("football");????????dbList2.add("music");????????DBObject dbObject2 = new BasicDBObject("username","insert2")????????????.append("age", 18)????????????.append("address", new BasicDBObject("province","陜西").append("city", "西安"))????????????.append("favourite", dbList2);????????//數(shù)據(jù)三????????BasicDBList dbList3 = new BasicDBList();????????dbList3.add("Linux");????????DBObject dbObject3 = new BasicDBObject("username","insert3")????????????.append("age", 18)????????????.append("address", new BasicDBObject("province","河北").append("city", "保定"))????????????.append("favourite", dbList3);????????//數(shù)據(jù)四????????BasicDBList dbList4 = new BasicDBList();????????dbList4.add("swim");????????dbList4.add("android");????????DBObject dbObject4 = new BasicDBObject("username","insert4")????????????.append("age", 18)????????????.append("address", new BasicDBObject("province","四川").append("city", "成都"))????????????.append("favourite", dbList4);????????//數(shù)據(jù)五????????DBObject dbObject5 = new BasicDBObject("username", "insert5")????????????.append("age", 28)????????????.append("address", new BasicDBObject("city", "杭州"));????????mongoDBService1.printListDBObj(mongoDBService1.findAll());????????System.out.println("——————————————————insert collection——————————————————");????????Listdbobject list = new ArrayListdbobject();????????list.add(dbObject1);????????list.add(dbObject2);????????list.add(dbObject3);????????list.add(dbObject5);????????mongoDBService1.insertBatch(list);????????System.out.println("——————————————————insert one——————————————————");????????mongoDBService1.insert(dbObject4);????????mongoDBService1.printListDBObj(mongoDBService1.findAll());????}?????????//測試查詢數(shù)據(jù)????@Test????public void testFind(){????????DBObject dbObject = new BasicDBObject("username","insert1");????????System.out.println("數(shù)量:" + mongoDBService1.getCollectionCount());????????System.out.println("username=java的數(shù)據(jù)數(shù)量:" + mongoDBService1.getCount(dbObject));????????System.out.println("——————————————————find all——————————————————");????????mongoDBService1.printListDBObj(mongoDBService1.findAll());????????System.out.println("——————————————————find obj——————————————————");????????mongoDBService1.printListDBObj(mongoDBService1.find(dbObject));????????System.out.println("——————————————————find sort——————————————————");????????mongoDBService1.printListDBObj(mongoDBService1.find(new BasicDBObject(), new BasicDBObject("age", 1)));????????System.out.println("——————————————————find sort limit——————————————————");????????mongoDBService1.printListDBObj(mongoDBService1.find(new BasicDBObject(), new BasicDBObject("age", 1), 1, 2));????}?????//測試數(shù)據(jù)更新????@Test????public void testUpdate(){????????BasicDBObject newDocument = new BasicDBObject("$set",new BasicDBObject("age",11));?????????????????????????BasicDBObject searchQuery = new BasicDBObject().append("username", "insert2");?????????????mongoDBService1.printListDBObj(mongoDBService1.find(searchQuery));????????System.out.println("——————————————————update——————————————————");????????mongoDBService1.update(newDocument, searchQuery);????????mongoDBService1.printListDBObj(mongoDBService1.find(searchQuery));????}?????????//測試數(shù)據(jù)刪除????@Test????public void testDelete(){????????DBObject dbObject1 = new BasicDBObject("username", "insert1");????????DBObject dbObject2 = new BasicDBObject("username", "insert2");????????DBObject dbObject3 = new BasicDBObject("username", "insert3");????????DBObject dbObject4 = new BasicDBObject("username", "insert4");????????DBObject dbObject5 = new BasicDBObject("username", "insert5");????????Listdbobject list = new ArrayListdbobject();????????list.add(dbObject1);????????list.add(dbObject2);????????list.add(dbObject3);????????list.add(dbObject4);????????mongoDBService1.printListDBObj(mongoDBService1.findAll());????????System.out.println("——————————————————delete list——————————————————");????????mongoDBService1.deleteBatch(list);????????System.out.println("——————————————————delete one——————————————————");????????mongoDBService1.delete(dbObject5);????????//System.out.println("——————————————————delete all——————————————————");????????//mongoDBService1.delete(new BasicDBObject());????????mongoDBService1.printListDBObj(mongoDBService1.findAll());????}}/dbobject/dbobject/dbobject/dbobject

測試結(jié)果:

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

MongoDBServiceImpl.java

public class MongoDBServiceImpl implements MongoDBService {????private String dbName;????private String collName;????private DB db;?????????//有參構(gòu)造方法,指定數(shù)據(jù)庫名與集合名????public MongoDBServiceImpl(String dbName, String collName) {????????this.dbName = dbName;????????this.collName = collName;????????try {????????????db = getDb();????????} catch (Throwable e) {????????????e.printStackTrace();????????}????}????//無參構(gòu)造方法,返回配置文件配置的數(shù)據(jù)庫對象引用,如果配置文件中沒有設(shè)置則返回默認(rèn)數(shù)據(jù)庫對象引用????public MongoDBServiceImpl() {????????getDb();????}????/*?????* 獲取數(shù)據(jù)庫對象,3種情況(優(yōu)先級從高到低):?????*1、構(gòu)造方法指定2、配置文件指定3、默認(rèn)數(shù)據(jù)庫?????*(情況2、3在MongoDButil中設(shè)置)?????*/????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種情況(優(yōu)先級從高到低):?????*1、構(gòu)造方法指定2、配置文件指定3、默認(rèn)數(shù)據(jù)庫?????*(情況2、3在MongoDButil中設(shè)置)?????*/????public DBCollection getCollection() {????????if(this.collName != null){????????????return db.getCollection(this.collName);????????}????????else {????????????return MongoDBUtil.getDBCollection();????????}????}?????public DBObject map2Obj(Mapstring, object="" map) {????????DBObject obj = new BasicDBObject();????????if (map.containsKey("class") map.get("class") instanceof Class)????????????map.remove("class");????????obj.putAll(map);????????return obj;????}????//插入數(shù)據(jù)????public void insert(DBObject obj) {????????getCollection().insert(obj);????}????//插入多條數(shù)據(jù)????public void insertBatch(Listdbobject list) {????????if (list == null || list.isEmpty()) {????????????return;????????}????????Listdbobject listDB = new ArrayListdbobject();????????for (int i = 0; i list.size(); i++) {????????????listDB.add(list.get(i));????????}????????getCollection().insert(listDB);????}????//刪除數(shù)據(jù)????public void delete(DBObject obj) {????????getCollection().remove(obj);????}????//刪除多條數(shù)據(jù)????public void deleteBatch(Listdbobject list) {????????if (list == null || list.isEmpty()) {????????????return;????????}????????for (int i = 0; i list.size(); i++) {????????????getCollection().remove(list.get(i));????????}????}????//獲取集合中的數(shù)據(jù)數(shù)量????public long getCollectionCount() {????????return getCollection().getCount();????}????//查找符合條件的數(shù)據(jù)數(shù)量????public long getCount(DBObject obj) {????????if (obj != null)????????????return getCollection().getCount(obj);????????return getCollectionCount();????}????//查找符合條件的數(shù)據(jù)????public Listdbobject find(DBObject obj) {????????DBCursor cur = getCollection().find(obj);????????return DBCursor2list(cur);????}?????????//查找符合條件的數(shù)據(jù)并排序????@Override????public Listdbobject 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);????}?????//查找符合條件的數(shù)據(jù)并排序,規(guī)定數(shù)據(jù)個數(shù)????@Override????public Listdbobject 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轉(zhuǎn)化為listdbobject????private Listdbobject DBCursor2list(DBCursor cur) {????????Listdbobject list = new ArrayListdbobject();????????if (cur != null) {????????????list = cur.toArray();????????}????????return list;????}?????//更新數(shù)據(jù)????public void update(DBObject setFields, DBObject whereFields) {????????getCollection().updateMulti(whereFields, setFields);????}????//查詢集合中所有數(shù)據(jù)????public Listdbobject findAll() {????????DBCursor cur = getCollection().find();????????Listdbobject list = new ArrayListdbobject();????????if (cur != null) {????????????list = cur.toArray();????????}????????return list;????}?????//由ID獲取數(shù)據(jù)????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;????}????@Override????public void printListDBObj(Listdbobject list) {????????// TODO Auto-generated method stub????????for(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 {????// 定義默認(rèn)配置,1、IP地址 2、端口號 3、用戶名 4、密碼 5、配置文件位置名 6、數(shù)據(jù)庫名????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";????// 定義靜態(tài)變量,1、Mongo對象(代表數(shù)據(jù)庫連接)2、DB對象(代表數(shù)據(jù)庫)3、集合名4、數(shù)據(jù)庫相關(guān)配置映射集合5、已獲取的數(shù)據(jù)庫連接????private static Mongo mongo;????private static DB db;????private static DBCollection collection;????private static Mapstring, string="" cfgMap = new HashMapstring, string=""();????private static Hashtablestring, db="" mongoDBs = new Hashtablestring, db=""();?????/**?????* 初始化Mongo的數(shù)據(jù)庫?????*/????static {????????init();????}?????/**?????* 獲取配置文件中配置的DB對象?????*/????public static DB getDB() {????????return db;????}?????/**?????* 獲取配置文件中配置的DBCollection對象?????*/????public static DBCollection getDBCollection() {????????return collection;????}?????/**?????* 根據(jù)數(shù)據(jù)庫名稱,得到數(shù)據(jù)庫 如果不存在,則創(chuàng)建一個該名稱的數(shù)據(jù)庫,并設(shè)置用戶名和密碼為配置文件中的參數(shù)值?????*?????* @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配置文件初始化配置映射集合,如果沒有編寫配置文件,則加載程序指定的默認(rèn)配置?????*/????@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 { // 如果沒有編寫配置文件,則加載默認(rèn)配置????????????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的數(shù)據(jù)庫(將db指向相應(yīng)對象引用,將collection指向相應(yīng)對象引用,通過mongoDBs記錄現(xiàn)有數(shù)據(jù)庫對象)?????*/????@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();????????}????}?}

關(guān)于java 連接數(shù)據(jù)庫實現(xiàn)增刪改查的具體功能代碼。

首先你得確定你的數(shù)據(jù)庫連接是通過什么形式連接的,hibernate還是原生態(tài)的jdbc 還是spring;

如果是只有hibernate,那么你得通過加載配置文件得到sessionFactory,然后得到session

如果spring,那么同樣也需要注入sessionfactory到你的dao

如果是jdbc方式,那么你就按照原生態(tài)jdbc寫法

總之,在你構(gòu)造DAO時,得有數(shù)據(jù)源。這樣才能操縱你的數(shù)據(jù)庫

如果搞懂了這些問題,那么你的第一個,第三個問題就迎刃而解了。至于第二問題,我沒明白你什么意思!

處理刪除操作時出現(xiàn) Unknown entity: java.lang.Integer

看一下--------SubjectAction

public String deleteSubject(){

isubjectbiz.deleteSubject(subjectId);

return SUCCESS;

}

subjectId 是否是用struts2 自動從頁面封裝過來的 還是你使用request獲取的 首先你輸出一下有沒有值 然后如果是request獲取的 必須要把它轉(zhuǎn)換成integer 其次如果有值 且為integer 這時你就要檢查你的數(shù)據(jù)庫隱射文件是否正確 一級一級排除

JAVA 實現(xiàn)數(shù)據(jù)庫增刪改查的Dao和DaoImpl的寫法

package org.dao;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Date;

import org.dbc.DBManageer;

import org.dbc.DBUtil;

import org.vo.CardInfo;

public class CardInfoDao {

public void testResultSetMetaData() {

String sql = "select * from cardinfo";

Connection con = new DBUtil().getConnection();

PreparedStatement ps = null;

ResultSetMetaData rsmd = null;

ResultSet rs = null;

try {

ps = con.prepareStatement(sql);

rs = ps.executeQuery();

rsmd = rs.getMetaData();

for (int i = 1; i rsmd.getColumnCount(); i++) {

System.out.print(" 數(shù)據(jù)類型名:" + rsmd.getColumnClassName(i));

System.out.print(" 別名:" + rsmd.getColumnLabel(i));

System.out.print(" 列名:" + rsmd.getColumnName(i));

System.out.print(" 數(shù)據(jù)類型:" + rsmd.getColumnTypeName(i));

System.out.println(" 數(shù)據(jù)類型:" + rsmd.getColumnType(i));

}

while (rs.next()) {

// 通過反射可以對VO對象(CardInfo)自動賦值

// for(...)

// CardInfo ci=new CardInfo();

// ci.setId(rs.getInt(columnIndex))

/*

* CardInfo ca = new CardInfo(); Class cc = ca.getClass();

*/

/*Class? c = Class.forName("org.vo.CardInfo");

CardInfo ca = (CardInfo) c.newInstance();

ca.setCardId(rs.getString("cardId"));

ca.setCustomerName(rs.getString("customerName"));

ca.setCurrentMoney(rs.getFloat("currentMoney"));

ca.setOpenDate(rs.getDate("openDate"));

System.out.println(ca);//ok */

//加載一個CardInfo類

Class c = Class.forName("org.vo.CardInfo");

Object o = c.newInstance(); //獲得它的一個實例

//定義String類的對象數(shù)組

Class[] params = new Class[] { String.class};

//定義float類的對象數(shù)組

Class[] floatparams = new Class[] { float.class};

//定義Date(util)類的對象數(shù)組

Class[] dateparams = new Class[] { Date.class};

//獲得setCardId方法

Method me = c.getMethod("setCardId", params);

//實例方法的參數(shù)

Object []ostr = new Object[]{rs.getString("cardId")};

//如果底層方法是靜態(tài)的,那么可以忽略指定的 obj 參數(shù)。該參數(shù)可以為 null。

//如果底層方法所需的形參數(shù)為 0,則所提供的 args 數(shù)組長度可以為 0 或 null。

//o 調(diào)用方法的對象 ostr 方法調(diào)用的參數(shù)

me.invoke(o, ostr);

//獲得setCustomerName方法

Method name = c.getMethod("setCustomerName", params);

Object []cusname = new Object[]{rs.getString("customerName")};

name.invoke(o,cusname);

//獲得setCurrentMoney方法

Method money = c.getMethod("setCurrentMoney", floatparams);

Object []cusmoney = new Object[]{rs.getFloat("currentMoney")};

money.invoke(o,cusmoney);

//獲得setOpenDate方法

Method date = c.getMethod("setOpenDate",dateparams);

Object []openDate = new Object[]{rs.getDate("openDate")};

date.invoke(o, openDate);

//打印

System.out.println(o);

}

} catch (SQLException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

}catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

public void showResultSet() {

String sql = "select * from cardinfo";

Connection con = new DBManageer().getConnection();

PreparedStatement ps = null;

try {

ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

ResultSet rs = ps.executeQuery();

System.out.println("---依次讀取------");

while (rs.next()) {

CardInfo ca = new CardInfo();

ca.setCardId(rs.getString("cardId"));

ca.setCustomerName(rs.getString("customerName"));

ca.setCurrentMoney(rs.getFloat("currentMoney"));

ca.setOpenDate(rs.getDate("openDate"));

System.out.println(ca);

}

System.out.println("---倒讀------");

while (rs.previous()) {

CardInfo ca = new CardInfo();

ca.setCardId(rs.getString("cardId"));

ca.setCustomerName(rs.getString("customerName"));

ca.setCurrentMoney(rs.getFloat("currentMoney"));

ca.setOpenDate(rs.getDate("openDate"));

System.out.println(ca);

}

rs.absolute(3);// 定位倒第幾行

rs.updateString("customerName", "star");

// rs.updateRow();

rs.beforeFirst();

while (rs.next()) {

CardInfo ca = new CardInfo();

ca.setCardId(rs.getString("cardId"));

ca.setCustomerName(rs.getString("customerName"));

ca.setCurrentMoney(rs.getFloat("currentMoney"));

ca.setOpenDate(rs.getDate("openDate"));

System.out.println(ca);

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

java如何實現(xiàn)對數(shù)據(jù)表里面的數(shù)據(jù)刪除(最好給個具體代碼范例)

連接數(shù)據(jù)庫

public class DBManager {

//定義數(shù)據(jù)庫連接的URL

private static final String URL="jdbc:sqlserver://localhost:1433;database=j1105";

//定義數(shù)據(jù)庫的用戶名

private static final String USERNAME = "sa";

//定義數(shù)據(jù)庫密碼

private static final String PASSWORD = "sa";

//定義一個連接的引用,使用單例模式

private static Connection conn = null;

//使用靜態(tài)塊來注冊驅(qū)動

//類加載時自動執(zhí)行代碼塊

static {

//反射com.microsoft.sqlserver.jdbc.SQLServerDriver.class

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

//獲得連接

//在程序使用過程中始終只有1個對象存在

//使用單例模式來給Connection賦值

public static Connection getConnection(){

if(conn == null){

try {

conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

} catch (SQLException e) {

e.printStackTrace();

}

}

return conn;

}

/**

* 關(guān)閉的一些操作 , 優(yōu)化

* @param conn

* @param stat

* @param rs

*/

public static void close(Connection conn,Statement stat,ResultSet rs){

try{

if(conn != null){

conn.close();

}

if(stat != null){

stat.close();

}

if(rs != null){

rs.close();

}

}catch(SQLException e){

e.printStackTrace();

}

}

/**

* 重寫上面的方法,在只有2個參數(shù)的情況下關(guān)閉

* @param conn

* @param stat

*/

public static void close(Connection conn,Statement stat){

try{

if(conn != null){

conn.close();

}

if(stat != null){

stat.close();

}

}catch(SQLException e){

e.printStackTrace();

}

}

public static void main(String[] args){

Connection conn = DBManager .getConnection();

System.out.println(conn);

}

}

接口

public interface IStudentDao {

public void deleteStudent(int xh);

}

實現(xiàn)

public class StudentDAOimpl implements IStudentDao {

public void deleteStudent(int xh) {

try{

String sql = "delete from tb_student where xh = ?";

PreparedStatement ps = conn.prepareStatement(sql);

ps.setInt(1, xh);

ps.executeUpdate();

System.out.println("成功刪除");

}catch(SQLException e){

e.printStackTrace();

}

}

}


分享名稱:java刪除impl代碼 java中impl
網(wǎng)頁URL:http://weahome.cn/article/hpeoec.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部