问题是连接Java驱动程序以连接到SQL Server。

huangapple go评论86阅读模式
英文:

Problem connecting java driver for connecting to SQL Server

问题

以下是翻译好的部分:

我在尝试将我的Java应用程序连接到SQL Server数据库时遇到了错误,这是我的代码。

try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn = DriverManager.getConnection(CONN, USER, PASS);
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from articulos");
    while (rs.next()) {
        int id = rs.getInt(1);
        String descripcion = rs.getString(2);
        float precio = rs.getFloat(3);
        int rubro = rs.getInt(4);

        Articulo a = new Articulo(id, descripcion, precio);
        lista.add(a);
    }

} catch (SQLException | ClassNotFoundException ex) {
    Logger.getLogger(GestorBD.class.getName()).log(Level.SEVERE, null, ex);
}

但我一直在遇到这个错误:

严重: null
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:333)
	at gestor.GestorBD.obtenerArticulos(GestorBD.java:19)
	at repasobd.RepasoBD.main(RepasoBD.java:14)

这显示了我的驱动程序已连接。

问题是连接Java驱动程序以连接到SQL Server。

我已更新了我的CLASSPATH,但仍然遇到相同的错误。

问题是连接Java驱动程序以连接到SQL Server。

英文:

I'm getting an error while trying to connect my java application to a SQL Server database, here's my code.

 try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection(CONN,USER,PASS);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from articulos");
        while(rs.next()) {
            int id = rs.getInt(1);
            String descripcion = rs.getString(2);
            float precio = rs.getFloat(3);
            int rubro = rs.getInt(4);
            
            Articulo a = new Articulo(id,descripcion,precio);
            lista.add(a);
        }
        
    } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(GestorBD.class.getName()).log(Level.SEVERE, null, ex);
    }

and i keep having this error

SEVERE: null
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:333)
at gestor.GestorBD.obtenerArticulos(GestorBD.java:19)
at repasobd.RepasoBD.main(RepasoBD.java:14)

This shows my driver connected.

问题是连接Java驱动程序以连接到SQL Server。

I've update my CLASSPATH and still getting the same error.

问题是连接Java驱动程序以连接到SQL Server。

答案1

得分: 2

你的类路径中需要包含com.microsoft.sqlserver.jdbc.SQLServerDriver类,该类可以在Microsoft JDBC驱动程序中找到。
所以问题不在连接上,而是你缺少了一个驱动程序库。

英文:

You need to have com.microsoft.sqlserver.jdbc.SQLServerDriver class in your classpath which can be found in Microsoft JDBC Driver.
So the issue isn't in the connection. It's that you are missing a driver lib.

答案2

得分: 1

不要使用系统范围的 CLASSPATH 变量 - 它已经被废弃很久了。

您的 NetBeans 截图显示了内置 NetBeans 数据库查询工具可用的驱动程序。这不会更改项目的类路径。

如果您正在使用基于 Ant 的项目,请右键单击项目,选择属性,然后在“库”部分添加驱动程序的 jar 文件。

如果这是一个 Maven 项目,您需要通过 Maven 将 Microsoft JDBC 驱动程序的依赖项添加进来。

英文:

Do not use the system wide CLASSPATH variable - it has been deprecated for ages.

Your NetBeans screeshot show the drivers available for the built-in NetBeans database query tool. This does not change the classpath of your project.

If you are using an Ant based project, right click on the project, choose properties, then add the jar file of the driver in the "Libraries" section.

If it's a Maven project, you need to add the dependency to the Microsoft JDBC driver, through Maven

huangapple
  • 本文由 发表于 2020年9月1日 03:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63677092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定