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;
}
}
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;
}
}