dbcon.java

来自「JBuilder 2006 +SQL Server2000 +JavaBean+」· Java 代码 · 共 73 行

JAVA
73
字号
package com.pet.util;

import java.sql.*;

public class DBCon {
    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private static DBCon dbBasic;

    //工厂方法
    public static DBCon getInstance() {
        if (dbBasic == null) {
            dbBasic = new DBCon();
        }
        return dbBasic;
    }

    //连接数据库
    private Connection getConnection() throws SQLException,
            ClassNotFoundException {
        //纯JDBC连接
//        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//        this.conn = DriverManager.getConnection(
//                "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=pet",
//                "sa", "");
       //ODBC连接

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          this.conn = DriverManager.getConnection("jdbc:odbc:pets");


        return conn;
    }

    //查询方法
    public ResultSet querySQL(String strSql) throws SQLException,
            ClassNotFoundException {
        if (conn == null) {
            this.getConnection();
        }
        this.stmt = conn.createStatement();
        //rs = stmt.executeQuery(strSql);
        return stmt.executeQuery(strSql);
    }


    public int updateResult(String strSql) throws SQLException,
            ClassNotFoundException {
        if (conn == null) {
            this.getConnection();
        }
        this.stmt = conn.createStatement();
        return stmt.executeUpdate(strSql);

    }
    //关闭数据库
    public void closeConn() throws SQLException {
        if (rs != null) {
            rs.close();
            rs = null;
        }
        if (stmt != null) {
            stmt.close();
            stmt = null;
        }
        if (conn != null) {
            conn.close();
            conn = null;
        }
    }
}

⌨️ 快捷键说明

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