conn.java

来自「AOI(面向属性的归纳)是数据分析中有效的数据约简手段」· Java 代码 · 共 89 行

JAVA
89
字号
package data;

import java.sql.*;

public class Conn {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String drivername = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:firm";
    public static Connection getCon() throws Exception {
        try {
            Class.forName(drivername);
            con = DriverManager.getConnection(url,"sa","");
            return con;
        } catch (SQLException e) {
            System.err.println("getCon" + e.getMessage());
            throw e;
        }
    }

    public static Statement getStmt() {
        try {
            con = getCon();
            Statement stmt = con.createStatement(ResultSet.
                                                 TYPE_SCROLL_SENSITIVE,
                                                 ResultSet.CONCUR_READ_ONLY);
            return stmt;
        } catch (Exception e) {
            System.err.println("getStmt" + e.getMessage());
            return null;
        }
    }

    public static ResultSet executeQuery(String sql) {
        try {
            stmt = getStmt();
            rs = stmt.executeQuery(sql);
        } catch (Exception e) {
            System.err.println("executeQuery" + e.getMessage());
        }
		 return rs;
    }

    public static int executeUpdate(String sql) {
        int count = 0;
        stmt = getStmt();
        try {
            count = stmt.executeUpdate(sql);
        } catch (Exception e) {
            count = -2;
            System.err.println("executeUpdata" + e.getMessage());
            return count;
        } finally {
        }
		            return count;

    }

    public static void close() {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
        } catch (Exception e) {
            System.out.println("closers" + e.getMessage());
        }
        try {
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
        } catch (Exception e) {
            System.out.println("closestmt" + e.getMessage());
        }
        try {
            if (con != null) {
                con.close();
                con = null;
            }
        } catch (Exception e) {
            System.out.print("closecon" + e.getMessage());
        }
    }

}

⌨️ 快捷键说明

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