您的位置:首页 > 数据库 > Oracle

java连接Oracle详解

2017-09-18 15:23 176 查看
package demo.utils;

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:@172.0.0.1:1521:orcl";
//172.0.0.1是你的数据库所在ip地址,不知道的自己百度
//name和password是数据库的用户名和密码
private static String name = "scott";
private static String password = "scott";

//注册数据库的驱动
static{
try {
Class.forName(driver);
//DriverManager.registerDriver(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}

//获取数据库的连接
public static Connection getConnection(){

try {
return DriverManager.getConnection(url, name, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

//释放数据库的资源
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;
}
}
}
}


具体代码和jar包下载地址http://pan.baidu.com/s/1b3QqVO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle java class