⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db.java

📁 这是一个连接数据库的类
💻 JAVA
字号:
package connect;import java.sql.*;import java.util.logging.Level;import java.util.logging.Logger;public class DB {    private PreparedStatement pstmt;    public Connection getConn() throws IllegalAccessException, InstantiationException {        Connection conn = null;        try {            try {                Class.forName(DBConst.CLASSNAME).newInstance();            } catch (ClassNotFoundException ex) {                Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);            }            conn = DriverManager.getConnection(DBConst.URL, DBConst.NAME, DBConst.PWD);        } catch (SQLException e) {            e.printStackTrace();        }        return conn;    }    public void add(String addSQL) {        try {            pstmt = getConn().prepareStatement(addSQL);            System.out.println(pstmt);        } catch (Exception ex) {            System.out.println(ex);        }    }    public Statement getStatement(Connection conn) {        Statement stmt = null;        try {            if (conn != null) {                stmt = conn.createStatement();            }        } catch (SQLException e) {            e.printStackTrace();        }        return stmt;    }    public PreparedStatement getPstmt() {        return pstmt;    }    public void setPstmt(PreparedStatement pstmt) {        this.pstmt = pstmt;    }    public 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 void closeConn(Connection conn) {        try {            if (conn != null) {                conn.close();                conn = null;            }        } catch (SQLException e) {            e.printStackTrace();        }    }    public void closeStmt(Statement stmt) {        try {            if (stmt != null) {                stmt.close();                stmt = null;            }        } catch (SQLException e) {            e.printStackTrace();        }    }public void closePreStmt(PreparedStatement pstmt) {        try {            if (pstmt != null) {                pstmt.close();                pstmt = null;            }        } catch (SQLException e) {            e.printStackTrace();        }    }    public void closeRs(ResultSet rs) {        try {            if (rs != null) {                rs.close();                rs = null;            }        } catch (SQLException e) {            e.printStackTrace();        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -