zoukankan      html  css  js  c++  java
  • 一、JDBC操作

    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;
        }
    }
  • 相关阅读:
    Centos下 安装和测试kafka
    Java枚举
    Java 数组
    Java变量
    Java标识符
    Java修饰符
    java 基本语法
    Java 基础语法
    Java开发工具
    JAVA 发展历史
  • 原文地址:https://www.cnblogs.com/myz666/p/8446170.html
Copyright © 2011-2022 走看看