一个Java基础数据库公共类的封装

 db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/s1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
userName=root
password=52Java

连接公共类

package com.store.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBHelper {
    private static String driver;
    private static String url;
    private static String userName;
    private static String password;
    static protected Connection conn;
    protected PreparedStatement pstmt;
    protected ResultSet rs;

    /**
     * 加载驱动,并建立数据库连接
     *
     * @return 返回数据库链接对象
     * @throws SQLException           抛出SQLException
     * @throws ClassNotFoundException 抛出ClassNotFoundException
     * @throws IOException            抛出IOException
     */
    static {
        // 实例化Properties对象
        Properties properties = new Properties();
        // 加载properties配置文件
        try {
            properties.load(new FileInputStream(new File("project\\src\\com\\store\\db\\db.properties")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 通过键名获取对应的值
        driver = properties.get("driver").toString();
        url = properties.get("url").toString();
        userName = properties.get("userName").toString();
        password = properties.get("password").toString();

    }

    /**
     * 连接数据库公共类
     */
    public static Connection getConnection() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, userName, password);
            System.out.println("连接成功");
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到驱动类");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 增删改公共类
     *
     * @param sql
     * @param args
     * @return
     * @throws Exception
     */
    public boolean update(String sql, Object... args) throws Exception {
        conn = getConnection();
        pstmt = conn.prepareStatement(sql);
        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                pstmt.setObject(i + 1, args[i]);
            }
        }
        System.out.println(pstmt);//输出用来调试程序,做完注释
        int i = pstmt.executeUpdate();
        return i > 0 ? true : false;
    }


    /**
     * 查询公共类
     *
     * @param sql
     * @param args
     * @return
     * @throws Exception
     */
    public ResultSet query(String sql, Object... args) throws Exception {
        conn = getConnection();
        pstmt = conn.prepareStatement(sql);
        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                pstmt.setObject(i + 1, args[i]);
            }
        }
        rs = pstmt.executeQuery();
        System.out.println(pstmt);//输出用来调试程序,做完注释
        return rs;
    }

    /**
     * 关数据库的公共方法
     */
    public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        getConnection();
    }

    /**
     * Java代码实现MySQL数据库导出
     *
     * @param userName     进入数据库所需要的用户名
     * @param password     进入数据库所需要的密码
     * @param savePathName 数据库导出文件保存路径加名字
     * @param databaseName 要导出的数据库名
     * @return 返回true表示导出成功,否则返回false。
     */
    public static boolean backup(String userName, String password, String savePathName, String databaseName) {
        try {
//            String stmt = "mysql -udog -p52Java java95 < " + "c:/sql.sql";
            String stmt = "mysqldump -u" + userName + " -p" + password + " " + databaseName + " > " + savePathName;
            String[] cmd = {"cmd", "/c", stmt};
            Process process = Runtime.getRuntime().exec(cmd);
            if (process.waitFor() == 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 操作结果:恢复数据库,前提是数据库里有该数据库名字,否则无法恢复(所以应该先创建一个数据库)
     *
     * @param username     用户名
     * @param password     用户数据库密码
     * @param databasename 数据库名字
     * @param filePathName 数据库文件路径及名字加后缀
     * @return boolean 如果恢复成功则返回true,否则返回false
     */
    public static boolean recover(String username, String password, String databasename, String filePathName) {
        try {
//            String stmt = "mysql -uroot -padmin myDB < " + "c:/sql.sql";
            String stmt = "mysql -u" + username + " -p" + password + " " + databasename + " < " + filePathName;
            String[] cmd = {"cmd", "/c", stmt};
            Process process = Runtime.getRuntime().exec(cmd);
            if (process.waitFor() == 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
经典java数据库封装类,package com.bjsxt.shopping.util; import java.sql.*; public class DB { public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=root"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static PreparedStatement prepare(Connection conn, String sql) { PreparedStatement pstmt = null; try { if(conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) { PreparedStatement pstmt = null; try { if(conn != null) { pstmt = conn.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static Statement getStatement(Connection conn) { Statement stmt = null; try { if(conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } /* public static ResultSet getResultSet(Connection conn, String sql) { Statement stmt = getStatement(conn); ResultSet rs = getResultSet(stmt, sql); close(stmt); return rs; } */ public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if(stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void executeUpdate(Statement stmt, String sql) { try { if(stmt != null) { stmt.executeUpdate(sql); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { if(rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤永红

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值