BaseDao类(jdbc实现数据库连接,增删改查功能)
package dao2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import entity.pet;
public class BaseDao {
private static Connection getConn() { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123123"; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); System.err.println("连接成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /*增加*/ public static int insert(pet p) { Connection conn = getConn(); int i = 0; String sql = "INSERT INTO pet(petName,petBread,petSex,birthday,description) VALUES(?,?,?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, p.getPetName()); pstmt.setString(2, p.getPetBread()); pstmt.setString(3, p.getPetSex()); pstmt.setString(4, p.getBirthday()); pstmt.setString(5, p.getDescription()); i = pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; } /*改*/ public static int update() {Scanner s=new Scanner(System.in);System.err.println("请输入您要修改的宠物的名字:");pet p=new pet();String n1 = s.next(); Connection conn = getConn(); int i = 0; String sql = "update pet set petName='" + p.getPetName() + "' where Name='" + p.getPetName() + "'"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; } /*查*/ public static Integer getAll() { Connection conn = getConn(); String sql = "select * from pet"; PreparedStatement pstmt; try { pstmt = (PreparedStatement)conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); System.out.println("============================"); while (rs.next()) { for (int i = 1; i <= col; i++) { System.out.print(rs.getString(i) + "t"); if ((i == 2) && (rs.getString(i).length() < 8)) { System.out.print("t"); } } System.out.println(""); } System.out.println("============================"); } catch (SQLException e) { e.printStackTrace(); } return null; } /*删*/ public static int delete(String petName) { Connection conn = getConn(); int i = 0; String sql = "delete from pet where petName='" + petName + "'"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104}
entity类
package entity;
import java.sql.Date;
public class pet {
private int petId;
private String petName;
private String petBread;
private String petSex;
private String birthday;
private String description;
public pet(int petId, String petName, String petBread, String petSex,
String birthday, String description) {
this.petId = petId;this.petName = petName;this.petBread = petBread;this.petSex = petSex;this.birthday = birthday;this.description = description; } public pet() { } public int getPetId() {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 getPetBread() {return petBread; } public void setPetBread(String petBread) {this.petBread = petBread; } public String getPetSex() {return petSex; } public void setPetSex(String petSex) {this.petSex = petSex; } public String getBirthday() {return birthday; } public void setBirthday(String birthday) {this.birthday = birthday; } public String getDescription() {return description; } public void setDescription(String description) {this.description = description; }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950}
相关知识
IDEA+Java控制台实现宠物管理系统
宠物商店项目,增删改查
宠物管理系统(宠物管理系统的设计与实现)
基于Springboot+vue的宠物之家领养救助管理系统的设计与实现
springboot宠物领养管理系统论文
基于Spring Boot的宠物医院管理系统设计与实现
网上宠物商店系统开题报告
基于SSM的宠物店销售系统设计与实现
基于SpringBoot的宠物管理系统的设计与实现
Android宠物领养救助系统app毕业设计
网址: 使用JDBC实现简单的宠物系统(增删改查) https://m.mcbbbk.com/newsview330802.html
上一篇: 宠物岛:主人带爱犬参加比赛,虽然 |
下一篇: 猫咪喜欢放在室外吗为什么 |