Dao的构造

来源:互联网 发布:钢琴简谱软件 编辑:程序博客网 时间:2024/05/19 17:51

//Dao类的构造方法
    public Dao() {
        try {
            if (conn == null) {
                Class.forName(dbClassName);
                conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
            } else {
                return;
            }
            System.out.println("conn成功!");//控制台打印显示连接成功
        } catch (Exception ee) {
            ee.printStackTrace();
        }


    }
    /*数据库查询方法
     * 方法参数:sql查询语句
     * 返回值:查询返回的结果集
     */


    private static ResultSet executeQuery(String sql) {
        try {
            if (conn == null) {
                new Dao();
            }
            //下面一行调用了Statement类的executeQuery(String sql)方法
            //执行给定的 SQL 语句,该语句返回单个 ResultSet 对象,绝大多数是用SELECT语句
            return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }
    /*数据库更新方法
     * 方法参数:sql更新语句
     * 返回值:一个整数,指示受影响的行数(即更新计数)
     */


    private static int executeUpdate(String sql) {


        try {
            if (conn == null) {
                new Dao();
            }
            //下面一行调用了Statement类中的executeUpdate方法
            //用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句
            return conn.createStatement().executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return -1;
        } finally {
        }
    }

0 0
原创粉丝点击