毛片在线视频观看,一级日韩免费大片,在线网站黄色,澳门在线高清一级毛片

薈聚奇文、博采眾長(zhǎng)、見(jiàn)賢思齊
當(dāng)前位置:公文素材庫(kù) > 公文素材 > 范文素材 > SQL增刪改查例子

SQL增刪改查例子

網(wǎng)站:公文素材庫(kù) | 時(shí)間:2019-05-29 22:28:45 | 移動(dòng)端:SQL增刪改查例子

SQL增刪改查例子

數(shù)據(jù)庫(kù)連接與關(guān)閉類(lèi)

Connectionconn=null;

importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.*;

importjava.sql.SQLException;

publicclassDBConnection{

publicstaticConnectiongetConnection()throws}

publicstaticvoidcloseResource(ResultSetrs,Statement

if(rs!=null){}

if(stmt!=null){

try{try{}

rs.close();rs=null;

e.printStackTrace();

Connectionconn=null;try{

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

Stringuser="sa";

Stringpassword="123456";

conn=DriverManager.getConnection(url,user,password);//System.out.println("數(shù)據(jù)庫(kù)連接成功!");}catch(ClassNotFoundExceptione)

{System.out.println("數(shù)據(jù)庫(kù)連接失。"+e.getMessage());}catch(SQLExceptionex){

System.out.println("SQLException:"+ex.getMessage());System.out.println("SQLState:"+ex.getSQLState());}

returnconn;

SQLException,IllegalAccessException{

PreparedStatementps=null;ResultSetrs=null;Stringsql="";

="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cwgsj";

stmt,Connectionconn){

}catch(SQLExceptione){}

}}

stmt.close();stmt=null;

e.printStackTrace();

}catch(SQLExceptione){

if(conn!=null){

try{

conn.close();conn=null;

System.err.println(ex.getMessage());ex.printStackTrace();

}catch(SQLExceptionex){

System.out.println("-------------------數(shù)據(jù)庫(kù)關(guān)閉失。");}

}}

查詢某單個(gè)字段

//根據(jù)采購(gòu)單編號(hào)查找采購(gòu)單是否屬于大類(lèi)型publicStringfindBigFlag(Stringbuyid){

Stringbigflag="";try{}

}catch(Exceptione){

ps=conn.prepareStatement(sql);ps.setString(1,buyid);rs=ps.executeQuery();if(rs.next()){

bigflag=rs.getString(1);

conn=DBConnection.getConnection();

Stringsql="selectisbigfromapplywherebuyid=?";

System.out.println("查找該采購(gòu)申請(qǐng)是否是大宗采購(gòu),ConPurchaseDAO");

}}

returnbigflag;

e.printStackTrace();

DBConnection.closeResource(rs,ps,conn);}finally{查詢所有(對(duì)象集合)

//根據(jù)商品大類(lèi)編號(hào)查找小類(lèi)編號(hào)publicListgetGoodstypeBysmall(Stringsmall){

}

Listlist=newArrayList();try{}

returnlist;

conn=DBConnection.getConnection();

Stringsql="selectgoodsid,goodsnamefromgoodstypewhereps=conn.prepareStatement(sql);ps.setString(1,small);rs=ps.executeQuery();while(rs.next()){}

e.printStackTrace();

DBConnection.closeResource(rs,ps,conn);

Goodstypegtype=newGoodstype();

gtype.setGoodsid(rs.getString("goodsid"));gtype.setGoodsname(rs.getString("goodsname"));list.add(gtype);

smallgoodsid=?";

}catch(Exceptione){}finally{

//查找流程表flow里級(jí)別為id的崗位編號(hào)及流程編號(hào)publicFlowfindFlowGweiByLevel(intid){

Flowflow=newFlow();

//Listlist=newArrayList();//System.out.println("id"+id);try{

conn=DBConnection.getConnection();

Stringsql="selectflowid,gweiidfromflowwhere//System.out.println(sql);ps=conn.prepareStatement(sql);ps.setInt(1,id);rs=ps.executeQuery();

flow.setFlowid(rs.getString(1));flow.setGweiid(rs.getString(2));

flowtypeid="f002"andlevelflag=?";

if(rs.next()){}

}

}catch(Exceptione){}

returnflow;

System.out.println("根據(jù)流程標(biāo)號(hào)查找出錯(cuò)");e.printStackTrace();

DBConnection.closeResource(rs,ps,conn);

}finally{

增加

//增加采購(gòu)申請(qǐng)單publicintaddApply(Applyapply){

}}

returnflag;

System.out.println("添加申請(qǐng)單的buyapply.jsp出錯(cuò)");e.printStackTrace();

DBConnection.closeResource(null,ps,conn);

conn=DBConnection.getConnection();Stringsql="insertinto

ps=conn.prepareStatement(sql);ps.setString(1,apply.getBuyid());ps.setString(2,apply.getWriteman());ps.setString(3,apply.getApplytime());ps.setString(4,apply.getIsbig());flag=ps.executeUpdate();

intflag=-1;try{

apply(buyid,writeman,applytime,isbig)values(?,?,?,?)";

}catch(Exceptione){

}finally{

更新

publicintupdateAppreceive(Appreceiveapp,intid){

conn=DBConnection.getConnection();

Stringsql="updateappreceiveset

intflag=-1;try{userid=?,ideas=?,examtime=?,isexam="1"whereid=?";

flag=ps.executeUpdate();ps.setString(2,app.getIdeas());ps.setString(3,app.getExamtime());ps.setInt(4,id);

ps.setString(5,flowid);ps.setString(6,gweiid);

ps=conn.prepareStatement(sql);ps.setString(1,app.getUserid());

//////

}

}catch(Exceptione){}

returnflag;

System.out.println("添加采購(gòu)單的.jsp出錯(cuò)");e.printStackTrace();

DBConnection.closeResource(null,ps,conn);

}finally{

刪除

//刪除被勾選的采購(gòu)單申請(qǐng)記錄

publicbooleandelPurchase(String[]id){

for(inti=0;i}

}

}finally{}

returnflag;

DBConnection.closeResource(rs,ps,conn);

擴(kuò)展閱讀:SQl常用增刪改查

SQL常用增刪改查語(yǔ)句

增加

現(xiàn)在有一張表,表(Test)里面有三個(gè)字段,分別為sno,sname,age。舉例用一條增加SQL語(yǔ)句,插入一條數(shù)據(jù)進(jìn)庫(kù)。

語(yǔ)句:Insertinto表名value(‘?dāng)?shù)據(jù)1’,’數(shù)據(jù)2’,’數(shù)據(jù)3’)

具體操作:

Insertintotestvalues("test","test","1")

通過(guò)上面這條語(yǔ)句,Test表里面就多了一條數(shù)據(jù)。如下圖所示:

上面這個(gè)例子,是在每條字段都需要插入的時(shí)候?yàn)榱朔奖愣苯釉趇nsertinto后面跟表名。但是也會(huì)遇到一些特殊的情況,比如一張表,因?yàn)橛兄魍怄I約束(我這里只有一張表),而我只想插入被約束的字段sno(主鍵)加上age這個(gè)字段,在insertinto的時(shí)候就需要指明需要插入的字段,下面舉例說(shuō)明:

語(yǔ)句:Insertinto表名(‘字段名1’,’字段名2’)values(‘?dāng)?shù)據(jù)1’,’數(shù)據(jù)2’)

具體操作:

insertintotest(sno,age)values("彭宇","21")

這樣數(shù)據(jù)庫(kù)里面,又多了一條數(shù)據(jù),而沒(méi)有插入任何數(shù)據(jù)那個(gè)字段默認(rèn)為NULL。如下圖所示:

刪除

在我們?cè)黾訑?shù)據(jù)入庫(kù)的時(shí)候,難免會(huì)出現(xiàn)數(shù)據(jù)錄入錯(cuò)誤,或者信息過(guò)期后不再需要的數(shù)據(jù),所以我們要利用刪除語(yǔ)句將表里面不需要的數(shù)據(jù)刪除掉。下面舉例說(shuō)明。語(yǔ)句:Deletefrom表名where字段名="需要?jiǎng)h除的數(shù)據(jù)"

具體操作:

deletefromtestwheresno="test"

通過(guò)這條SQL語(yǔ)句,Test表主鍵sno字段里面數(shù)據(jù)為test的該條數(shù)據(jù)就已經(jīng)被刪除了。

Ps:一般來(lái)說(shuō)都以主鍵為條件進(jìn)行刪除,因?yàn)橹麈I是不可重復(fù)的,我們可以設(shè)

想一下,如果沒(méi)使用主鍵為刪除條件,假設(shè)一個(gè)公司有兩個(gè)叫彭宇的人。我使用sname=’彭宇’作為刪除條件的話,那么這兩個(gè)同名同姓人的資料都會(huì)被刪除掉,所以這是不可取的。

批量刪除

當(dāng)有多條數(shù)據(jù)需要?jiǎng)h除的時(shí)候,我們可以使用批量刪除語(yǔ)句來(lái)實(shí)現(xiàn)一次刪除多條數(shù)據(jù)。

語(yǔ)句:

deletefrom表名where字段名in("該字段里面的數(shù)據(jù)1","該字段里面的數(shù)據(jù)2",……)

具體操作:

首先,看一下Test表里面有多少條數(shù)據(jù),如下圖:

現(xiàn)在我想利用一條SQL語(yǔ)句,將前三條數(shù)據(jù)刪除掉。

deletefromtestwheresnoin("test","test2","test3")

通過(guò)執(zhí)行這條SQL語(yǔ)句后,前三條數(shù)據(jù)已經(jīng)被我批量刪除了。

修改一條已經(jīng)錄入數(shù)據(jù)庫(kù)里面的數(shù)據(jù)如果需要更新、修正,我們就需要用到SQL修改語(yǔ)句。

語(yǔ)句:

Update表名set字段="修改后的數(shù)據(jù)"where字段="修改條件"

具體操作:

Updatetestsetsno="SQL修改語(yǔ)句"wheresno="test"

修改前后比較,下圖所示:

(修改前)(修改后)

查詢

上面進(jìn)行了增加,修改操作后,數(shù)據(jù)庫(kù)里面已經(jīng)存在有數(shù)據(jù)了,最后我們要利用SQL查詢語(yǔ)句將它們查詢并顯示出來(lái)。

全部查詢

語(yǔ)句:

Select*from表名

具體操作:

Select*fromtest

執(zhí)行了上面這句話,那么test表里面存在的數(shù)據(jù)都會(huì)被查詢出來(lái),如果我想要單獨(dú)查詢出某個(gè)人的數(shù)據(jù)怎么辦?很簡(jiǎn)單,只需要加上一個(gè)關(guān)鍵詞where就能夠?qū)崿F(xiàn)了。

單條件查詢

語(yǔ)句:

Select*from表名where字段=’需要查詢的數(shù)據(jù)’

具體操作:

Select*fromtestwheresno=’彭宇’

這樣我就查詢出數(shù)據(jù)庫(kù)里面sno字段為彭宇的數(shù)據(jù)了。多條件查詢

多條件查詢就是比起單條件查詢多了一個(gè)and關(guān)鍵詞,使用多條件查詢,查出來(lái)的結(jié)構(gòu)能夠更加的精確。

語(yǔ)句:

Select*from表名where字段=’需要查詢的數(shù)據(jù)’and字段=’需要查詢的數(shù)據(jù)’

具體操作:

Select*fromtestwheresno=’彭宇’’21’

andage=

友情提示:本文中關(guān)于《SQL增刪改查例子》給出的范例僅供您參考拓展思維使用,SQL增刪改查例子:該篇文章建議您自主創(chuàng)作。

來(lái)源:網(wǎng)絡(luò)整理 免責(zé)聲明:本文僅限學(xué)習(xí)分享,如產(chǎn)生版權(quán)問(wèn)題,請(qǐng)聯(lián)系我們及時(shí)刪除。


SQL增刪改查例子》由互聯(lián)網(wǎng)用戶整理提供,轉(zhuǎn)載分享請(qǐng)保留原作者信息,謝謝!
鏈接地址:http://www.seogis.com/gongwen/747393.html
相關(guān)文章