views:

103

answers:

2
## 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);


    }

}

But its again giving this error.. can any one tell me where i m wrong.the database is open office base.. thanx

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
A: 

You certainly haven't loaded the HSQL database in your application classpath and your connection string does not look right.

Add the HSQL database driver jar found in openoffice lib directory and follow this post.

You can refer directly to the HSQL documentation

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

GNU JDK is known to be buggy. Get rid of GNU JDK and install Sun JDK.

That said, your JDBC code is poorly written. It is not threadsafe and has potential resource leaks. You should declare all external resources in a method block and for sure not as a static variable. You should acquire and close resources in the very same try/finally block. To get an idea how to do the JDBC properly, you may find this article useful.

BalusC