Olá pessoal boa-noite,
Estou aprendendo a usar JSP, Servlets, criei uma classe que usa properties essa classe pega as informações do arquivo de proporties “db.txt” e joga em uma classe que fará a conexão com o banco de dados, mas esta dando algum problema por que ela não conecta no banco. Segue abaixo as classes:
public class Propriedade {
private static Properties props;
private static FileInputStream fis;
public static Properties properties() throws IOException{
try{
props = new Properties();
fis = new FileInputStream("/WEB-INF/db.txt");
props.load(fis);
}
catch(Exception e){
}
finally{
fis.close();
}
return props;
}
public class Conectar implements Conexao {
private String driver;
private String usuario;
private String senha;
private String url;
private Connection conn;
private PreparedStatement stmt;
@Override
public Connection conexao() throws Exception {
driver = Propriedade.properties().getProperty("driver");
usuario = Propriedade.properties().getProperty("usuario");
senha = Propriedade.properties().getProperty("senha");
url = Propriedade.properties().getProperty("url");
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,usuario,senha);
}
catch(SQLException e){
}
catch(ClassNotFoundException e){
}
return conn;
}
public void fecharConexao(){
try{
if(conn != null){
conn.close();
}
if(stmt != null){
stmt.close();
}
}
catch(Exception e){
}
}
public class CrudSql {
private Conectar conec = new Conectar();
private PreparedStatement stmt;
public void salvar(String sql){
try{
stmt = conec.conexao().prepareStatement(sql);
stmt.executeUpdate();
}
catch(Exception e){
}
finally{
conec.fecharConexao();
}
}
public class CadastrarNF {
private static String sql;
private static CrudSql crud = new CrudSql();
public static void cadastrarNF(NotaFiscal nf){
sql = "insert into notas(cfop,cliente,produto,natoperacao,descricao)value('"+nf.getCfop()+"','"+nf.getCliente()+"','"+nf.getProduto()+"','"+nf.getNaturezaOperacao()+"','"+nf.getDescricao()+"')";
crud.salvar(sql);
}
}
Aqui o servlet:
private void controladorNF(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
NotaFiscal nf = new NotaFiscal();
RequestDispatcher rd;
String cfop = request.getParameter("cfop");
String cliente = request.getParameter("cliente");
String produto = request.getParameter("produto");
String natoperacao = request.getParameter("natoperacao");
String descricao = request.getParameter("descricao");
Integer cfop1 = Integer.parseInt(cfop);
nf.setCfop(cfop1);
nf.setCliente(cliente);
nf.setProduto(produto);
nf.setNaturezaOperacao(natoperacao);
nf.setDescricao(descricao);
[b]CadastrarNF.cadastrarNF(nf);[/b]
if(request.getParameter("cliente").equals("")){
request.setAttribute("Cliente", "campo vazio");
rd = request.getRequestDispatcher("cadastrar.jsp");
}
else{
rd = request.getRequestDispatcher("salvo.jsp");
}
rd.forward(request, response);
}
Grato.