Então galera sou novo no Java, e com a ajuda do meu professor criei essas classes mais não consigo inserir dados na mina tabela no Oracle. o que eu faço?
package teste.oracle;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class PessoaDAOTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\\n");
PessoaDAO pessoaDAO = new PessoaDAO();
try {
Pessoa pessoa = leDadosPessoa(sc);
pessoaDAO.criar(pessoa);
System.out.println("Pessoa criada:" + pessoa);
System.out.println("Digite o id para consulta:");
int idPessoaParaConsulta = Integer.parseInt(sc.next().trim());
pessoa = pessoaDAO.buscarPorId(idPessoaParaConsulta);
System.out.println(pessoa);
listarPessoas(pessoaDAO);
System.out.println("Digite o id para apagar:");
idPessoaParaConsulta = Integer.parseInt(sc.next().trim());
pessoaDAO.apagar(idPessoaParaConsulta);
listarPessoas(pessoaDAO);
System.out.println("Digite o id para atualizar:");
idPessoaParaConsulta = Integer.parseInt(sc.next().trim());
pessoa = pessoaDAO.buscarPorId(idPessoaParaConsulta);
System.out.println(pessoa);
pessoa = leDadosPessoa(sc);
pessoa.setId(idPessoaParaConsulta);
pessoaDAO.salvar(pessoa);
System.out.println("Bucando registro atualizado");
pessoa = pessoaDAO.buscarPorId(idPessoaParaConsulta);
System.out.println(pessoa);
} catch (DaoException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
sc.close();
}
}
private static Pessoa leDadosPessoa(Scanner sc)
throws ParseException {
Pessoa pessoa = new Pessoa();
System.out.println("Digite o prenome");
String prenome = sc.next().trim();
System.out.println("Digite o sobrenome");
String sobrenome = sc.next().trim();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Digite a data de nascimento");
Date dataNasc = sdf.parse(sc.next());
System.out.println("Digite a data de falecimento");
Date dataFalec = null;
String dataFalecStr = sc.next();
if (dataFalecStr != null && !"".equals(dataFalecStr.trim())) {
dataFalec = sdf.parse(dataFalecStr);
}
pessoa.setPrenome(prenome);
pessoa.setSobrenome(sobrenome);
pessoa.setDataNascimento(dataNasc);
pessoa.setDataFalecimento(dataFalec);
return pessoa;
}
private static void listarPessoas(PessoaDAO pessoaDAO) throws DaoException {
System.out.println("\n\nLista de todos as pessoas");
List<Pessoa> listaTodos = pessoaDAO.buscarTodos();
for (Pessoa p : listaTodos) {
System.out.println(p);
}
}
}
package teste.oracle;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class PessoaDAO {
private String QUERY_SEQUENCE = "select TB_PESSOA_SEQ.nextval as NOVO_ID_PESSOA from dual";
private String QUERY_BUSCAR_TODOS =
"select " +
" ID_PESSOA, " +
" NM_PRENOME, " +
" NM_SOBRENOME, " +
" DT_NASCIMENTO, " +
" DT_FALECIMENTO " +
"from " +
" TB_PESSOA";
private String QUERY_BUSCAR_POR_ID =
"select " +
" ID_PESSOA, " +
" NM_PRENOME, " +
" NM_SOBRENOME, " +
" DT_NASCIMENTO, " +
" DT_FALECIMENTO " +
"from " +
" TB_PESSOA " +
"where " +
" TB_PESSOA.ID_PESSOA = ?";
private String QUERY_APAGAR_POR_ID = "delete from TB_PESSOA where TB_PESSOA.ID_PESSOA = ?";
private String QUERY_CRIAR_PESSOA =
"insert into TB_PESSOA (" +
" ID_PESSOA, " +
" NM_PRENOME, " +
" NM_SOBRENOME, " +
" DT_NASCIMENTO, " +
" DT_FALECIMENTO ) " +
"values(" +
" ?, ?, ?, ?, ?)" ;
private String QUERY_ATUALIZAR_PESSOA =
"update TB_PESSOA " +
"set " +
" NM_PRENOME = ?, " +
" NM_SOBRENOME = ?, " +
" DT_NASCIMENTO = ?, " +
" DT_FALECIMENTO = ? " +
"where " +
" TB_PESSOA.ID_PESSOA = ? ";
private int getProximoValorSequence() throws DaoException {
Connection conn = DbUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
int novoIdPessoa = 0;
try {
statement = conn.prepareStatement(QUERY_SEQUENCE);
result = statement.executeQuery();
if (result.next()) {
novoIdPessoa = result.getInt("NOVO_ID_PESSOA");
}
} catch (SQLException e) {
throw new DaoException(e);
} finally {
DbUtil.close(conn, statement, result);
}
return novoIdPessoa;
}
/**
* Este metodo retorna uma lista com todos os objetos pessoa armazenados
* @return
* @throws DaoException
*/
public List<Pessoa> buscarTodos() throws DaoException {
Connection conn = DbUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
List<Pessoa> listaTodos = new ArrayList<Pessoa>();
try {
statement = conn.prepareStatement(QUERY_BUSCAR_TODOS);
result = statement.executeQuery();
while (result.next()) {
Pessoa pessoa = new Pessoa();
pessoa.setId(result.getInt("ID_PESSOA"));
pessoa.setPrenome(result.getString("NM_PRENOME"));
pessoa.setSobrenome(result.getString("NM_SOBRENOME"));
pessoa.setDataNascimento(DbUtil.getJavaDate(result, "DT_NASCIMENTO"));
pessoa.setDataFalecimento(DbUtil.getJavaDate(result, "DT_FALECIMENTO"));
listaTodos.add(pessoa);
}
} catch (SQLException e) {
throw new DaoException(e);
} finally {
DbUtil.close(conn, statement, result);
}
return listaTodos;
}
/**
* Este metodo retorna o objeto pessoa de acordo com o id
* @return
* @throws DaoException
*/
public Pessoa buscarPorId(int idPessoa) throws DaoException {
Connection conn = DbUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
Pessoa pessoa = null;
try {
statement = conn.prepareStatement(QUERY_BUSCAR_POR_ID);
statement.setInt(1, idPessoa);
result = statement.executeQuery();
if (result.next()) {
pessoa = new Pessoa();
pessoa.setId(result.getInt("ID_PESSOA"));
pessoa.setPrenome(result.getString("NM_PRENOME"));
pessoa.setSobrenome(result.getString("NM_SOBRENOME"));
pessoa.setDataNascimento(DbUtil.getJavaDate(result, "DT_NASCIMENTO"));
pessoa.setDataFalecimento(DbUtil.getJavaDate(result, "DT_FALECIMENTO"));
}
} catch (SQLException e) {
throw new DaoException(e);
} finally {
DbUtil.close(conn, statement, result);
}
return pessoa;
}
/**
* Este metodo apaga o objeto pessoa de acordo com o id
* @throws DaoException
*/
public void apagar(int idPessoa) throws DaoException {
Connection conn = DbUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
statement = conn.prepareStatement(QUERY_APAGAR_POR_ID);
statement.setInt(1, idPessoa);
statement.execute();
} catch (SQLException e) {
throw new DaoException(e);
} finally {
DbUtil.close(conn, statement, result);
}
}
/**
* Este metodo apaga o objeto pessoa
* @throws DaoException
*/
public void apagar(Pessoa pessoa) throws DaoException {
apagar(pessoa.getId());
}
/**
* Este metodo salva o objeto pessoa
* @throws DaoException
*/
public void salvar(Pessoa pessoa) throws DaoException {
Connection conn = DbUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
statement = conn.prepareStatement(QUERY_ATUALIZAR_PESSOA);
statement.setString(1, pessoa.getPrenome());
statement.setString(2, pessoa.getSobrenome());
statement.setDate(3, DbUtil.getSqlDate(pessoa.getDataNascimento()));
statement.setDate(4, DbUtil.getSqlDate(pessoa.getDataFalecimento()));
statement.setInt(5, pessoa.getId());
statement.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e);
} finally {
DbUtil.close(conn, statement, result);
}
}
/**
* Este metodo cria o objeto pessoa e retorna o objeto criado com o novo id
* preenchido
* @throws DaoException
*/
public void criar(Pessoa pessoa) throws DaoException {
int novoIdPessoa = getProximoValorSequence();
Connection conn = DbUtil.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
try {
statement = conn.prepareStatement(QUERY_CRIAR_PESSOA);
statement.setInt(1, novoIdPessoa);
statement.setString(2, pessoa.getPrenome());
statement.setString(3, pessoa.getSobrenome());
statement.setDate(4, DbUtil.getSqlDate(pessoa.getDataNascimento()));
statement.setDate(5, DbUtil.getSqlDate(pessoa.getDataFalecimento()));
statement.executeUpdate();
pessoa.setId(novoIdPessoa);
} catch (SQLException e) {
throw new DaoException(e);
} finally {
DbUtil.close(conn, statement, result);
}
}
}
package teste.oracle;
import java.util.Date;
public class Pessoa {
private int id;
private String prenome;
private String sobrenome;
private Date dataNascimento;
private Date dataFalecimento;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPrenome() {
return prenome;
}
public void setPrenome(String prenome) {
this.prenome = prenome;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public Date getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Date dataNascimento) {
this.dataNascimento = dataNascimento;
}
public Date getDataFalecimento() {
return dataFalecimento;
}
public void setDataFalecimento(Date dataFalecimento) {
this.dataFalecimento = dataFalecimento;
}
@Override
public String toString() {
String value = String
.format("Pessoa[id:%d;prenome:%s;sobrenome:%s;dataNascimento:%4$td/%4$tm/%4$tY;dataFalecimento:%5$td/%5$tm/%5$tY]",
this.id,
this.prenome,
this.sobrenome,
this.dataNascimento,
this.dataFalecimento);
return value;
}
}
package teste.oracle;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbUtil {
private static final String URL_DATABASE = "jdbc:oracle:thin:@localhost:1521";
private static final String DRIVER_JDBC = "oracle.jdbc.driver.OracleDriver";
private static final String USUARIO_DB = "system";
private static final String SENHA_USUARIO_DB = "123456";
static {
try {
Class.forName(DRIVER_JDBC);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
Connection connection = DriverManager.getConnection(URL_DATABASE, USUARIO_DB, SENHA_USUARIO_DB);
connection.setAutoCommit(true);
return connection;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static java.util.Date getJavaDate(ResultSet result, String nomeCampo) throws SQLException {
Date dataFalecimento = result.getDate(nomeCampo);
java.util.Date javaDate = null;
if (dataFalecimento != null) {
javaDate = new Date(dataFalecimento.getTime());
}
return javaDate;
}
public static Date getSqlDate(java.util.Date date) {
Date sqlDate = null;
if (date != null) {
sqlDate = new java.sql.Date(date.getTime());
}
return sqlDate;
}
public static void close(Connection conn, Statement statement, ResultSet result) {
try {
if (conn != null) {
conn.close();
}
if (statement != null) {
statement.close();
}
if (result != null) {
result.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package teste.oracle;
public class DaoException extends Exception {
public DaoException() {
}
public DaoException(String message) {
super(message);
}
public DaoException(Throwable throwed) {
super(throwed);
}
public DaoException(String message, Throwable throwed) {
super(message, throwed);
}
}