![[原创]用JAVA来实现对数据库中信息的增删改查_第1页](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/cb2c69e4-d475-41c0-b99d-86f24d920aa0/cb2c69e4-d475-41c0-b99d-86f24d920aa01.gif)
![[原创]用JAVA来实现对数据库中信息的增删改查_第2页](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/cb2c69e4-d475-41c0-b99d-86f24d920aa0/cb2c69e4-d475-41c0-b99d-86f24d920aa02.gif)
![[原创]用JAVA来实现对数据库中信息的增删改查_第3页](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/cb2c69e4-d475-41c0-b99d-86f24d920aa0/cb2c69e4-d475-41c0-b99d-86f24d920aa03.gif)
![[原创]用JAVA来实现对数据库中信息的增删改查_第4页](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/cb2c69e4-d475-41c0-b99d-86f24d920aa0/cb2c69e4-d475-41c0-b99d-86f24d920aa04.gif)
![[原创]用JAVA来实现对数据库中信息的增删改查_第5页](http://file2.renrendoc.com/fileroot_temp3/2021-7/15/cb2c69e4-d475-41c0-b99d-86f24d920aa0/cb2c69e4-d475-41c0-b99d-86f24d920aa05.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 原创用JAVA来实现对数据库中信息地增删改查.txt6宽容润滑了彼此地关系,消除了彼此地隔阂,扫清了彼此地顾忌,增进了彼此地了解.这是前几天地习题,是一个宠物管理系统,就是对宠物地信息进行管理地一套系统宠物信息:宠物ID,宠物类别,宠物名字,宠物性别,宠物年龄,宠物入库日期系统完成功能:实现对宠物信息地录入,修改,删除,查询解决方法:一共创建了四个类:一个宠物类PetMessage 里面是宠物地信息 一个是数据库连接类DBCon 里面主要是完成数据连接功能 一个是宠物管理类PetDAO 完成对宠物信息地增删该查 最后一个就是一个测试类PetTest 完成对系统地测试PetMessage类:p
2、ackage petsys.Pet;import java.sql.*;public class PetMessage /宠物信息private int petId;private String petSort;private String petName;private String petSex;private int petAge;private String petDate;public int getPetAge() return petAge;public void setPetAge(int petAge) this.petAge = petAge;public int getP
3、etId() return petId;public void setPetId(int petId) this.petId = petId;public String getPetName() return petName;public void setPetName(String petName) this.petName = petName;public String getPetSex() return petSex;public void setPetSex(String petSex) this.petSex = petSex;public PetMessage() public
4、String getPetDate() return petDate;public void setPetDate(String petDate) this.petDate = petDate;public String getPetSort() return petSort;public void setPetSort(String petSort) this.petSort = petSort;/重载构造函数public PetMessage(int petId,String petSort,String petName,String petSex,int petAge,String pe
5、tDate) this.petId=petId; this.petSort=petSort; this.petName=petName; this.petSex=petSex; this.petAge=petAge; this.petDate=petDate;DBCon类:package petsys.Pet;import java.sql.*;public class DBCon public static Connection getCon() try Class.forName(com.mysql.jdbc.Driver).newInstance(); Connection con =
6、DriverManager.getConnection(jdbc:mysql:/localhost:3306/mydb,root,root); return con; catch (InstantiationException e) / TODO 自动生成 catch 块 e.printStackTrace(); catch (IllegalAccessException e) / TODO 自动生成 catch 块 e.printStackTrace(); catch (ClassNotFoundException e) / TODO 自动生成 catch 块 e.printStackTra
7、ce(); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); return null;/用方法来实现对对象地关闭public static void closeAllMethod(Connection con,Statement stmt,ResultSet rs) try if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); catch (SQLException e) / TODO 自动生成 catch 块
8、 e.printStackTrace(); PetDAO类:package petsys.Pet;import java.sql.*;public class PetDAO /增添宠物信息public static void doAdd(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate
9、=pet.getPetDate(); /与数据库连接 Connection con=null; con=DBCon.getCon(); /创建会话 try Statement stmt=con.createStatement(); /定义sql语句 String sqlString=insert into petmessage(petid,petsort,petname,petsex,petage,petdate) values(+pet.getPetId()+,+pet.getPetSort()+,+pet.getPetName()+,+pet.getPetSex()+,+pet.getPe
10、tAge()+,+pet.getPetDate()+);/ 执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物添加成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /根据宠物ID删除宠物信息public static void doDeleteWithId(PetMessage p
11、et) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Statement stmt=con.createStatement(); /定义sql语句 String sqlStrin
12、g=delete from petmessage where petid=+pet.getPetId()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物删除成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /根据宠物名字删除宠物信息public static void d
13、oDeleteWithName(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Statement stmt=con.createStatement
14、(); /定义sql语句 String sqlString=delete from petmessage where petName=+pet.getPetName()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物删除成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /
15、根据宠物ID修改宠物信息public static void doUpdateWithID(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Stat
16、ement stmt=con.createStatement(); /定义sql语句 String sqlString=update petmessage set petName=+pet.getPetName()+ where petId=+pet.getPetId()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物信息修改成功!(o); System.out.println(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLE
17、xception e) / TODO 自动生成 catch 块 e.printStackTrace(); /根据宠物名字修改宠物信息public static void doUpdateWithName(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate()
18、; /建立数据库连接 Connection con=DBCon.getCon(); /建立会话 try Statement stmt=con.createStatement(); /定义sql语句 String sqlString=update petmessage set petAge=+pet.getPetAge()+ where petName=+pet.getPetName()+; /执行sql语句 stmt.execute(sqlString); System.out.println(*); System.out.println(宠物信息修改成功!(o); System.out.pr
19、intln(*); DBCon.closeAllMethod(con,stmt,null); catch (SQLException e) / TODO 自动生成 catch 块 e.printStackTrace(); /按ID查询宠物信息public static void doSelectWithId(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int pe
20、tAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); try /创建语句对象 Statement stmt=con.createStatement(); /定义sql语句 String sqlString=select * from petMessage where petId=+pet.getPetId()+; /创建结果集 并执行sql语句 ResultSet rs=stmt.executeQuery(sqlString); /对结果集进行解析 Syste
21、m.out.println(查询结果如下:); while(rs.next() System.out.println(宠物ID: +rs.getInt(petId)+ 宠物种类:+ rs.getString(petSort)+ 宠物名字:+rs.getString(petName)+ 宠物性别:+rs.getString(petSex)+ 宠物年龄:+rs.getInt(petAge)+ 宠物入库时间:+rs.getString(petDate); DBCon.closeAllMethod(con,stmt,rs); catch (SQLException e) / TODO 自动生成 cat
22、ch 块 e.printStackTrace(); /按名字查询宠物信息public static void doSelectWithName(PetMessage pet) int petId=pet.getPetId(); String petSort=pet.getPetSort(); String petName=pet.getPetName(); String petSex=pet.getPetSex(); int petAge=pet.getPetAge(); String petDate=pet.getPetDate(); /建立数据库连接 Connection con=DBCon.getCon(); try /创建语句对象 Statement stmt=con.createStatement(); /定义sql语句 String sqlString=select * from petMessage where petName=+pet.getPetName()+; /创建结果集 并执行sql语句 ResultSet rs=stmt.executeQuery(sqlString); /对结果集进行解析 System.out.println(查
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- JJF 2199-2025数字式时钟校准规范
- 前期策划合同范本
- 养牛设备出售合同范本
- 保障性住房购房合同范本
- 加油卡租车合同范本
- 协议单位优惠合同范例
- 医药物流合同范本
- 修叉车合同范本
- 劳务分包协议合同范本
- 劳务合同范本已填
- 脱硫自动化控制-洞察分析
- 医务人员医德医风培训
- 人教版初中历史八上-第2课 第二次鸦片战争
- 2024湖北省金口电排站管理处招聘易考易错模拟试题(共500题)试卷后附参考答案
- 油井供水合同范例
- 2025年人教部编版语文五年级下册教学计划(含进度表)
- 全国计算机等级考试一级试题及答案(5套)
- 银河证券-科创板认知测评题目及答案
- 产品方案设计模板
- 部队通讯员培训
- 物业公司水浸、水管爆裂事故应急处置预案
评论
0/150
提交评论