打开Office数据库错误(运行时显示异常)

时间:2009-12-28 05:01:18

标签: java jdbc openoffice.org

## This is the code i m runnig in linux ubuntu.. ##
import java.sql.*;
/**
 *
 * @author spk
 */
public class Connectionsetting {

    private static Connection con;

    private static java.sql.Statement sm;

    private static ResultSet rs;

    public void close()
    {
        try
        {
            sm.close();
            con.close();
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
    }
    public void connection() {
       String db_file_name_prefix = "/home/spk/Desktop/database/mydb.odb";
/*
    If required change the file name if you are working in windows os
  connection is in work

 */
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            System.out.println("Driver Found");
        }
            catch(ClassNotFoundException e){
            System.out.println(e.getMessage());
            }
             try {
                        //con=DriverManager.getConnection("jdbc:hsqldb:file"+db_file_name_prefix,"sa", "");
              con=DriverManager.getConnection("jdbc:hsqldb:file:"+db_file_name_prefix,"", "");
            System.out.println("Connection Eshtablished");


            sm=con.createStatement();
       // sm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException e) {
           System.out.println(e.getMessage());
        }
    }

    public static int executeupdate(String query) {
        //Execute & update block insert, update, delete statements
        int bool = 0;
        try {
                bool=sm.executeUpdate(query);
                con.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return bool;
        }

    public ResultSet executeQuery(String query) {
        //Block Returns single resultset,,,sql statements such as sql select
        ResultSet rs=null;
        try {
            rs = sm.executeQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    public boolean checkTableStatus(String tblName) {
        String sql = "selec * from cat";

        ResultSet rs=null;

        boolean status = false;

        int i = 0;

        String allTableNames[] = new String[20];
        try {
            connection();
            rs = sm.executeQuery(sql);
            while (rs.next()) {

                allTableNames[i] = rs.getString(0);
                i++;

                if (allTableNames[i].equals(tblName)) {
                    status = true;
                    break;
                } else {
                    status = false;
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }
  public static void main(String []args)
    {
      String query,s1,s2,s3,s4,s5,s6,s7,s8;
      int i=0;
       Connectionsetting cn=new Connectionsetting();
       cn.connection();
       s1="same";
       s2="sam";
       s3="923847";
       s4="sam";
       s5="sam";
       s6="sam";
       s7="sam";
       s8="R01";
       query="insert into Agency_Master values("+s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+","+s8+")";
       i=cn.executeupdate(query);

       System.out.println(i);


    }

}

但它再次给出了这个错误.. 谁能告诉我哪里错了。数据库是开放的办公室基地.. 感谢名单

Driver Found
Exception in thread "main" java.lang.NoClassDefFoundError: org.hsqldb.jdbc.JDBCConnection
   at java.lang.Class.initializeClass(libgcj.so.90)
   at org.hsqldb.jdbc.JDBCDriver.getConnection(JDBCDriver.java:285)
   at org.hsqldb.jdbc.JDBCDriver.connect(JDBCDriver.java:245)
   at java.sql.DriverManager.getConnection(libgcj.so.90)
   at java.sql.DriverManager.getConnection(libgcj.so.90)
   at Connectionsetting.connection(Connectionsetting.java:45)
   at Connectionsetting.main(Connectionsetting.java:116)
Caused by: java.lang.ClassNotFoundException: java.sql.Wrapper not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/home/COOPERENG/spk/workspace/Test/,file:/home/COOPERENG/spk/workspace/hsqldb.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
   at java.net.URLClassLoader.findClass(libgcj.so.90)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.VMClassLoader.defineClass(libgcj.so.90)
   at java.lang.ClassLoader.defineClass(libgcj.so.90)
   at java.security.SecureClassLoader.defineClass(libgcj.so.90)
   at java.net.URLClassLoader.findClass(libgcj.so.90)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.Class.forName(libgcj.so.90)
   at java.lang.Class.initializeClass(libgcj.so.90)
   ...6 more

2 个答案:

答案 0 :(得分:1)

您当然没有在应用程序类路径中加载HSQL数据库,并且您的连接字符串看起来不正确。

添加在openoffice lib目录中找到的HSQL数据库驱动程序jar,并按照post进行操作。

您可以直接参考HSQL documentation

答案 1 :(得分:0)

Caused by: java.lang.ClassNotFoundException: java.sql.Wrapper not found in gnu.gcj.runtime.SystemClassLoader

众所周知,GNU JDK是错误的。摆脱GNU JDK并安装Sun JDK

也就是说,你的JDBC代码编写得很糟糕。它不是线程安全的,并且有潜在的资源泄漏。您应该在方法块中声明所有外部资源,并确保不作为静态变量。您应该在同一个try / finally块中获取和关闭资源。要了解如何正确地执行JDBC,您可能会发现this article很有用。