zoukankan      html  css  js  c++  java
  • 使用Jdbc连接Oracle数据库

    使用Jdbc连接Oracle数据库

    一、编写代码

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JdbcTest02 {
    
        public static void main(String[] args) {
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                String driver = "oracle.jdbc.driver.OracleDriver";
                String url = "jdbc:oracle:thin:@localhost:1521:xe";
                String user = "xuanyuan";
                String password = "xuanyuan";
    
                Class.forName(driver);
                con = DriverManager.getConnection(url, user, password);
    
                st = con.createStatement();
                rs = st.executeQuery("select sysdate from dual");
    
                while (rs.next())
                    System.out.println(rs.getString(1));
    
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    
                if (st != null) {
                    try {
                        st.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    二、编译代码

    $ javac JdbcTest02.java

    三、运行程序 JdbcTest02

    $ java -cp /opt/javalib/orajdbc/ojdbc14.jar:. JdbcTest02
    2009-10-31 14:34:50.0

    说明:Oracle和MySQL的jdbc 驱动都在 /opt/javalib/目录下

    轩辕高端C++培训:专注 LinuxC/C++ACEBoost高性能服务器端应用 开发技术培训

  • 相关阅读:
    lightoj-1050
    lightoj-1305
    lightoj-1354
    lightoj-1433
    lightoj-1227
    lightoj-1189
    lightoj-1182
    lightoj-1011
    lightoj-1009
    lightoj-1023
  • 原文地址:https://www.cnblogs.com/xyath/p/3309996.html
Copyright © 2011-2022 走看看