使用Jar文件和命令提示符时jdbc的问题

时间:2011-04-19 11:42:43

标签: java oracle command-line jdbc

我使用JDeveloper开发JDBC程序,现在我的程序完成了,我想单独运行它当我尝试从命令提示符运行我的jar文件时遇到问题。

当我使用命令提示符运行我的程序时,我的程序无法连接到我的oracle数据库!但是当我使用我的JDeveloper IDE时,没有问题! 我的IDE运行此命令:

javaw.exe -client -classpath C:\JDeveloper\mywork\SourceManagmentTools\.adf;C:\JDeveloper\mywork\SourceManagmentTools\Client\classes;C:\Oracle\Middleware\jdeveloper\jdev\lib\jdev-rt.jar;C:\JDeveloper\mywork\SourceManagmentTools\Client\ojdbc14.jar;C:\JDeveloper\mywork\SourceManagmentTools\Client\quaqua-filechooser-only.jar;C:\JDeveloper\mywork\SourceManagmentTools\Client\quaqua-test.jar;C:\JDeveloper\mywork\SourceManagmentTools\Client\quaqua.jar;C:\JDeveloper\mywork\SourceManagmentTools\Client\swing-layout.jar;C:\JDeveloper\mywork\SourceManagmentTools\Client\image;C:\JDeveloper\mywork\SourceManagmentTools\Client\lipstikLF-1.1.jar -Djavax.net.ssl.trustStore=C:\Oracle\Middleware\wlserver_10.3\server\lib\DemoTrust.jks Main.GUI.SmRun amir

我使用这个命令:

Java -jar smt.jar amir

当我运行IDE命令时没有问题,一切正常,请你帮我吗? (我的程序正在运行& working我无法从数据库中获取数据)

当我尝试从我的数据库中选择时,我收到以下错误...

C:\JDeveloper\mywork\SourceManagmentTools\Client\deploy>java -jar smt.jar amir
[Ljava.lang.StackTraceElement;@7a148bd3[Ljava.lang.StackTraceElement;@51b48197[L
java.lang.StackTraceElement;@58ecb281[Ljava.lang.StackTraceElement;@25fa1bb6[Lja
va.lang.StackTraceElement;@62da3a1e[Ljava.lang.StackTraceElement;@15f48262[Ljava
.lang.StackTraceElement;@7dc05ffd[Ljava.lang.StackTraceElement;@15e232b5[Ljava.l
ang.StackTraceElement;@36baa466[Ljava.lang.StackTraceElement;@177f409c[Ljava.lan
g.StackTraceElement;@2b8bbc5a[Ljava.lang.StackTraceElement;@62facf0b[Ljava.lang.
StackTraceElement;@5ff3ce5c[Ljava.lang.StackTraceElement;@561777b1Exception in t
hread "AWT-EventQueue-0" java.lang.NullPointerException
        at Main.Database.SmDatabase.selectCustomers(SmDatabase.java:151)
        at Main.GUI.SmCreateProject.this_windowOpened(SmCreateProject.java:109)
        at Main.GUI.SmCreateProject.access$000(SmCreateProject.java:30)
        at Main.GUI.SmCreateProject$1.windowOpened(SmCreateProject.java:58)
        at java.awt.AWTEventMulticaster.windowOpened(Unknown Source)
        at java.awt.Window.processWindowEvent(Unknown Source)
        at javax.swing.JDialog.processWindowEvent(Unknown Source)
        at java.awt.Window.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

mydatabase类的一部分:

public class SmDatabase {

    Connection conn = null;

    public SmDatabase() {
        try {
            String drivername = "oracle.jdbc.driver.OracleDriver";
            Class.forName(drivername);

            String ServerName = "127.0.0.1";
            String ProtNumber = "1521";
            //String sid = "DEVP0";
            String sid = "cms";

            String url =
                "jdbc:oracle:thin:@" + ServerName + ":" + ProtNumber + ":" +
                sid;

            //String Username = "PENTAVCB";
            //String Password = "pentasoft123";
            String Username = "cmsuser";
            String Password = "pass";

            conn = DriverManager.getConnection(url, Username, Password);
        } catch (ClassNotFoundException e) {
            System.out.print(e.getStackTrace());
        } catch (SQLException se) {
            System.out.print(se.getStackTrace());
        }
    }

public Vector<String[]> selectCustomers(String command) {
    ResultSet resultSet = null;
    Statement statement;
    Vector<String[]> customerName = new Vector<String[]>();
    try {
        statement = conn.createStatement();
LINE151     resultSet = statement.executeQuery(command);

        while (resultSet.next()) {
            String[] temp = new String[2];
            temp[0] = resultSet.getString("V_CUST_NAME");
            temp[1] = resultSet.getString("V_CUST_CODE");
            customerName.add(temp);
        }
        resultSet.close();
        statement.close();
        //    conn.close();
    } catch (SQLException e) {
        System.out.println(e.getStackTrace());
    }
    return customerName;
}

4 个答案:

答案 0 :(得分:0)

你有什么例外?如果它是ClassNotFoundException,那么您只需要在命令中添加-cp oracleclient.jar

Oracle客户端jar也可以放在传递给-classpath的{​​{1}}争论的末尾。

答案 1 :(得分:0)

e.printStackTrace();将产生比System.out.println(e.getStackTrace());更有用的输出。我会改变它在你的应用程序中出现的任何地方,然后再试一次。 DriverManager.getConnection(url, Username, Password);很可能抛出一个你没有正确打印的异常,这会产生“[Ljava.lang.StackTraceElement ...”垃圾。

答案 2 :(得分:0)

输出指示第151行的空指针异常,它应指示“statement”变量为null。 “conn”不为null,或者在前一行抛出异常。这很奇怪,如果你有一个有效的连接,你应该能得到一个Statement。行号是否可能被1关闭?您能否将代码更改为:


   if conn is null  System.out.println("conn is null!!!");
   else System.out.println("conn is set");

   statement = conn.createStatement();   

   if statement is null  System.out.println("statement is null!!!");
   else System.out.println("statement is set");

   resultSet = statement.executeQuery(command);

   if resultSet is null  System.out.println("resultSet is null!!!");
   else System.out.println("resultSet is set");

我没有看到调用SmDatabase的位置来设置conn。它是从其他地方调用的吗?调用是否捕获异常并且不打印错误消息?

答案 3 :(得分:0)

我使用此命令解决了我的问题:

java -client -classpath ojdbc14.jar;lipstikLF-1.1.jar;swing-layout.jar;smt.jar Main.GUI.SmRun rimrim

我想我们在项目中使用的那些库也需要运行,但我不确定,我只是建议这项工作

java -client -classpath <name of you library>;<name of you library>;<name of your jar file> nameOfPackage.nameOfRunClass parameter[if any]

如果有人在这篇文章中添加一些有用的信息,我将非常感激