package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBDao { static String DBURL="jdbc:mysql://localhost:3306/bookmanage"; static String DBUser="root"; static String DBPswd="123456"; static String DBDriver="com.mysql.jdbc.Driver"; static Connection connection=null; public static Connection getConnection(){ try { Class.forName(DBDriver); connection=DriverManager.getConnection(DBURL, DBUser,DBPswd); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return connection; } public static void closeConnection(Connection connection){ if(connection!=null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } connection=null; } } }
2.
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import bean.Book; public class BookDao { static Connection connection=null; static String erro=""; public static ResultSet getAllBook(){ ResultSet rs=null; String SQL="select * from bookInfo"; try { connection = DBDao.getConnection(); PreparedStatement ppsm=connection.prepareStatement(SQL); rs = ppsm.executeQuery(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return rs; } public static ResultSet getIdBook(int id){ ResultSet rs =null; String SQL="select * from bookInfo where bookId ="+id; try { connection = DBDao.getConnection(); PreparedStatement ppsm=connection.prepareStatement(SQL); rs = ppsm.executeQuery(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return rs; } public static boolean uptateBook(int oldBookId,int newBookId,String bookName,String bookAuthor,String bookPress){ boolean flag=false; String SQL="update bookInfo set bookId=?,bookName=?,bookAuthor=?,bookPress=? where bookId="+oldBookId; try { connection = DBDao.getConnection(); PreparedStatement ppsm=connection.prepareStatement(SQL); ppsm.setInt(1, newBookId); ppsm.setString(2, bookName); ppsm.setString(3, bookAuthor); ppsm.setString(4, bookPress); int rs = ppsm.executeUpdate(); if(rs==1) flag=true; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return flag; } public static boolean deleteBook(int id){ boolean flag=false; String SQL="delete from bookInfo where bookId ="+id; try { connection = DBDao.getConnection(); PreparedStatement ppsm=connection.prepareStatement(SQL); int rs = ppsm.executeUpdate(); if(rs==1) flag=true; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return flag; } public static ResultSet getBook(String s){ ResultSet rs =null; String SQL="select * from bookInfo where bookId like '%"+s+"%' or bookName like '%"+s+"%' or bookAuthor like '%"+s+"%' or bookPress like '%"+s+"%'"; try { connection = DBDao.getConnection(); PreparedStatement ppsm=connection.prepareStatement(SQL); rs = ppsm.executeQuery(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return rs; } public static Connection getConnection() { return connection; } public static void setConnection(Connection connection) { BookDao.connection = connection; } public static boolean insertBook(Book book){ boolean flag=false; String SQL="insert into bookInfo values(?,?,?,?)"; try{ connection = DBDao.getConnection(); PreparedStatement ppsm=connection.prepareStatement(SQL); ppsm.setInt(1, book.getBookId()); ppsm.setString(2, book.getBookName()); ppsm.setString(3, book.getBookAuthor()); ppsm.setString(4, book.getBookPress()); int rs = ppsm.executeUpdate(); if(rs==1){ flag=true; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return flag; } }