我有一个连接到oracle数据库的java代码。 它在Windows机器和Mac上运行良好。但是当我尝试从openstack机器运行代码(在centos上运行)时,我收到错误。通过调试,我发现机器甚至没有连接到数据库。
这是代码
public static void main(String[] args){
System.out.println("Connection requested");
String selectStatement = "SELECT * FROM TABLE_NAME";
Connection connection = getConnection("username","password");
try {
PreparedStatement preparedStatement = connection.prepareStatement(selectStatement);
ResultSet result = preparedStatement.executeQuery();
System.out.println("Connection successful");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(String username, String password) {
Connection connection = null;
try {
Class.forName("classname");
connection = DriverManager.getConnection("url",username, password);
} catch (ClassNotFoundException e) {
System.out.println("Connection error\n" + e);
} catch (SQLException e) {
System.out.println("Connection error\n" + e);
}
System.out.println("Connection acquired");
return connection;
}
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/net/ns/Message
at oracle.net.ns.NSProtocol.disconnect(NSProtocol.java:470)
at oracle.jdbc.driver.T4CConnection.logoff(T4CConnection.java:515)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:595)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.dal.OracleDBConnection.getConnection(OracleDBConnection.java:289)
at com.data.format.FeedFormatter.getHistory(FeedFormatter.java:167)
at com.data.FeedGenerator.generateJson(FeedGenerator.java:143)
at com.data.FeedGenerator.main(FeedGenerator.java:55)
Caused by: java.lang.ClassNotFoundException: oracle.net.ns.Message
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)
我已将代码导出为可运行的jar。因此jdbc驱动程序应该可用。我在这里缺少什么?
编辑:正如评论中所建议的那样,我查看了我创建的jar,我可以看到Message.class
存在。而这个jar在OSX中运行良好,而在CentOS中根本没有连接。我可以看到Connection Requested
,之后几分钟都没有看到。