声明的软件包与预期的软件包不匹配

时间:2019-06-04 19:14:19

标签: java jdbc oracledb

import java.sql.*; //The declared package "" does not match the expected package "jdbc" 

class Oraclecon {

    public static void main(String args[]) {

        try {

            //step1 load the driver class  

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

            //step2 create  the connection object  

            Connection con = DriverManager.getConnection(

                "jdbc:oracle:thin:@localhost:1521:xe", "system", "system");

            //step3 create the statement object  

            Statement stmt = con.createStatement();

            //step4 execute query  

            ResultSet rs = stmt.executeQuery("select * from JNTURESULTS");

            while (rs.next())

                System.out.println(rs.getInt(1) + "  " + rs.getString(2) + "  " + rs.getString(3));

            //step5 close the connection object  

            con.close();


        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

问题是您的代码未声明程序包(它在默认的无名程序包中)。

您的文件结构可能类似于

src
  \-jdbc
      \-Oraclecon.java

当以src作为根进行编译时,Java希望您的Oraclecon.java将其程序包声明为jdbc。要解决此问题,请在package jdbc;的顶部添加Oraclecon.java

请注意,像jdbc这样的软件包名称与Java naming conventions for packages不匹配。

相关问题