java-oracle数据库连接

来源:互联网 发布:淘宝吸黑头仪器有用吗 编辑:程序博客网 时间:2024/06/16 03:12

package demo.util;

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

public class JDBCUtils {
private static String driver = “oracle.jdbc.OracleDriver”;
private static String url = “jdbc:oracle:thin:@localhost:1521:orcl”;
private static String user = “scott”;
private static String password = “tiger”;

static{    try {        Class.forName(driver);    } catch (ClassNotFoundException e) {        throw new ExceptionInInitializerError(e);    }}public static Connection getConnection(){    try {        return DriverManager.getConnection(url, user, password);    } catch (SQLException e) {        e.printStackTrace();    }    return null;}/* * 执行java程序: * java -Xms100M -Xmx200M HelloWorld */public static void release(Connection conn, Statement st, ResultSet rs){    if(rs!=null){        try {            rs.close();        } catch (SQLException e) {            e.printStackTrace();        }finally{            rs = null;//垃圾回收: 是否可以通过代码干预垃圾回收??        }    }    if(st!=null){        try {            st.close();        } catch (SQLException e) {            e.printStackTrace();        }finally{            st = null;//垃圾回收: 是否可以通过代码干预垃圾回收??        }    }    if(conn!=null){        try {            conn.close();        } catch (SQLException e) {            e.printStackTrace();        }finally{            conn = null;//垃圾回收: 是否可以通过代码干预垃圾回收??        }    }}

}

0 0
原创粉丝点击