JDBC增删查改(原创,亲测)

JDBC增删查改(原创,亲测)

ID:37852498

大小:27.00 KB

页数:4页

时间:2019-06-01

JDBC增删查改(原创,亲测)_第1页
JDBC增删查改(原创,亲测)_第2页
JDBC增删查改(原创,亲测)_第3页
JDBC增删查改(原创,亲测)_第4页
资源描述:

《JDBC增删查改(原创,亲测)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、jdbc操作mysql数据库(原创,亲测)数据库是mysql数据的表名是stu其中的字段是:number(int主键自增)Name(vachar(30))Hometown(vachar(30))Age(int)使用四个类来封装操作1.数据库连接类myconnection2.关闭数据库连接类myclose3.数据操作类myoperation4.测试类mytest1.数据库连接类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.关闭数据

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("关闭connection对象成功");}}3

5、.数据库操作类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. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。