jdbc连接Oracle数据库(easy to integration)

本文提供了一个使用Java进行Oracle数据库操作的示例,包括连接数据库、执行查询等步骤,并介绍了如何利用工具类简化数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先去下载驱动包;http://download.youkuaiyun.com/detail/u011193134/6207933
并添加了lib中。

easy demo:一般生产不这么做,只是看看而已

public class oracle01 {
    public static void main(String[] args) {
        //与特定数据库的连接
        Connection ct = null;
        //表示预编译的SQL语句对象
        PreparedStatement ps = null;
        //表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //2.得到连接
            ct = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","scott");
            //3.创建PrepareStatement接口引用对象
            ps = ct.prepareStatement("select * from emp");
            System.out.println(ps);
            //4.完成查询任务
            rs = ps.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString("ename")+"-"+rs.getString("sal")+"-"+rs.getString("deptno"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //关闭资源
            if(rs!=null){
                try {
                    rs.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                rs = null;
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                ps = null;
            }
            if(ct!=null){
                try {
                    ct.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ct =null;
            }
        }

    }

}

工具类开发SQLHelper

public class SQLHelper {
    // 定义三个变量
    private static Connection ct = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    // 连接数据库的用户名,密码,url,驱动
    // 在实际开发中我们会把变量写到外部配置文件中
    // 当程序启动时,我们读入这些配置信息,java.util.Properties
    private static String username;
    private static String password;
    private static String driver;
    private static String url;

    // 使用静态代码块加载驱动(驱动只需要加一次)
    static {
        // 使用Properties类,来读取配置文件
        Properties pp = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("dbinfo.properties");
            // 让pp与dbinfo.properties文件关联起来
            pp.load(fis);
            // 获取dbinfo.properties文件内信息
            username = pp.getProperty("username");
            password = pp.getProperty("password");
            driver = pp.getProperty("driver");
            url = pp.getProperty("url");
            // 获得驱动
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            fis = null;
        }
    }

    //统一的curd操作
    public static void executeUpdate(String sql,String[]parameters){

        try {
            ct = DriverManager.getConnection(url,username,password);
            ps = ct.prepareStatement(sql);
            if (parameters!=null) {
                for (int i = 0; i < parameters.length; i++) {
                    ps.setString(i+1,parameters[i]);
                }
            }
            //执行
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }finally{
            close(rs,ps,ct);
        }
    }

    //写一个方法进行查询操作
    //sql表示要执行的sql语句
    //sql select * from emp where ename=?
    public static ResultSet executeQuery(String sql,String[]parameters){
        try {
            //根据实际情况我们对sql语句?赋值
            //得到连接
            ct = DriverManager.getConnection(url,username,password);
            //创建ps对象,得到sql语句对象
            ps = ct.prepareStatement(sql);
            //如果parameters不为null,才赋值
            if(parameters!=null){
                for (int i = 0; i < parameters.length; i++) {
                    ps.setString(i+1, parameters[i]);
                }
            }
            rs = ps.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
            //抛出运行异常
            throw new RuntimeException(e.getMessage());
        }finally{
            //close(rs, ps, ct);
        }
        return rs;
    }

    //把关闭资源写成函数
    public static void close(ResultSet rs,Statement ps,Connection ct){
        if (rs!=null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs=null;
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            ps=null;
        }

        if(ct!=null){
            try {
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            ct = null;
        }

    }
    public static Connection getCt() {
        return ct;
    }

    public static PreparedStatement getPs() {
        return ps;
    }

}

测试Demo

public class OracleDemo {
    public static void main(String[] args) {
        sel();
        //executeUpdate();
    }
    public static void executeUpdate() {
        String sql = "delete from emp where empno=9999";
        SQLHelper.executeUpdate(sql, null);
    }

    public static void sel() {
        String sql = "select * from emp";
        ResultSet rs = SQLHelper.executeQuery(sql, null);
        try {
            while (rs.next()) {
                System.out.println(rs.getString("ename"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            SQLHelper.close(rs, SQLHelper.getPs(), SQLHelper.getCt());
        }
    }

}

dbinfo.properties

username=scott
password=scott
driver=oracle.jdbx.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值