JDBC增刪查改(原創(chuàng),親測)

JDBC增刪查改(原創(chuàng),親測)

ID:37852498

大?。?7.00 KB

頁數(shù):4頁

時間:2019-06-01

JDBC增刪查改(原創(chuàng),親測)_第1頁
JDBC增刪查改(原創(chuàng),親測)_第2頁
JDBC增刪查改(原創(chuàng),親測)_第3頁
JDBC增刪查改(原創(chuàng),親測)_第4頁
資源描述:

《JDBC增刪查改(原創(chuàng),親測)》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。

1、jdbc操作mysql數(shù)據(jù)庫(原創(chuàng),親測)數(shù)據(jù)庫是mysql數(shù)據(jù)的表名是stu其中的字段是:number(int主鍵自增)Name(vachar(30))Hometown(vachar(30))Age(int)使用四個類來封裝操作1.數(shù)據(jù)庫連接類myconnection2.關(guān)閉數(shù)據(jù)庫連接類myclose3.數(shù)據(jù)操作類myoperation4.測試類mytest1.數(shù)據(jù)庫連接類importjava.sql.*;publicclassmyconnection{publicConnectiongetconnect(){Connectioncon=null;try{Class.for

2、Name("org.gjt.mm.mysql.Driver");}catch(ClassNotFoundExceptione){System.out.println(e);}try{con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}System.out.println("連接成功");returncon;}}2.關(guān)閉數(shù)據(jù)

3、庫連接類importjava.sql.*;publicclassmyclose{publicstaticvoidclose(ResultSetrs,Statementstmt,Connectionconn){close(rs);close(stmt);close(conn);}publicstaticvoidclose(Statementstmt,Connectionconn){close(stmt);close(conn);}publicstaticvoidclose(ResultSetrs){try{if(rs!=null){rs.close();}}catch(Exce

4、ptione){e.printStackTrace();}}publicstaticvoidclose(Statementstmt){try{if(stmt!=null){stmt.close();}}catch(Exceptione){e.printStackTrace();}}publicstaticvoidclose(Connectioncon){try{if(con!=null){con.close();}}catch(Exceptione){e.printStackTrace();}System.out.println("關(guān)閉connection對象成功");}}3

5、.數(shù)據(jù)庫操作類importjava.sql.*;publicclassmyoperation{publicvoidReadData()throwsSQLException{myconnectionmycon=newmyconnection();Connectioncon=mycon.getconnect();Statementstmt=con.createStatement();Stringsqlstr="select*fromstu";ResultSetrs=stmt.executeQuery(sqlstr);while(rs.next()){System.out.prin

6、tln(rs.getInt(1)+";"+rs.getString(2)+";"+rs.getString(3)+";"+rs.getInt(4)+".");}myclose.close(rs,stmt,con);}publicvoidReadDataByNum(intnum)throwsSQLException{myconnectionmycon=newmyconnection();Connectioncon=mycon.getconnect();Statementstmt=con.createStatement();Stringsqlstr="select*fromstu

7、wherenumber="+num;ResultSetrs=stmt.executeQuery(sqlstr);while(rs.next()){System.out.println(rs.getInt(1)+";"+rs.getString(2)+";"+rs.getString(3)+";"+rs.getInt(4)+".");}myclose.close(rs,stmt,con);}publicvoidinsertData(Stringname,Stringhome,intage)throwsSQ

當前文檔最多預覽五頁,下載文檔查看全文

此文檔下載收益歸作者所有

當前文檔最多預覽五頁,下載文檔查看全文
溫馨提示:
1. 部分包含數(shù)學公式或PPT動畫的文件,查看預覽時可能會顯示錯亂或異常,文件下載后無此問題,請放心下載。
2. 本文檔由用戶上傳,版權(quán)歸屬用戶,天天文庫負責整理代發(fā)布。如果您對本文檔版權(quán)有爭議請及時聯(lián)系客服。
3. 下載前請仔細閱讀文檔內(nèi)容,確認文檔內(nèi)容符合您的需求后進行下載,若出現(xiàn)內(nèi)容與標題不符可向本站投訴處理。
4. 下載文檔時可能由于網(wǎng)絡波動等原因無法下載或下載錯誤,付費完成后未能成功下載的用戶請聯(lián)系客服處理。