當前位置:首頁 » 編程語言 » java增刪

java增刪

發布時間: 2022-05-15 15:07:46

java增刪改查功能怎麼實現

你所說的增刪改查是資料庫的命令操作。在Java編程中,在Java和資料庫完成連接以後,可以調用資料庫的select、delete、updata等命令。你也可以把相關的命令變成一個字元串對象,這樣調用起來會更簡單。

② Java增刪改查是個什麼概念

有個表格記錄數據 一行是一條記錄 對應 java的一個對象

---------------------------------

編號 姓名 年齡

1 張三 21

2 李四 22

---------------------------------

publicclassPerson{
privateintid;//編號
privateStringname;
privateintage;

...
}
//利用jdbc等技術向表中,寫入記錄,刪除記錄,修改記錄,查看記錄就叫增刪改查了

③ java 表格增刪改查

import java.util.*;
import java.text.*;
import java.io.*;
import jxl.*;
public class jxl
{
public static void main(String[] args)
{
String fileName = "c://signupcheckin.xls";
InputStream is = null;
FileInputStream fn = null;
int cs = 0;
int rs = 0;
try{
fn = new FileInputStream(fileName);
is = fn;
Workbook rb = Workbook.getWorkbook(is);
Sheet s = rb.getSheet(0);//此處只讀取第一個sheet內容
Cell a2=s.getCell(0,1); //此處是獲取單個單元格的數據
Cell c4=s.getCell(2,3);
String sa2=a2.getContents();
String sc4=c4.getContents();
System.out.println(sa2+" "+sc4);
System.out.print("\n\n\n");
cs = s.getColumns();
rs = s.getRows();
for(int j=0;j<rs;j++){
Cell[] c = s.getRow(j);
for(int x=0;x<c.length;x++){
System.out.print(c[x].getContents()+"\t");
}
System.out.print("\r\n");
}
System.out.println("Columns:"+cs+" Rows:"+rs);
}catch(Exception e){
System.out.println(e.toString());
}finally{
try{
fn.close();
is.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
}
}

④ 怎麼通過JAVA創建表、欄位並可以進行增刪改查

java數組不能直接進行增加和刪除,只能重新new一個數組,重新賦值

⑤ 如何在java中實現資料庫的增刪改查

首先得確定你的資料庫連接是通過什麼形式連接的,hibernate還是原生態的jdbc 還是spring;

如果是只有hibernate,那麼你得通過載入配置文件得到sessionFactory,然後得到session
如果spring,那麼同樣也需要注入sessionfactory到你的
如果是jdbc方式,那麼你就按照原生態jdbc寫法
總之,你構造DAO時,得有數據源。這樣才能操縱資料庫

⑥ 如何通過java實現對指定目錄下的txt文件進行增刪改查

代碼如下:

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.util.ArrayList;
importjava.util.List;

publicclassApp61{

publicstaticvoidmain(String[]args)throwsIOException{

//查找行輸出
Stringline=findFileLine("mylist.txt","abc");
System.out.println(line);

//刪除指定行
removeFileLine("mylist.txt",2);
}

staticvoidremoveFileLine(Stringfile,intline)throwsIOException{

List<String>lines=readFileLines(file);

lines.remove(line-1);

FileOutputStreamoutputStream=null;

=null;

BufferedWriterwriter=null;

try{

outputStream=newFileOutputStream(file);

streamWriter=newOutputStreamWriter(outputStream);

writer=newBufferedWriter(streamWriter);

for(Stringstr:lines){
writer.write(str+System.lineSeparator());
}

}finally{
if(writer!=null){
writer.close();
}
if(streamWriter!=null){
streamWriter.close();
}
if(outputStream!=null){
outputStream.close();
}
}
}


//查找行
staticStringfindFileLine(Stringfile,Stringkeywork)throwsIOException{

List<String>lines=readFileLines(file);

for(Stringline:lines){
if(line.contains(keywork)){
returnline;
}
}

return"";
}

//返迴文件所有行
staticList<String>readFileLines(Stringfile)throwsIOException{

List<String>lines=newArrayList<>();

FileInputStreaminputStream=null;

InputStreamReaderstreamReader=null;

BufferedReaderreader=null;

try{

inputStream=newFileInputStream(file);

streamReader=newInputStreamReader(inputStream);

reader=newBufferedReader(streamReader);

Stringline="";

while((line=reader.readLine())!=null){
lines.add(line);
}
}finally{
if(reader!=null){
reader.close();
}
if(streamReader!=null){
streamReader.close();
}
if(inputStream!=null){
inputStream.close();
}
}

returnlines;
}
}

⑦ java多層list實現圖書的增刪改查

import java.util.ArrayList;
import java.util.List;
/**
* 測試類
*
* @author 時間在流
*
*/
public class Test {
/**
* 這個屬性用來模仿書房
*/
private List<List<String>> room = new ArrayList<List<String>>();
/**
* 向書房裡添加一個書架
*
* @return 新添加書架的索引
*/
public int addBookShelf() {
room.add(new ArrayList<String>());
return room.size() - 1;
}
/**
* 向書架里添加一本書
*
* @param bookShelfIndex 書架索引
* @param book 書。這里就用一個字元串代表一本書了,你要願意也可以單獨創建一個書類
* @return 新添加書籍的索引
*/
public int addBook(int bookShelfIndex, String book) {
List<String> shelf = room.get(bookShelfIndex);
shelf.add(book);
return shelf.size() - 1;
}
// 更新書架我就不寫了。
/**
* 更新書架上的某本書
*
* @param bookShelfIndex 書架索引
* @param bookIndex 舊書索引
* @param newBook 新書
*/
public void setBook(int bookShelfIndex, int bookIndex, String newBook) {
List<String> shelf = room.get(bookShelfIndex);
shelf.set(bookIndex, newBook);
}
/**
* 移除一個書架
*
* @param bookShelfIndex 書架索引
* @return 剩餘書架的個數
*/
public int removeBookShelf(int bookShelfIndex) {
room.remove(bookShelfIndex);
return room.size();
}
/**
* 移除書架上的某本書
*
* @param bookShelfIndex 書架索引
* @param bookIndex 書籍索引
* @return 該書架剩餘書的數量
*/
public int removeBook(int bookShelfIndex, int bookIndex) {
List<String> shelf = room.get(bookShelfIndex);
shelf.remove(bookIndex);
return shelf.size();
}
/**
* 獲得一個書架
*
* @param bookShelfIndex 書架索引
* @return 書架
*/
public List<String> getBookShelf(int bookShelfIndex) {
return room.get(bookShelfIndex);
}
/**
* 獲得書架上的某本書。在現實生活中拿走了一本書,書架上應該會少一本
*
* @param bookShelfIndex 書架索引
* @param bookIndex 書籍索引
* @return 書籍
*/
public String getBook(int bookShelfIndex, int bookIndex) {
List<String> shelf = room.get(bookShelfIndex);
return shelf.get(bookIndex);
}
public static void main(String[] args) {
Test test = new Test();
int shelf1Index = test.addBookShelf();
int shelf2Index = test.addBookShelf();
int book1Index = test.addBook(shelf1Index, "鬼吹燈");
int book2Index = test.addBook(shelf1Index, "盜墓筆記");
int book3Index = test.addBook(shelf2Index, "斗破蒼穹");
int book4Index = test.addBook(shelf2Index, "斗羅大陸");
test.setBook(shelf2Index, book4Index, "吞噬星空");
System.out.println("2號書架的第2本書是" + test.getBook(shelf2Index, book4Index));
System.out.println("移除了2號書架的第2本書,2號書架還剩" + test.removeBook(shelf2Index, book4Index) + "本書");
System.out.println("移除了第2個書架,現在還剩" + test.removeBookShelf(shelf2Index) + "個書架");
List<String> shelf = test.getBookShelf(shelf1Index);
System.out.println("1號書架書籍列表:");
for (String book : shelf) {
System.out.println(book);
}
}
}

⑧ Java 點擊按鈕實現數據的增刪改查。不用資料庫。

jsp中的增刪改查是通過操作特定條件來實現動態改變Map的值來實現的。
比如根據部門id來查詢:
定義一個存放部門信息的map,如下:
Map<integer,Dept> map=new HashMap<integer,Dept>();
查詢方法:
Iterator keys = map.keySet().iterator();
while(keys.hasNext()){
String key = (String)keys.next();
if("id".equals(key)){
System.out.println("id為xx的員工信息");
}
}
添加是這樣的map.put("001",new Dept());
刪除是這樣的map.remove();
更新是直接用map.put(key,value),這個方法會覆蓋原來這個key對應的值,就相當於更新了

⑨ 如何對JAVA字元串中的字元進行增刪操作

public static void main(String[] args) {
String names[]={"語文","數學","英語","政治","歷史","物理","化學"};
String a="";
for(int i=0;i<names.length;i++ ){//遍歷字元數組,如果有元素等於"語文";則置為null
a=names[i];
if(a.equals("語文"))
names[i]=null;
}
System.out.println(" 刪除後的字元數組輸出 ");
for(int i=0;i<names.length;i++){//輸出字元數組names,如果為空則不輸出此元素
if(names[i]!=null)
System.out.print(names[i]+" ");
}
System.out.println(" \n names字元數組復制到names2 ");

int x=names.length;
String names2[]=new String[2*x];
for(int i=0;i<names.length;i++){//定義字元串數組names2,令他的長度為names的2倍(便於復制,不會越界異常)
if(names[i]!=null)
names2[i]=names[i];
}
for(int i=0;i<names2.length;i++){//在字元串數組names2中增加元素,找到第一個是null的地方,把要添加的元素放進去,利用break終止循環,不在找下去
if(names2[i]==null)
names2[i]="體育";
break;
}
System.out.println(" \n 添加後的字元數組輸出 ");
for(int i=0;i<names2.length;i++){//對字元串數組names2遍歷輸出,過濾掉空元素!
if(names2[i]!=null)
System.out.print(names2[i]+" ");

}
//上面所有方法都在一個main方法內,樓主可考慮寫不同的方法實現,並互相調用,更省代碼,和易讀!
}

程序運行結果控制台輸出如下

刪除後的字元數組輸出
數學 英語 政治 歷史 物理 化學
names字元數組復制到names2

添加後的字元數組輸出
體育 數學 英語 政治 歷史 物理 化學

⑩ java 集合的增刪排查

JAVA的集合主要是List,Map,Set.他們都是介面.%D%AList的實現類主要是:ArrayList,Vector,LinkedList.%D%A ArrayList:底層使用數組實現;隨機讀取數據較快;%D%A Vector:和ArrayList一樣,只是方法都是同步的;%D%A LinkedList:使用鏈表實現;插入,刪除數據較快.%D%AMap的實現類主要是:HashMap,LinkedHashMap,TreeMap.%D%A HashMap:以對象的哈希值來存取對象,允許一個null鍵和多個null值;%D%A Hashtable:與HashMap一樣,只是方法都是同步的,而且不允許任何null值和鍵;%D%A LinkedHashMap:(和HashMap的區別與ArrayList和LinkedList的區別類似);%D%A TreeMap:存入的對象是以自然或自定義方式排序,即當我們插入對象後,所以的對象在該集合里都是有序的(默認是自然順序的升序).%D%ASet的實現類主要是:HashSet,LinkedHashSet,TreeSet.%D%A HashSet:未排序的Set;%D%A LinkedHashSet:和HashSet一樣,只是已排序;%D%A TreeSet:以一種樹形結構進行排序(紅-黑樹).%D%A在JDK1.4中,集合可以存儲任何類型的對象,如List可以存String對象,也可以是Integer對象,但是這會帶來安全問題.由於我們不知道對象的類型,當我們取出List中的對象時,若取出的對象是Integer,而讓其調用charAt()方法,就會出現異常,因為Integer對象沒有charAt()方法.%D%A於是在JDK1.5中引入了泛型的概念.如:%D%A List<String> list=new ArrayList<String>();%D%A上面的語句說明只能存入String類型的對象,若試圖存放Integer類型的對象就會出現錯誤.%D%A通用模式:%D%A List<T> list=new ArrayList<T>();%D%A LinkedList<T> linklist=new LinkedList<T>();%D%A Map<K,V> map=new HashMap<K,V>();%D%A Set<T> set=new TreeSet<T>();%D%AT,K代表任何類型,但不能是基本數據類型(int,long等都不可以!),如:String,Integer.說明只能存入該類型的對象.%D%A在你提到的public List list_querry(){}方法中,也可以是這么的:%D%A public List<String> list_querry(){}%D%A我這里是假設你返回的List中只有String類型的對象.同樣public boolean ADDUsers(Map map){}%D%A public boolean ADDUsers(Map<String> map){}

熱點內容
小翼管家如何查看密碼 發布:2024-04-19 09:57:31 瀏覽:156
怎麼緩存小品 發布:2024-04-19 09:49:02 瀏覽:410
在系統編程 發布:2024-04-19 08:54:55 瀏覽:235
visualstudio反編譯 發布:2024-04-19 08:44:46 瀏覽:320
ise怎麼配置晶元 發布:2024-04-19 08:27:31 瀏覽:997
免費搭建在線查詢伺服器 發布:2024-04-19 08:17:28 瀏覽:46
vs資料庫實例 發布:2024-04-19 08:14:54 瀏覽:295
vfp9反編譯 發布:2024-04-19 08:11:31 瀏覽:381
火車軟卧無線密碼是多少 發布:2024-04-19 07:38:59 瀏覽:423
vb系統文件夾 發布:2024-04-19 07:29:58 瀏覽:740