共计 4739 个字符,预计需要花费 12 分钟才能阅读完成。
创立实体类
package com.kjzz;
public class BaseDao {
private int BookID;
private String BookName;
private int TypeID;
private Double Price;
private String Publish;
private String StateID;
public BaseDao(){}
public BaseDao(int bookID, String bookName, int typeID, Double price, String publish, String stateID) {
BookID = bookID;
BookName = bookName;
TypeID = typeID;
Price = price;
Publish = publish;
StateID = stateID;
}
public int getBookID() {return BookID;}
public void setBookID(int bookID) {BookID = bookID;}
public String getBookName() {return BookName;}
public void setBookName(String bookName) {BookName = bookName;}
public int getTypeID() {return TypeID;}
public void setTypeID(int typeID) {TypeID = typeID;}
public Double getPrice() {return Price;}
public void setPrice(Double price) {Price = price;}
public String getPublish() {return Publish;}
public void setPublish(String publish) {Publish = publish;}
public String getStateID() {return StateID;}
public void setStateID(String stateID) {StateID = stateID;}
@Override
public String toString() {
return "BaseDao{" +
"BookID=" + BookID +
", BookName='" + BookName + '\'' +
", TypeID=" + TypeID +
", Price=" + Price +
", Publish='" + Publish + '\'' +
", StateID='" + StateID + '\'' +
'}';
}
}
连贯数据库
package com.kjzz;
import java.sql.*;
public class DBUtils {
public static String DRIVER = "com.mysql.jdbc.Driver";
public static String URl = "jdbc:mysql://localhost:3306/book";
public static String USER = "root";
public static String PWd = "root";
// 加载驱动
static {
try {Class.forName(DRIVER);
} catch (ClassNotFoundException e) {e.printStackTrace();
}
}
// 连贯办法
public static Connection Connection() {
Connection conn = null;
try {conn = DriverManager.getConnection(URl, USER, PWd);
} catch (SQLException e) {e.printStackTrace();
}
return conn;
}
// 测试
public static void main(String[] args) {Connection conn = Connection();
System.out.println("连贯对象是"+conn);
}
// 敞开
public static void CloseConn(Connection conn, PreparedStatement prep, ResultSet rs) {
try {if (conn != null) {conn.close();
}
if (prep != null) {prep.close();
}
if (rs != null) {rs.close();
}
} catch (SQLException e) {e.printStackTrace();
}
}
}
增删改查的使用
package com.kjzz;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ZSGC {
public static Connection conn = null;
public static PreparedStatement prep = null;
public static ResultSet rs = null;
// 查问
public List<BaseDao> ChaXun(){List<BaseDao> list = new ArrayList<>();
String SelectSql = "select * from ok";
try {conn = DBUtils.Connection();
prep = conn.prepareStatement(SelectSql);
rs = prep.executeQuery();// 查问返回的后果集
while (rs.next()){BaseDao dao = new BaseDao();
int BookID = rs.getInt("BookID");
String BookName = rs.getString("BookName");
int TypeID = rs.getInt("TypeID");
Double Price = rs.getDouble("Price");
String Publish =rs.getString("Publish");
String StateID =rs.getString("StateID");
dao = new BaseDao(BookID, BookName, TypeID, Price, Publish, StateID);
list.add(dao);
}
} catch (SQLException e) {e.printStackTrace();
}
return list;
}
// 减少
public static boolean Insert(BaseDao dao) {
int num = 0;
String InsetSql = "insert into ok values (?,?,?,?,?,?)";
try {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 (SQLException e) {e.printStackTrace();
}
return num > 0;
}
// 删除
public static boolean Delete(int BookID){
int num =0;
String DeleteSql = "Delete from ok where BookID=?";
try {conn=DBUtils.Connection();
prep=conn.prepareStatement(DeleteSql);
prep.setInt(1,BookID);
num = prep.executeUpdate();}catch (SQLException e){e.printStackTrace();
}
return num > 0;
}
// 批改
public static boolean Update(BaseDao dao){
int num =0;
String UpdateSql ="update ok set BookName = ?,TypeID=?,Price =?,Publish =?,StateID=? where BookID =?";
try {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 (SQLException e){e.printStackTrace();
}
return num >0;
}
}
测试
package com.kjzz;
import org.junit.Test;
import java.util.List;
public class Test01 {
@Test
public void test01(){
// 减少
BaseDao dao = new BaseDao(66,"节",1001,2456.0,"成都府","接触");
ZSGC.Insert(dao);
}
@Test
// 查问
public void test02(){ZSGC zsgc = new ZSGC();
List<BaseDao> list = zsgc.ChaXun();
for (BaseDao dao : list){System.out.println(dao.getBookID()+" ");
System.out.println(dao.getBookName()+" ");
System.out.println(dao.getTypeID()+" ");
System.out.println(dao.getPrice()+" ");
System.out.println(dao.getPublish()+" ");
System.out.println(dao.getStateID()+" ");
}
}
@Test
// 删除
public void test03(){boolean Delete = ZSGC.Delete(1);
}
@Test
// 批改
public void test05() {BaseDao Dao2 = new BaseDao();
Dao2.setBookID(66);
Dao2.setBookName("胡杨");
Dao2.setTypeID(0000);
Dao2.setPrice(0.0);
Dao2.setPublish("美国出版");
Dao2.setStateID("接触");
boolean update = ZSGC.Update(Dao2);
}
}
正文完