Boas Práticas

2 respostas
J
Criei um método de insert e gostaria se a forma como foi colocada segue a melhor prática.

Gostaria de deixar o mais generico possível, pois nessa classe ainda deve ter delete e select

public static void insert(File f, String tipoCont, String tipoDoc) throws SQLException {

try {

FileInputStream fis = new FileInputStream(f);

int len = (int)f.length();		

String insert = (insert into dbo.files_store (file_name, content, update_date, content_type, myme, deleted) values (?, ?, getdate(), ?, ?, ?));

String select = (select file_name from dbo.files_store where file_name = ?);

String update = (update dbo.files_store set file_name = ?, content = ?, update_date = getdate(), content_type = ?, myme = ?, deleted = ?);

stmt = getConnection().prepareStatement(select);

if (stmt == null){

stmt = getConnection().prepareStatement(insert);

stmt.setString(1,f.getName());

stmt.setBinaryStream(2, fis, len);

stmt.setString(3, tipoDoc);

stmt.setString(4, tipoCont);

stmt.setString(5, N);	

}

else{

stmt = getConnection().prepareStatement(update);

stmt.setString(1,f.getName());

stmt.setBinaryStream(2, fis, len);

stmt.setString(3, tipoDoc);

stmt.setString(4, tipoCont);

stmt.setString(5, N);	

}

stmt.executeUpdate();	

stmt.close();

con.close();
} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

2 Respostas

Kassiane_Pretti

Coloca a tag code para ficar mais facil de ler seu codigo

Assim poderemo te ajudar…

ViniGodoy

Você deve fechar sua conexão, statement, etc, num finally.
Caso contrário, corre o risco de uma exceção deixar elas abertas.

Dentro do seu try…catch, adicione um novo bloco try…finally.

Ou use as classes de BD do Spring, que gerenciam isso automaticamente e são muito parecidas com as da própria Sun.

Ah, e se você ainda não sabe usar a tag code, dá uma lida aqui:
http://www.guj.com.br/posts/list/50115.java

Faz realmente muita falta.

Criado 11 de julho de 2008
Ultima resposta 11 de jul. de 2008
Respostas 2
Participantes 3