简体   繁体   English

无法将JDBC与SQL Server连接

[英]Not able to connect JDBC with SQL Server

 import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class JdbcSQLServerConnection {

        public static void main(String[] args) throws ClassNotFoundException {



            Connection conn = null;
            try {


              String url = "jdbc:sqlserver://microsoft\\SQLEXPRESS;databaseName=crud";
              String userName = "sa";
              String password = "pwd";
              System.out.println("Connected2");
              Class.forName("com.sqlserver.jdbc.SQLServerDriver");
              conn = DriverManager.getConnection(url, userName, password);


                System.out.println("Connected");
                if (conn != null) {
                    DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
                    System.out.println("Driver name: " + dm.getDriverName());
                    System.out.println("Driver version: " + dm.getDriverVersion());
                    System.out.println("Product name: " + dm.getDatabaseProductName());
                    System.out.println("Product version: " + dm.getDatabaseProductVersion());
                }

            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (conn != null && !conn.isClosed()) {
                        conn.close();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

output:- 输出:-

Connected2 Exception in thread "main"
java.lang.ClassNotFoundException: com.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:28)
I have included sqljdbc4.jar in eclipse library but still it is not working on my system please help me in this solution`enter code here. 我已经在eclipse库中包含了sqljdbc4.jar,但是仍然无法在我的系统上正常工作,请在此解决方案中帮助我,在此处输入代码。

Obviously the class you try to load ( com.sqlserver.jdbc.SQLServerDriver ) is not found. 显然, com.sqlserver.jdbc.SQLServerDriver尝试加载的类( com.sqlserver.jdbc.SQLServerDriver )。

According to Microsoft Docs "Using the JDBC Driver" you have to load the JDBC-Driver by calling Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 根据Microsoft Docs“使用JDBC驱动程序”,您必须通过调用Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");来加载JDBC驱动程序Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); -- note the microsoft between com and sqlserver -注意comsqlserver之间的microsoft

If this does not help, you might want to 如果这样做没有帮助,您可能想要

  1. Compile your code and run it on the command line, manually adding the JDBC driver JAR to the classpath ( -cp ) to get rid of any pitfall in Eclipse. 编译代码并在命令行上运行它,将JDBC驱动程序JAR手动添加到类路径( -cp )中,以消除Eclipse中的任何陷阱。

  2. Rename the sqljdbc4.jar to sqljdbc4.zip , unzip it and check whether the class you try to load really exists in there... sqljdbc4.jar重命名为sqljdbc4.zip ,解压缩并检查您尝试加载的类是否确实存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM