0

I'm trying to develop a program including a JTable and a Embedded database. So i added the logic to the table model(logic is not yet completely built to work with the jTable due to this error).

Here i start the connection in the setup() method.

private void setup(){
        try{
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            connection=DriverManager.getConnection("jdbc:derby:sheet;create=true","test","1234");
            statement=connection.createStatement();
        }

        catch(ClassNotFoundException cnf){
            System.out.println("class error");
        }

        catch(SQLException se){
            System.out.println(se);
        }
    }

finally, i clear all the data and set them to null, when the program begins.

private void clearDataBase(){
        for(int i=1;i<this.getColumnCount();i++){
            for(int j=1;j<this.getRowCount();j++){

                try{
                    prepStatement=connection.prepareStatement("UPDATE SHEETDATA SET "+getColumnName(i)+"=NULL WHERE INDEX IN (?)");
                    prepStatement.setInt(1, j);
                    prepStatement.executeUpdate();
                }
                catch(SQLException se){
                    System.out.println(se);  
                }
            }

        }
    }

In this manner, it gives me an error, schema TEST deosn's exist. But in my database i have an schema called TEST and also, i have have included all the columns to it. Then i used the default schema APP and added the data to it.(Also changed the statement as "UPDATE SHEETDATA SET "+getColumnName(i)+"=NULL WHERE INDEX IN (?)"). But then the error saying Table/View 'APP.SHEETDATA' does not exist comes.

I'm pretty much sure that there's no error when adding data to the database. Also, this code works fine when i change the driver to ClientDriver and use a server-client aproach. I can't find the error here and also, i have searched the internet to find a solution. I'm posting this because i can't find the solutions. Thanks in Advance!

Here's my complete TableModel class:

package myUserInterface;


import javax.swing.table.AbstractTableModel;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;


public class MyTableModel extends AbstractTableModel {

    private String[] columnNames;
    private Cell[][] cells;
    private String[][] data;
    private Connection connection;
    private Statement statement;
    private PreparedStatement prepStatement;

    MyTableModel(int row,int col){
        columnNames=setColumns(col);
        data=setRows(row,col);
        setCells(row,col);
        setup();
        clearDataBase();
    }

    @Override
    public int getColumnCount(){
        return columnNames.length;
    }

    @Override
    public int getRowCount(){
        return data.length;
    }

    @Override
    public Object getValueAt(int row,int col){
        return cells[row][col].getContent();
    }


    @Override
    public String getColumnName(int col){
        return columnNames[col];
    }


    @Override
    public Class getColumnClass(int col){
        return getValueAt(0,col).getClass();
    }


    @Override
    public boolean isCellEditable(int row,int col){
        return col!=0;
    }


    @Override
    public void setValueAt(Object content,int row,int col){
        data[row][col]=(String)content;
        cells[row][col].setContent((String)content);
        String query="UPDATE IMESHA.SHEETDATA SET "+this.getColumnName(col)+"="+"'"+(String)content+"'"+
                "WHERE INDEX IN ("+row+")";

        //updating the database accordingly to the changes made to the jTable.
        try{
            statement.executeUpdate(query);
        }
        catch(SQLException se){
           System.out.println(se);
        }

        fireTableCellUpdated(row,col);
    }


    public String getCellContent(int row,int col){
        return cells[row][col].getContent();
    }

    public String getCellTemp(int row,int col){
        return cells[row][col].getTemp();
    }

    //This method returns a string array, which will be used as the set of column names
    private String[] setColumns(int col){
        String[] columns=new String[col];
        columns[0]="Index";
        int x=66;
        while(x<92){
            columns[x-65]=Character.toString((char)(x-1));
            x++;
        }
        return columns;
    }

    //this method returns the 2 dimension array of cell objects
    private String[][] setRows(int row,int col){
        data=new String[row][col];
        int index=1;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(j==0){
                    data[i][j]=Integer.toString(index);
                    index++;
                    continue;
                }
                data[i][j]="";
            }            
        }
        return data;
    }

    private void setCells(int row,int col){
        cells=new Cell[row][col];
        int index=1;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(j==0){
                    cells[i][j]=new Cell(Integer.toString(index));
                    index++;
                    continue;
                }
                cells[i][j]=new Cell();
            }            
        }
    }

    /**
     * This method is to connect the database with the table
     */
    private void setup(){
        try{
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            connection=DriverManager.getConnection("jdbc:derby:sheet;create=true","test","1234");
            statement=connection.createStatement();
        }

        catch(ClassNotFoundException cnf){
            System.out.println("class error");
        }

        catch(SQLException se){
            System.out.println(se);
        }
    }


    private void clearDataBase(){
        for(int i=1;i<this.getColumnCount();i++){
            for(int j=1;j<this.getRowCount();j++){

                try{
                    prepStatement=connection.prepareStatement("UPDATE APP.SHEETDATA SET "+getColumnName(i)+"=NULL WHERE INDEX IN (?)");
                    prepStatement.setInt(1, j);
                    prepStatement.executeUpdate();
                }
                catch(SQLException se){
                    System.out.println(se);  
                }
            }

        }
    }

}
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • 1
    See also `JDBCAdapter` cited [here](http://stackoverflow.com/a/22183184/230513). – trashgod Jan 06 '15 at 10:26
  • Thanks for the guild. But unfortunately that's not the answer i'm seeking. My problem lies in between creating the database and reading/updating database(though some schemas are actually in the database, they are not recognized.) – Imesha Sudasingha Jan 06 '15 at 11:50

1 Answers1

1

You're probably using two different databases, both named "sheet".

When you run with the client/server driver, you're using a database named "sheet" which is stored in the home directory of your Derby Network Server.

While when you run with the embedded driver, you're using a database named "sheet" which is stored in the current working directory of your program when you run it.

Try searching your hard drive for a folder named "sheet", and see if you don't find two different folders.

For a few other possible reasons why your schema doesn't look the way you expect when you connect to Derby, see this question: Is it necessary to create tables each time you connect the derby database?

Community
  • 1
  • 1
Bryan Pendleton
  • 16,128
  • 3
  • 32
  • 56