- http://uaihebert.com/?p=15
- http://www.avajava.com/tutorials/lessons/how-do-i-create-a-jsp-error-page-to-handle-exceptions.html
web.xml
404.jsp
exception.jsp
create table usuarios ( id int not null primary key, nome varchar(40) not null unique, senha varchar(40) not null, admin int not null ); insert into usuarios values (1, 'admin', '123', 1); create table paises ( id int not null primary key, codigo varchar(10) not null unique, nome varchar(40) not null unique ); create table estados ( id int not null primary key, codigo varchar(10) not null unique, nome varchar(40) not null unique, id_pais int not null references paises (id) ); create table cidades ( id int not null primary key, codigo varchar(10) not null unique, nome varchar(40) not null unique, capital varchar(1) not null, populacao int not null, id_estado int not null references estados (id) );
package br.com.ipa.usuario;
import br.com.ipa.dao.*;
import java.util.*;
public class UsuarioCtrl {
private static synchronized int getProximoID() throws Exception {
RecordSet rs = BD.executeQuery("SELECT MAX(id)+1 FROM usuarios");
if (!rs.next()) {
return 0;
}
return rs.getInt(1);
}
public static synchronized void incluir(Usuario u) throws Exception {
u.setId(getProximoID());
String sql = "INSERT INTO usuarios VALUES (" +
u.getId().toString() + ", " +
"'" + u.getNome() + "', " +
"'" + u.getSenha() + "', " +
(u.getAdmin()?"1":"0") + ")";
BD.execute(sql);
}
public static synchronized void excluir(Usuario u) throws Exception {
String sql = "DELETE FROM usuarios WHERE id = " + u.getId();
BD.execute(sql);
}
public static synchronized void alterar(Usuario u) throws Exception {
String sql = "UPDATE usuarios SET " +
"nome = '" + u.getNome() + "', " +
"senha = '" + u.getSenha() + "', " +
"admin = " + (u.getAdmin()?"1":"0") + " " +
"WHERE id = " + u.getId().toString();
BD.execute(sql);
}
public static synchronized Usuario carregar(Integer id) throws Exception {
RecordSet rs = BD.executeQuery("SELECT * FROM usuarios WHERE id=" + id.toString());
if (!rs.next()) {
return null;
}
Usuario usuario = new Usuario();
usuario.setId(rs.getInt("id"));
usuario.setNome(rs.getString("nome"));
usuario.setSenha(rs.getString("senha"));
usuario.setAdmin(rs.getInt("admin") == 1);
return usuario;
}
public static synchronized ArrayList<Usuario> listar() throws Exception {
ArrayList usuarios = new ArrayList();
RecordSet rs = BD.executeQuery("SELECT * FROM usuarios");
while (rs.next()) {
Usuario usuario = new Usuario();
usuario.setId(rs.getInt("id"));
usuario.setNome(rs.getString("nome"));
usuario.setSenha(rs.getString("senha"));
usuario.setAdmin(rs.getInt("admin") == 1);
usuarios.add(usuario);
}
return usuarios;
}
}
package br.com.ipa.dao;
import java.sql.*;
public class BD {
private static final String url =
"jdbc:derby://localhost:1527/ipa;user=admin;password=123";
private static Connection getConnection() throws Exception {
return DriverManager.getConnection(url);
}
public synchronized static int execute(String sql) throws Exception {
Connection conn = BD.getConnection();
Statement stmt = conn.createStatement();
try {
return stmt.executeUpdate(sql);
} finally {
stmt.close();
conn.close();
}
}
public synchronized static RecordSet executeQuery(String sql) throws Exception {
Connection conn = BD.getConnection();
Statement stmt = conn.createStatement();
try {
return new RecordSet(stmt.executeQuery(sql));
} finally {
stmt.close();
conn.close();
}
}
public static void main(String[] args) throws Exception {
System.out.println(BD.getConnection());
}
}
package br.com.ipa.dao;
import java.sql.*;
import com.sun.rowset.CachedRowSetImpl;
public class RecordSet extends CachedRowSetImpl {
public RecordSet() throws SQLException {
super();
}
public RecordSet(ResultSet rs) throws SQLException {
super();
try {
populate(rs);
} finally {
rs.close();
}
}
}
import java.io.*;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
public class XMLUtil {
private static String dir = "c:/";
public static void setDiretorio(String d) { dir = d; }
public static String getDiretorio() { return dir; }
public static void salvar(Object objeto, String arq) throws Exception {
XMLEncoder xml = null;
try {
File f = new File(getDiretorio() + arq);
xml = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(f)));
xml.writeObject(objeto);
xml.flush();
} finally {
if (xml != null) {
xml.close();
}
}
}
public static Object buscar(String arq) throws Exception {
Object objeto = null;
XMLDecoder xml = null;
try {
File f = new File(getDiretorio() + arq);
if (!f.exists()) {
return null;
}
xml = new XMLDecoder(
new BufferedInputStream(
new FileInputStream(f)));
objeto = xml.readObject();
} finally {
if (xml != null) {
xml.close();
}
}
return objeto;
}
public static void main(String args[]) throws Exception {
String s = "IPA-OO-2008";
XMLUtil.salvar(s, "Teste.xml");
System.out.println(XMLUtil.buscar("Teste.xml").toString());
}
}