【发布时间】:2020-10-16 02:40:28
【问题描述】:
我正在使用 甲骨文
SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jun 25 20:17:54 2020
Java
java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
jar 文件
ojdbc14.jar
Copyright (c) 1982, 2005, Oracle. All rights reserved.
我在我的 oracle dbms 中创建了一个表
SQL> create Table employees ( name varchar(20));
Table created.
我插入了几条记录
SQL> insert into employees (name) values ('Navjot');
1 row created.
SQL> insert into employees (name) values ('jagjot');
SQL> commit;
Commit complete.
我试图从我的 java 类中获取这些记录但它说不存在这样的表。
BaseDAO.java
import java.sql.*;
class BaseDAO
{
public Connection getConnection()
{
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521/xe";
String user="system";
String password="system";
Connection conn=null;
try
{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
System.out.println("Connected to oracle database successfully");
}
catch(Exception e)
{
System.out.println(e);
}
return conn;
}//End of getConnection method
}//End of BaseDAO class
RetriveAllEmployees.java
import java.sql.*;
public class RetriveAllEmployees extends BaseDAO
{
public void retrive()
{
try
{
Connection conn=getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from EMPLOYEES");
while(rs.next())
{
String employeeName=rs.getString("name");
System.out.println(employeeName);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
RetriveAllEmployees retriveAllEmployees=new RetriveAllEmployees();
retriveAllEmployees.retrive();
}
}
当我尝试执行此代码时,它说表不存在
E:\aman\java\jdbc\jdbc 1 doc examples\003 connection oracle sql>java -classpath .;C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar; RetriveAllEmployees
Connected to oracle database successfully
java.sql.SQLException: ORA-00942: table or view does not exist
当我通过命令行登录oracle时
Enter user-name: system
Enter password:system
Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
SQL> conn sys as sysdba;
Enter password:none
Connected.
【问题讨论】:
-
无法建立与数据库的连接。检查您的连接属性(首先是 JDBC url)
-
我不明白你的评论。 '“不可能建立与数据库的连接。”@AngeloImmediate。正如你所看到的,当我编译我的 cod 时,它说已成功连接到数据库。 BaseDAO
-
在以前版本的问题中(在编辑之前),错误消息是:网络适配器无法建立连接这就是我写该评论的原因