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

📄 conn.java

📁 一个用jsp喝sql做的航空订票系统
💻 JAVA
字号:
package com.bwm.db;

import java.sql.*;

/**
 *class explain:Database connection获取数据库的连接
 *set up name:	crazyadept
 *set up time:	11/22/2004
 */
public class Conn {
    private static Connection con;
    private Statement stmt;
    private ResultSet rs;
    private static final String drivername =
        "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private static final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=plane;user=sa;password=;";

    /**
     *method explain:   Get Connection DataBase
     *modify time:	12/20/2004
     */

    public static synchronized Connection getCon() throws Exception {
        try {
          //加载驱动程序
            Class.forName(drivername);
            con = DriverManager.getConnection(url);
            return con;
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            throw e;
        }
    }

    /**
     *output parameter:	in order select in SQL获取Statement只能用于查询语句
     *modify time:	12/20/2004
     */
    public Statement getStmtread() {
        try {
            con = getCon();
            /**TYPE_SCROLL_INSENSITIVE允许记录指针向前向后移动,且当ResultSet对象
            *变动记录指针时,会影响记录指针的位置
            * CONCUR_READ_ONLY只能读,不能修改主要用于查询
             */
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                       ResultSet.CONCUR_READ_ONLY);
            return stmt;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     *output parameter: Data
     *input parameter:  SQL select sentence获取ResultSet
     *modify time:      11/29/2004
     */
    public ResultSet getRs(String sql) {
        try {
            stmt = getStmtread();
            rs = stmt.executeQuery(sql);
            return rs;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     *output parameter: not in order select in str SQL获取Statement用于删除、更新和添加的SQL语句
     *modify time:      11/29/2004
     */
    public Statement getStmt() {
        try {
            con = getCon();
            stmt = con.createStatement();
            return stmt;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     *method explain:   Close DataBase Connection关闭数据库的连接
     *modify time:      11/29/2004
     */
    public synchronized void close() {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        try {
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        try {
            if (con != null) {
                con.close();
                con = null;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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