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 6.5环境实现corosync+pacemaker实现DRBD高可用
    通达OA2008优化前端web为lnmp环境及后续优化
    CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解
    利用mycat实现基于mysql5.5主从复制的读写分离
    登录服务器windows2008出现:远程桌面服务当前正忙,因此无法完成您尝试执行的任务。请在几分钟后重试。其他用户应该仍然能够登录
    CentOS 6.5使用Corosync + pacemaker实现httpd服务的高可用
    ansible的安装部署及简单应用
    centos6.7安装系统后看不到网卡无法配置IP的解决办法
    Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
    centos6环境创建局域网http方式的yum源
  • 原文地址:https://www.cnblogs.com/myz666/p/8446170.html
Copyright © 2011-2022 走看看