OracleJDBC

package cn.ng.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DbConnect {

    private final String url = "jdbc:oracle:thin:@服务器IP地址:1521:数据库名";

    private final String className = "oracle.jdbc.driver.OracleDriver";

    private final String userName = "用户名";

    private final String password = "用户密码";

    private Connection conn = null;

    public DbConnect() {

        try {

            Class.forName(className).newInstance();

        } catch (InstantiationException e) {

            e.printStackTrace();

        } catch (IllegalAccessException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }
    }

    // 获取数据库连接

    public Connection getCon() {

        try {

            conn = DriverManager.getConnection(url, userName, password);

        } catch (Exception e) {

            e.printStackTrace();

            System.out.print("");

        }

        return conn;

    }

    // 关闭数据库

    public void close(Connection conn, PreparedStatement ps, ResultSet rs) {

        try {

            if (rs != null)
                rs.close();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (ps != null)
                    ps.close();

            } catch (Exception e2) {

                e2.printStackTrace();

            } finally {

                try {

                    if (conn != null)
                        conn.close();

                } catch (Exception e3) {

                    e3.printStackTrace();

                }
            }
        }
    }
}

 

posted @ 2012-03-22 10:12  残星  阅读(414)  评论(0编辑  收藏  举报