如何将intelliJ连接到oracle

时间:2017-01-27 17:30:29

标签: java oracle

我已经安装了oracle并在其中创建了表。现在我想将intellij想法连接到oracle。我已将classes12.jar添加到库中,但我无法将代码连接到oracle。我该怎么办?我的代码是:

package example;

import java.sql.*;
public class first {
    private Connection connection;
    private Statement statement;
    public first()throws Exception
    {
        Class.forName("oracle.jdbc.driver.oracleDriver");
        connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","maryam","myjava123");
        statement = connection.createStatement();
    }

    public void insert() throws Exception
    {
        statement.executeUpdate("INSERT INTO T1 (ID,NAME) VALUES (1,'ALI')");

    }
    public void close() throws Exception
    {
        statement.close();
        connection.close();
    }

    public static void main(String[] args)throws Exception {
        first mari=new first();
        mari.insert();
    }
}

,错误是:

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.oracleDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at example.first.<init>(first.java:9)
    at example.first.main(first.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

2 个答案:

答案 0 :(得分:2)

更改

Class.forName("oracle.jdbc.driver.oracleDriver");
//--------------------------------^-- here is the issue

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

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

此外,检查驱动程序jar文件是否确实存在于类路径中。

答案 1 :(得分:0)

在classpath中使用ojdbc6.jar或ojdbc7.jar。并且还将oracleDriver更改为OracleDriver。