SQL Server and JSP connection error

0

I'm trying to connect to SQL Server using JSP. But the connection is not made.

Connection Class:

package DAO;

import java.sql.*;
public class Conexao {




public static Connection conectar() {


try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager
.getConnection(
"jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=minhadb;namedPipe=false",
"user", "123456");
System.out.println("Conexao ok");
return con;


} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;


} catch (SQLException e) {


System.out.println(e.getErrorCode());
System.out.println(e.getMessage());
e.printStackTrace();
return null;
} catch (Exception e) {


e.printStackTrace();
return null;
}


}


}

Index.jsp

<%@ page language="java" import="java.sql.*, DAO.Conexao" %>
<html>
<head>
<title>
Aprendendo JSP
</title>
</head>
<body>
<%


Connection con = Conexao.conectar();


if( con != null){
out.println("Sucesso");


}
%> 


</body>
</html>

Console Message:

java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DAO.Conexao.conectar(Conexao.java:10)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:74)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
    
asked by anonymous 08.11.2014 / 18:53

1 answer

1

Try this, using the microsoft driver

String usuario = "seuUsuario";
String senha = "suaSenha";
String url = "jdbc:sqlserver://seuHost:1433;databaseName=seuBanco" +
                       ";user=" + usuario + ";password=" + senha + ";";
try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conexao = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
    // Erro caso o driver JDBC não foi instalado
    e.printStackTrace();
} catch (SQLException e) {
    // Erro caso haja problemas para se conectar ao banco de dados
    e.printStackTrace();
}

Or if you want to try again with jtds

String url = "jdbc:jtds:sqlserver://seuHost:1433/seuBanco";
String usuario = "seuUsuario";
String senha = "suaSenha";
try {
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
    Connection conexao = DriverManager.getConnection(url, usuario, senha);
} catch (ClassNotFoundException e) {
    // Erro caso o driver JDBC não foi instalado
    e.printStackTrace();
} catch (SQLException e) {
    // Erro caso haja problemas para se conectar ao banco de dados
    e.printStackTrace();
}
    
08.11.2014 / 19:20