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

来源:互联网 发布:ug编程实例教程零件 编辑:程序博客网 时间:2024/06/08 05:27

先去下载驱动包;http://download.csdn.net/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=scottpassword=scottdriver=oracle.jdbx.driver.OracleDriverurl=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
0 0