创建实体类packagecom.kjzz;publicclassBaseDao{privateintBookID;私有字符串书名;私有int类型ID;私人双倍价格;私有字符串发布;私有字符串StateID;publicBaseDao(){}publicBaseDao(intbookID,StringbookName,inttypeID,Doubleprice,Stringpublish,StringstateID){BookID=bookID;书名=书名;类型ID=类型ID;价格=价格;发布=发布;状态ID=状态ID;}publicintgetBookID(){返回BookID;}publicvoidsetBookID(intbookID){BookID=bookID;}publicStringgetBookName(){返回书名;}publicvoidsetBookName(StringbookName){BookName=bookName;}publicintgetTypeID(){返回TypeID;}publicvoidsetTypeID(inttypeID){TypeID=typeID;}publicDoublegetPrice(){返回价格;}publicvoidsetPrice(Doubleprice){价格=价格;}publicStringgetPublish(){返回发布;}publicvoidsetPublish(Stringpublish){Publish=publish;}publicStringgetStateID(){返回StateID;}publicvoidsetStateID(StringstateID){StateID=stateID;}@OverridepublicStringtoString(){return"BaseDao{"+"BookID="+BookID+",BookName='"+BookName+'\''+",TypeID="+TypeID+",Price="+Price+",Publish='"+Publish+'\''+",StateID='"+StateID+'\''+'}';}}连接数据库packagecom.kjzz;importjava.sql.*;publicclassDBUtils{publicstaticStringDRIVER="com.mysql.jdbc.Driver";publicstaticStringURl="jdbc:mysql://localhost:3306/book";公共静态字符串用户=“根”;publicstaticStringPWd="root";//加载驱动static{try{Class.forName(DRIVER);}catch(ClassNotFoundExceptione){e.printStackTrace();}}//连接方法publicstaticConnectionConnection(){Connectionconn=null;尝试{conn=DriverManager.getConnection(URl,USER,PWd);}catch(SQLExceptione){e.printStackTrace();}返回康恩;}//测试publicstaticvoidmain(String[]args){Connectionconn=Connection();System.out.println("连接对象是"+conn);}//关闭publicstaticvoidCloseConn(Connectionconn,PreparedStatementprep,ResultSetrs){try{if(conn!=null){conn.close();}if(prep!=null){prep.close();}if(rs!=null){rs.close();}}catch(SQLExceptione){e.printStackTrace();}}}增加修改的运行包com.kjzz;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;publicclassZSGC{publicstaticConnectionconn=null;publicstaticPreparedStatementprep=null;publicstaticResultSetrs=null;//查询publicListChaXun(){Listlist=newArrayList<>();StringSelectSql="select*fromok";尝试{conn=DBUtils.Connection();prep=conn.prepareStatement(SelectSql);rs=prep.executeQuery();//查询返回的结果集while(rs.next()){BaseDaodao=newBaseDao();intBookID=rs.getInt("BookID");StringBookName=rs.getString("书名");整型类型ID=rs.getInt("TypeID");双倍价格=rs.getDouble("价格");字符串发布=rs.getString("发布");StringStateID=rs.getString("StateID");dao=newBaseDao(BookID,BookName,TypeID,Price,Publish,StateID);list.add(道);}}catch(SQLExceptione){e.printStackTrace();}返回列表;}//增加publicstaticbooleanInsert(BaseDaodao){intnum=0;StringInsetSql="insertintookvalues(?,?,?,?,?,?)";尝试{conn=DBUtils.Connection();prep=conn.prepareStatement(InsetSql);prep.setInt(1,dao.getBookID());prep.setString(2,dao.getBookName());prep.setInt(3,dao.getBookID());prep.setDouble(4,dao.getPrice());prep.setString(5,dao.getPublish());prep.setString(6,dao.getStateID());num=prep.executeUpdate();}catch(SQLExceptione){e.printStackTrace();}返回数>0;}//删除publicstaticbooleanDelete(intBookID){intnum=0;StringDeleteSql="从ok中删除BookID=?";尝试{conn=DBUtils.Connection();prep=conn.prepareStatement(DeleteSql);prep.setInt(1,BookID);num=prep.executeUpdate();}catch(SQLExceptione){e.printStackTrace();}返回数>0;}//修改publicstaticbooleanUpdate(BaseDaodao){intnum=0;StringUpdateSql="updateoksetBookName=?,TypeID=?,Price=?,Publish=?,StateID=?whereBookID=?";尝试{conn=DBUtils.Connection();prep=conn.prepareStatement(UpdateSql);prep.setString(1,dao.getBookName());prep.setInt(2,dao.getTypeID());prep.setDouble(3,dao.getPrice());prep.setString(4,dao.getPublish());prep.setString(5,dao.getStateID());prep.setInt(6,dao.getBookID());num=prep.executeUpdate();}catch(SQLExceptione){e.printStackTrace();}返回数>0;}}测试包com.kjzz;importorg.junit.Test;importjava.util.List;publicclassTest01{@Testpublicvoidtest01(){//添加BaseDaodao=newBaseDao(66,"节",1001,2456.0,"成都府","接触");ZSGC.插入(道);}@Test//查询publicvoidtest02(){ZSGCzsgc=newZSGC();Listlist=zsgc.ChaXun();for(BaseDaodao:list){System.out.println(dao.getBookID()+"");System.out.println(dao.getBookName()+"");System.out.println(dao.getTypeID()+"");System.out.println(dao.getPrice()+"");系统.out.println(dao.getPublish()+"");System.out.println(dao.getStateID()+"");}}@Test//删除publicvoidtest03(){booleanDelete=ZSGC.Delete(1);}@Test//修改publicvoidtest05(){BaseDaoDao2=newBaseDao();Dao2.setBookID(66);Dao2.setBookName("胡杨");Dao2.setTypeID(0000);Dao2.setPrice(0.0);Dao2.setPublish("美国出版");Dao2.setStateID("联系人");布尔更新=ZSGC.Update(Dao2);}}