Java Web - Insert + SQL Server

0 respostas
B

Boa Noite

Sou novo em java estou aprendendo na raça, e  fiz uma rotina de CRUD(insert)

Criei Um HTM chamado um JSP

Porem quando clico no botao cadastrar ele nao insere no banco

Tentativa 1

public void CadastrarLivro(LivroDTO objLivroDTO) throws ClassNotFoundException{
String sql = "insert into Livro(nome_livro) values (?);
    con = new ConexaoDAO().conexaoBD();
    System.out.println(sql);
    try {
        pstm = con.prepareStatement(sql);
        pstm.setString(1, objLivroDTO.getNome_livro());
        pstm.execute();
        pstm.close();

quando deixo sem aspas na ? nao apresenta erro no browser
quando deixo com aspas
public void CadastrarLivro(LivroDTO objLivroDTO) throws ClassNotFoundException{

String sql = "insert into Livro(nome_livro) values ('?')";
    con = new ConexaoDAO().conexaoBD();
    System.out.println(sql);
    try {
        pstm = con.prepareStatement(sql);
        pstm.setString(1, objLivroDTO.getNome_livro());
        pstm.execute();
        pstm.close();

HTTP Status 500 - Internal Server Error


type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

root cause

java.lang.RuntimeException: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

root cause

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.

apresenta este erro.

frmLivroVIEW

TODO supply a title
<br>
        <label> SISTEMA CRUD </label><br><br>
        <label>NOME LIVRO: </label><br>
        <input type="text" name="nome">
        <button type="submit">CADASTRAR</button>
        
    </form>
</body>

inserirLivro

<%–
Document : inserirLivro
Created on : Oct 26, 2022, 8:28:47 PM
Author : Administrator
–%>

<%<a class="mention" href="/u/page">@page</a> import=“br.com.DAO.LivroDAO”%>

<%<a class="mention" href="/u/page">@page</a> import=“br.com.DTO.LivroDTO”%>

<%<a class="mention" href="/u/page">@page</a> contentType=“text/html pageEncoding=UTF-8%>
JSP Page <%
try {
        LivroDTO objLivroDTO = new LivroDTO();
       objLivroDTO.setNome_livro(request.getParameter("nome"));
       
       LivroDAO objLivroDAO = new LivroDAO();
       objLivroDAO.CadastrarLivro(objLivroDTO);
                           
       
     } catch (Exception e) {
     //   throw new RuntimeException(e);
       }
    %>   
    
</body>
try {

// Class.forName(“com.microsoft.sqlserver.jdbc.SqlServerDriver”);

Class.forName(com.microsoft.sqlserver.jdbc);

String url = (jdbc:jtds:sqlserver://localhost:1433/sistemabiblioteca,user= sa, password = Brasil@2025”);

con = DriverManager.getConnection(url);

<a href="//System.out.println" rel="noopener nofollow ugc">//System.out.println</a>(“conexao ok”);
} catch (SQLException e) {
        //  throw new RuntimeException(e);
    

    }
    return con;
}

se alguem puder me ajudar agradeço

Criado 28 de outubro de 2022
Respostas 0
Participantes 1