如何连接ORACLE DB

时间:2014-02-19 06:26:00

标签: java jdbc

这里我试图将安装在本地计算机上的oracle与DSN bam连接起来。 我得到java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver例外。任何人都可以帮我解决这个问题。

public class JdbcConnectionExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection con = DriverManager
                    .getConnection("jdbc:oracle:thin:@localhost:1521:bam"
                        ,"system","tiger");
                Statement stmt = con.createStatement();
                System.out.println("Created DB Connection....");
                ResultSet rs = stmt.executeQuery("select * from tt");
                while(rs.next()){
                    System.out.println(rs.getString("ename"));
                    System.out.println(rs.getInt("age"));
                }
                rs.close();
                con.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 }

日志就在这里

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.bam.sqlex.JdbcConnectionExample.main(JdbcConnectionExample.java:14)

3 个答案:

答案 0 :(得分:2)

可能你还没有添加jar的oracle驱动程序download jar from here

答案 1 :(得分:0)

java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver

毫无疑问,你缺少Oralce jar,将其压入你的项目

答案 2 :(得分:0)

Class.forName("oracle.jdbc.driver.OracleDriver");

您必须检查此类是否已添加到类路径中。