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

📄 jdbcclass.java~1~

📁 一个关于EJB的练习程序
💻 JAVA~1~
字号:
package emp2;

import emp.JdbcClass;

import java.sql.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class JdbcClass {
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;

    public JdbcClass() {

        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("数据库驱动未找到!");
        }
    }


    public Connection getConn() {
        try {
            if (conn == null || conn.isClosed()) {
                conn = DriverManager.getConnection(
                        "jdbc:microsoft:sqlserver://localhost:1433;databasename=employee",
                        "sa", "");
            }

            return conn;
        } catch (SQLException ex) {
            System.out.println("数据库连接出错!");
            ex.printStackTrace();
        }

        return null;
    }

    public Statement getStmt() {
        try {
            if (conn == null || conn.isClosed()) {
                this.getConn();
            }
            stmt = conn.createStatement();
            return stmt;
        } catch (SQLException ex) {
            System.out.println("Statement出错!");
            ex.printStackTrace();
        }
        return null;
    }


    public ResultSet executeQuery(String sql) {
        System.out.println("executeQuery:" + sql);
        try {
            if (stmt == null) {
                this.getStmt();
            }

            rs = stmt.executeQuery(sql);
            return rs;
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("executeQuery出错!");
        }
        return null;
    }


    public int executeUpdate(String sql) {
        System.out.println("executeUpdate:" + sql);

        try {
            if (stmt == null) {
                this.getStmt();
            }
            return stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("executeUpdate出错!");
        }
        return 0;
    }

    public void closeAll() {

        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                if (!conn.isClosed()) {
                    conn.close();
                }
                conn = null;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("closeAll出错!");
        }

    }

}

⌨️ 快捷键说明

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