欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

J2EE常用工具类—Jdbc操作

程序员文章站 2022-05-24 17:42:38
...
package cn.org.jshuwei.j2ee.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
* 
* Jdbc操作的工具类
* 
* @author huwei(jshuwei.org.cn)
* @since 1.0
* 
*/
public class JdbcUtil {
    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Class.forName("com.mysql.jdbc.Driver");
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到Connection
     * 
     * @since 1.0
     * @param dbType
     *            数据库类型(oracle/mysql)
     * @return 返回Connection
     */
    public static Connection getConnection(String dbType) {
        String url = "";
        String user = "";
        String password = "";
        if (dbType.equals("oracle")) {
            url = "jdbc:oracle:thin:@localhost:6666:XE";
            user = "jshuwei";
            password = "123456";
        }
        if (dbType.equals("mysql")) {
            url = "jdbc:mysql://localhost:3306/test";
            user = "jshuwei";
            password = "123456";
        }
        if (dbType.equals("sqlServer")) {
            url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_test";
            user = "jshuwei";
            password = "123456";
        }
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 打印记录集对象
     * 
     * @since 1.0
     * @param rs
     *            需要打印的记录集对象
     */
    public static void printRs(ResultSet rs) {
        if (rs == null) {
            System.out.println("ResultSet is null!");
            return;
        }
        try {
            ResultSetMetaData md = rs.getMetaData();
            int cols = md.getColumnCount();
            for (int i = 1; i <= cols; i++) {
                // 列名,类型编号,类型名称
                System.out
                        .println(md.getColumnName(i) + "-->"
                                + md.getColumnType(i) + "-->"
                                + md.getColumnTypeName(i));
            }
            System.out.println("=========================================");
            while (rs.next()) {
                for (int i = 1; i <= cols; i++) {
                    System.out.println(md.getColumnName(i) + "="
                            + rs.getString(i) + "\t");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放资源
     * 
     * @since 1.0
     * @param rs
     *            需要释放的记录集对象
     * @param stmt
     *            需要释放的Statement对象
     * @param con
     *            需要释放的连接对象
     */
    public static void release(ResultSet rs, Statement stmt, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (Exception e) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (Exception e) {
            }
        if (con != null)
            try {
                con.close();
            } catch (Exception e) {
            }
    }

    /**
     * 释放资源
     * 
     * @since 1.0
     * @param o
     *            需要释放的对象
     */
    public static void release(Object o) {
        try {
            if (o instanceof ResultSet) {
                ((ResultSet) o).close();
            } else if (o instanceof Statement) {
                ((Statement) o).close();
            } else if (o instanceof Connection) {
                ((Connection) o).close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}