使用sqljdbc4.jar从Eclipse连接到MSSQL服务器的JDBC连接 [英] JDBC connection from Eclipse to MSSQL server using sqljdbc4.jar

查看:788
本文介绍了使用sqljdbc4.jar从Eclipse连接到MSSQL服务器的JDBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Eclipse Web项目中的sqljdbc4与MS SQL Server 2014建立一个jdbc连接,
没有任何运气。

I am trying to make a jdbc connection to MS SQL server 2014 using sqljdbc4 from an Eclipse Web project, without any luck whatsoever.

这里是到目前为止我已经尝试过:

Here is what I have tried so far:


  1. 在Web项目之外创建一个测试类,添加jar来构建路径并尝试建立连接 - 成功

  2. 将jar放在项目的WEB-INF / lib下,添加jar来构建路径,并添加一个Web应用程序库,并尝试建立连接 - 失败

  3. 在中央Tomcat lib下放置jar并尝试建立连接 - 失败

大多数论坛有成功的用户在上面的列表中编号(2)。我刚刚开始使用JDBC,花了一段时间甚至到了这个阶段。
但不幸的是,无法进一步。现在我停留了近7个小时,令人沮丧的事情是每次常规java项目都可以工作。
为什么这样,当IDE中的任何类型的项目需要jar在其类路径?

Most of the forums have users who have succeeded by doing number (2) in the list above. I am just starting out with JDBC and it took a while to even get to this stage. But unfortunately, couldn't get any further. I am stuck at this point for close to 7 hours now and the frustrating thing is it works every time from a regular java project. Why is that so, when any kind of project in an IDE requires the jar to be in its classpath?

不知道这将是多少帮助,但是这里是我试图建立连接的代码。
它总是导致一个SQLException:在'try'之后的第一行找不到适合的jdbc:sqlserver的适当的驱动程序。

Not sure how much help this will be of, but here is the code that I had come up with that tries to establish the connection. And it always leads to an SQLException : No suitable driver found for jdbc:sqlserver on the first line after 'try'.

public class SQLConnector {
    private static final String DB_SERVER = "jdbc:sqlserver://SAI;"
                + "DatabaseName=LibraryManagementSystem";
    private static final String DB_USER="sa";
    private static final String DB_PASS="abc732XYZ";

    public static Connection getDatabaseConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(DB_SERVER, DB_USER, DB_PASS);
            if(connection != null) {
                System.out.println("Connection successful");
            }
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}

请仔细检查和帮助。

这是我的Web应用程序的项目树

推荐答案

我最终使用Tomcat连接池,它的工作。这通过在META-INF下的Web内容下创建一个具有以下内容的Context.xml文件:

I ended up using Tomcat Connection pooling and it worked. This is done by creating a Context.xml file under META-INF under Web content with the following content.

    <Context>
        <!-- Specify a JDBC datasource -->
        <Resource name="jdbc/LibraryManagementSystem" auth="Container"
        type="javax.sql.DataSource" username="sa" password="abc732XYZ"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://SAI:1433;DatabaseName=LibraryManagementSystem"
        maxActive="10" maxIdle="4" />
   </Context>

通过使用DataSource和Initial Context,适当地更改连接器类以反映连接池。另外,建议使用简单的jdbc连接池。

And suitably changing the connector class to reflect Connection pooling by using DataSource and Initial Context. Also, connection pooling is recommended over using plain jdbc.

public class SQLConnector 
{
    private static final String DB_NAME = "jdbc/LibraryManagementSystem";
    private static Connection connection;

    public static Connection getDatabaseConnection() {

        try {
            Context initContext  = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            DataSource dataSource = (DataSource)envContext.lookup(DB_NAME);
            connection = dataSource.getConnection();
        } 
        catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}

这篇关于使用sqljdbc4.jar从Eclipse连接到MSSQL服务器的JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆