实验七Jdbc编程1.实验目的(1)掌握通过JDBC方式操作数据库的基本步骤。(2)掌握增、删、改、查记录等的方法。(3)掌握查询记录以及遍历查询结果的方法。2.实验内容实验题1学生信息管理。创建student表,包含学生的学号、姓名、年龄信息。①根据学号,可以查询到学生的姓名和年龄;②给定学生的学号、姓名、年龄,在表中追加一行信息;③给定学生的学号,可以从表中删除该学生的信息;[基本要求]对上面的每一个功能编写相应的函数,并测试。1.创建Student表packageedu.cn.jp.text;importjava.util.Scanner;importedu.cn.jp.data.ConnectionBean;importedu.cn.jp.data.ConnectionWay;publicclasstext{/***@paramargs*/publicstaticvoidmain(String[]args){ConnectionBeancb=newConnectionBean();//加载驱动ConnectionWaycw=newConnectionWay();Scannerscan=newScanner(System.in);Stringnumber=null;Stringname=null;Stringage=null;//调用方法测试:number=scan.next();name=scan.next();age=scan.next();cw.numberQuery(number);cw.insert(2019,name,age);cw.delete(number);//关闭连接:cw.free();}2.增删改查}packageedu.cn.jp.data;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassConnectionWay{Connectionconn=ConnectionBean.getConnection();//连接PreparedStatementpst=null;ResultSetrs=null;publicvoidnumberQuery(Stringnumber){Stringsql=selectSname,SagefromSwherenumber=?;try{pst=conn.prepareStatement(sql);pst.setString(1,number);rs=pst.executeQuery();while(rs.next()){System.out.println(rs.getString(Sname)++rs.getString(Sage));}}catch(SQLExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}publicvoidinsert(Stringnumber,StringSname,StringSage){intcol=0;Stringsql=insertintoSvalues(?,?,?);try{pst=conn.prepareStatement(sql);pst.setString(1,number);pst.setString(2,Sname);pst.setString(3,Sage);col=pst.executeUpdate();if(col==1){System.out.println(插入数据成功!);}}catch(SQLExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}publicvoiddelete(Stringnumber){intcol=0;Stringsql=deletefromSwherenumber=?;try{pst=conn.prepareStatement(sql);pst.setString(1,number);col=pst.executeUpdate();if(col!=0)System.out.println(删除成功!);}catch(SQLExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}publicvoidfree(){ConnectionBean.free(rs,pst,conn);}}3.遍历packageedu.cn.jp.data;importjava.sql.PreparedStatement;importjava.sql.Statement;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassConnectionBean{privatestaticStringurl=jdbc:mysql://localhost:3306/Student;privatestaticStringuser=root;privatestaticStringpassWord=;static{try{Class.forName(com.mysql.jdbc.Driver);}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticConnectiongetConnection(){Connectionconn=null;try{conn=(Connection)DriverManager.getConnection(url,user,null/*,passWord*/);}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnconn;}publicstaticvoidfree(ResultSetrs,Statementst,Connectionconn){try{rs.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{try{st.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{try{conn.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}}实验题2Jdbc编程。在MySql数据库的test库中,建一个表student,其内容如下表所示,将表中score大于60的记录的信息输出到控制台。学生信息表IdNameGenderScore1002Tommale801003Maryfemale891004Petermale541005Johnmale761006polofemale90packageedu.cn.jp.text;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importedu.cn.jp.ConnectionBean;publicclasstext{/***@paramargs*/publicstaticvoidmain(String[]args){Connectionconn=null;PreparedStatementpst=null;ResultSetrs=null;conn=ConnectionBean.getConnection();try{floatscore=0f;pst=conn.prepareStatement(select*fromstudent);rs=pst.executeQuery();while(rs.next()){score=rs.getFloat(Score);if(score60){System.out.println(rs.getObject(1)+,+rs.getObject(2)+,+rs.getObject(3)+,+rs.getObject(4));}}}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}ConnectionBean.free(rs,pst,conn);}}packageedu.cn.jp;importjava.sql.PreparedStatement;importjava.sql.Statement;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassConnectionBean{privatestaticStringurl=jdbc:mysql://localhost:3306/student;privatestaticStringuser=root;privatestaticStringpassWord=null;static{try{Class.forName(com.mysql.jdbc.Driver);}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticConnectiongetConnection(){Connectionconn=null;try{conn=(Connection)DriverManager.getConnection(url,user,passWord);}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnconn;}publicstaticvoidfree(ResultSetrs,Statementst,Connectionconn){try{rs.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{try{st.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{try{conn.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}}*实验题3(选作题)熟悉课堂内容,学习掌握数据库访问编程实现的程序结构。(1)设计一个与连接数据库相关接口UserDao(访问数据库——增、删、改、查——DAO(DataAccessObject,数据访问对象),一般针对不同的数据库表采用不同的DAO功能实现类。)(2)设计持久实体类(PO