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);
}
}
}
}
}