1. 需要的软件和资源
①eclipse (已经配置好Javaweb开发环境)
②Mysql 以及 Navicat for MySQL
③MySQL驱动
2. 在eclipse中创建工程
创建一个工程 名为bms (Books Management System)
在src中创建几个包 如下
① com.software.util //用来作为框架
② com.software.test //用来测试
③ com.software.dao //存放方法
④ com.software.daoimpl //实现方法的接口
⑤ com.software.po // 存放实体类
3. 每个包的具体实现
①com.software.po
在该包中创建一个类book 其中包含book的属性以及构造方法
package com.software.po;
public class Book {
private int bookID;
private String bookISBN;
private String bookName;
private float bookPrice;
private String bookAuthor;
private String bookPublisher;
private int bookCount;
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
public String getBookISBN() {
return bookISBN;
}
public void setBookISBN(String bookISBN) {
this.bookISBN = bookISBN;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public float getBookPrice() {
return bookPrice;
}
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookPublisher() {
return bookPublisher;
}
public void setBookPublisher(String bookPublisher) {
this.bookPublisher = bookPublisher;
}
public int getBookCount() {
return bookCount;
}
public void setBookCount(int bookCount) {
this.bookCount = bookCount;
}
public Book(int bookID, String bookISBN, String bookName, float bookPrice, String bookAuthor, String bookPublisher,
int bookCount) {
super();
this.bookID = bookID;
this.bookISBN = bookISBN;
this.bookName = bookName;
this.bookPrice = bookPrice;
this.bookAuthor = bookAuthor;
this.bookPublisher = bookPublisher;
this.bookCount = bookCount;
}
public Book(String bookISBN, String bookName, float bookPrice, String bookAuthor, String bookPublisher,
int bookCount) {
super();
this.bookISBN = bookISBN;
this.bookName = bookName;
this.bookPrice = bookPrice;
this.bookAuthor = bookAuthor;
this.bookPublisher = bookPublisher;
this.bookCount = bookCount;
}
public Book() {
super();
}
}
② com.software.util
为了明确我们图书管理系统具体所需要完成的工作 我们可以先用具体的类来实现 以此为参照
在 com.software.util中创建一个DBUtilTmp类
package com.software.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtilTmp {
public static void main(String[] args) {
//准备数据库的资源
//1 用户名
String user = "root";
//密码
String password = "256256";
//url 数据库连接的地址
//如果安装在服务器或者虚拟机上 localhost 改为ip地址
String url ="jdbc:mysql://localhost:3306/bms?characterEncoding=utf-8";
//驱动名
//8.几的版本 用String driver="com.mysql.cj.jdbc.Driver";
String driver="com.mysql.jdbc.Driver";
try {
//1 加载驱动
Class.forName(driver);
//2 获取连接
Connection conn=DriverManager.getConnection(url, user, password);
//3 SQL 指令
String sql = "select * from book";
//4 创建 preparedStatement 对象 通过该对象将SQL 指令发送给MYSQL
//PreparedStatement 实例包含已编译的 SQL 语句.其执行速度要快于 Statement 对象
PreparedStatement pst = conn.prepareStatement(sql);
//5 执行 SQL 指令 保存结果
ResultSet rs= pst.executeQuery();
//6 循环输入结果集
while(rs.next())
{
System.out.println("图书ID:"+ rs.getInt("book_id"));
//System.out.println("图书ID:"+ rs.getInt(1));
System.out.println("图书ISBN:"+ rs.getString("book_isbn"));
System.out.println("图书名称:"+ rs.getString("book_name"));
System.out.println("图书价格:"+ rs.getFloat("book_price"));
System.out.println("图书作者:"+ rs.getString("book_author"));
System.out.println("图书出版社:"+ rs.getString("book_publisher"));
System.out.println("图书数量:"+ rs.getInt("book_count"));
}
//7 释放资源
if (rs !=null)
{
rs.close();
}
if (pst !=null)
{
pst.close();
}
if (conn !=null)
{
conn.close();
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
为实现与数据库的连接 需要把MySQL驱动放到下图位置
打开Navicat 新建一个连接 VMware
在该连接中新建一个名为bms 的数据库
在该数据库中 创建名为book的表 如下图
以上就是我们用一个类实现查询表所有信息的过程
结果如下
下面我们可以根据这个类分析我们到底需要完成那些工作
1 . 准备数据库的资源 :登录用的用户名 密码 以及数据库连接的地址
2 . 加载驱动并与数据库获得连接
3 .创建SQL 指令并执行 输出结果
4 .最后释放资源
为实现该图书管理系统能够对不同的书籍进行管理 所以应该把具有确定性的操作单独放到dao包中 在DBUtil中只保留具有一般性的操作
package com.software.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static String user="root";
private static String password="256256";
private static String url="jdbc:mysql://localhost:3306/bms?characterEncoding=utf-8";
private static String driver="com.mysql.jdbc.Driver";
private static Connection conn = null;
private static PreparedStatement pst = null;
private static ResultSet rs= null;
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url, user, password);
}
public static void release() throws SQLException
{
if (rs !=null)
{
rs.close();
}
if (pst !=null)
{
pst.close();
}
if (conn !=null)
{
conn.close();
}
}
}
③com.software.dao
dao包用于存放图书管理系统的操作
增加图书信息
删除图书信息
修改图书信息
查找图书信息
在dao包中新建名为IBookDao一个接口 如下
package com.software.dao;
import java.util.List;
import com.software.po.Book;
public interface IBookDao {
//添加图书信息
/*
* 创建方法的考虑
* 1.访问修饰符: public
* 2.返回值类型:暂定void
* 3.方法名 :addBook
* 4.参数 :Book book
*/
public void addBook(Book book);
//删除图书信息
/*
* 创建方法的考虑
* 1.访问修饰符: public
* 2.返回值类型: 暂定 void
* 3.方法名: delBook
* 4.参数:String bookINSB
*/
public void delBook(String bookINSB);
//更改图书信息
/*
* 创建方法的考虑
* 1.访问修饰符: public
* 2.返回值类型: 暂定 void
* 3.方法名: updataBook
* 4.参数: Book book
*/
public void updataBook(Book book);
//查询图书信息
/*
* 创建方法的考虑
* 1.访问修饰符: public
* 2.返回值类型: List
* 3.方法名: findAllBooks
* 4.参数: 无
*/
public List<Book> findAllBooks();
}
④com.software.daoimpl
在daoimpl中新建一个名为BookDaoimpl的类
该类是对dao包中的方法的具体实现
这里以查找为例 如下
package com.soft.ware.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.software.dao.IBookDao;
import com.software.po.Book;
import com.software.util.DBUtil;
public class BookDaoimpl implements IBookDao {
@Override
public void addBook(Book book) {
// TODO Auto-generated method stub
}
@Override
public void delBook(String bookINSB) {
// TODO Auto-generated method stub
}
@Override
public void updataBook(Book book) {
// TODO Auto-generated method stub
}
@Override
public List<Book> findAllBooks() {
List<Book> books = new ArrayList<Book>();
try {
Connection conn = DBUtil.getConnection();
String sql = "select * from book";
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs= pst.executeQuery();
while(rs.next())
{
Book book= new Book();
book.setBookID(rs.getInt("book_id"));
book.setBookISBN(rs.getString("book_isbn"));
book.setBookName(rs.getString("book_name"));
book.setBookPrice(rs.getFloat("book_price"));
book.setBookAuthor(rs.getString("book_author"));
book.setBookPublisher(rs.getString("book_publisher"));
book.setBookCount(rs.getInt("book_count"));
books.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
DBUtil.release();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return books;
}
}
⑤com.software.test
最后在test包中新建一个名为FindAllBooksTest类进行测试
package com.software.test;
import java.util.List;
import com.soft.ware.dao.impl.BookDaoimpl;
import com.software.dao.IBookDao;
import com.software.po.Book;
public class FindAllBooksTest {
public static void main(String[] args) {
IBookDao bookDao = new BookDaoimpl();
List<Book> books = bookDao.findAllBooks();
for(Book book:books)
{
System.out.println("图书ID:"+ book.getBookID());
System.out.println("图书ISBN:"+ book.getBookISBN());
System.out.println("图书名称:"+ book.getBookName());
System.out.println("图书价格:"+ book.getBookPrice());
System.out.println("图书作者:"+ book.getBookAuthor());
System.out.println("图书出版社:"+book.getBookPublisher());
System.out.println("图书数量:"+ book.getBookCount());
}
}
}