我正在尝试在我的linux终端上运行Jdbc的基本代码。我安装了sqlplus。版本是Oracle 11.2.0.2。每次运行代码我都会遇到以下错误:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at jdbcdisplay.main(jdbcdisplay.java:17)
现在我已经看到了这里提出的其他问题,他们中的大多数都是使用Eclipse连接到Jdbc驱动程序并且下载jar文件似乎已经足够了。但是如果我必须在终端中运行它我该怎么做?我会必须下载http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html或其他任何我可以做的方式。我的代码如下:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.SQLException;
class jdbcdisplay
{
public static void main(String[] args)
{
Connection conn=null;
try
{
String driver="oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
System.out.println("Connecting to database...");
String jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl";
String user="Sourajyoti";
String password="Sourajyoti7";
conn=DriverManager.getConnection(jdbcUrl,user,password);
Statement stmt=conn.createStatement();
String sql="SELECT ENAME,SAL,HIREDATE FROM EMP";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next())
{
String name=rs.getString("ENAME");
double salary=rs.getDouble(2);
Date date=rs.getDate("HIREDATE");
System.out.print("Name: "+name);
System.out.print("Salary: "+salary);
System.out.println("Hiredate: "+date);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
System.out.println("GoodBye!!");
}
}