jdbc连接oracle 10g,使用jdbc连接oracle10g数据库

首先先要oracle的下载驱动包

然后将classes12.jar导入到eclipse中的项目中

0818b9ca8b590ca3270a3433284dd417.png

然后创建如下工具类:

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:orcl";//如果oracle装在本机,也可以输入@ip地址

private static String user = "scott";//输入自己设置的账号

private static String password = "root";//输入自己设置的密码

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;//垃圾回收: 是否可以通过代码干预垃圾回收??

}

}

}

}

然后再创建一个测试类来调用它

public class test{

public static void main(String args[]){

JDBCUtils.getConnection();

}

}

如果没报错 证明连接没有问题了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值