使用JDBC實現簡單的寵物系統(增刪改查)
阿新 • • 發佈:2018-12-24
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; }
}