简体   繁体   English

Java SQl Server连接错误

[英]Java SQl Server connection error

I have written sql server connection part.When I run this code, i got this error. 我已经写了SQL Server连接部分。运行此代码时,出现此错误。

    Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
Error: No active Connection
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.sample.DB.getConnection(DB.java:30)
    at com.sample.DB.displayDbProperties(DB.java:49)
    at com.sample.DB.main(DB.java:85)

I have installed sql sever 2008 R2. 我已经安装了sql sever 2008 R2。 I serached google I couldn't find the solution.... 我向Google搜索,找不到解决方案。

This is my code 这是我的代码

    public class DB {
    private java.sql.Connection  con = null;
    private final String url = "jdbc:microsoft:sqlserver://";
    private final String serverName= "localhost";
    private final String portNumber = "1433";
    private final String databaseName= "XONTHOMass_User";
    private final String userName = "sa";
    private final String password = "xont@123";
    // Informs the driver to use server a side-cursor, 
    // which permits more than one active statement 
    // on a connection.
    private final String selectMethod = "cursor"; 



    private String getConnectionUrl(){
         return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
    }

    private java.sql.Connection getConnection(){
         try{
             System.out.println("========1========");
             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
             System.out.println("==== 2=====");
             con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
             if(con!=null) System.out.println("Connection Successful!");
         }catch(Exception e){
              e.printStackTrace();
              System.out.println("Error Trace in getConnection() : " + e.getMessage());
        }
         return con;
     }

    /*
         Display the driver properties, database details 
    */ 

    public void displayDbProperties(){
         java.sql.DatabaseMetaData dm = null;
         java.sql.ResultSet rs = null;
         try{
              con= this.getConnection();
              if(con!=null){
                   dm = con.getMetaData();
                   System.out.println("Driver Information");
                   System.out.println("\tDriver Name: "+ dm.getDriverName());
                   System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                   System.out.println("\nDatabase Information ");
                   System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                   System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                   System.out.println("Avalilable Catalogs ");
                   rs = dm.getCatalogs();
                   while(rs.next()){
                        System.out.println("\tcatalog: "+ rs.getString(1));
                   } 
                   rs.close();
                   rs = null;
                   closeConnection();
              }else System.out.println("Error: No active Connection");
         }catch(Exception e){
              e.printStackTrace();
         }
         dm=null;
    }     

    private void closeConnection(){
         try{
              if(con!=null)
                   con.close();
              con=null;
         }catch(Exception e){
              e.printStackTrace();
         }
    }
    public static void main(String[] args) throws Exception
      {
         DB myDbTest = new DB();
         myDbTest.displayDbProperties();
      }
}

Please help me..I did this application using the eclipse.I have put jar file also `sqljdbc4.jar'... 请帮助我..我使用eclipse完成了此应用程序。我还把jar文件也设置为sqljdbc4.jar ...

Please help me 请帮我

You receive the exception: 您收到异常:

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

That means your program cannot find the Driver and therefore not even try to connect to the database. 这意味着您的程序找不到驱动程序,因此甚至无法尝试连接到数据库。

Mircosoft has an article on How to Get Started with Microsoft JDBC : Mircosoft上有一篇有关如何开始使用Microsoft JDBC的文章

The Microsoft SQL Server 2000 driver for JDBC .jar files must be listed in your CLASSPATH variable. JDBC .jar文件的Microsoft SQL Server 2000驱动程序必须在CLASSPATH变量中列出。 The CLASSPATH variable is the search string that Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer.. CLASSPATH变量是Java虚拟机(JVM)用于在计算机上定位JDBC驱动程序的搜索字符串。

I think this is the driver that you need to use 我认为这是您需要使用的驱动程序

com.microsoft.sqlserver.jdbc.SQLServerDriver

not com.microsoft.jdbc.sqlserver.SQLServerDriver 不是com.microsoft.jdbc.sqlserver.SQLServerDriver

只需做一件事,即可转到http://www.java2s.com/Code/Jar/s/Downloadsqljdbcjar.htm并下载sqljdbc jar并将其在您的程序中使用。

As a complete guess, you have installed Express Edition (you should always mention the edition, not just the version) but you have not enabled TCP/IP support using the SQL Server Configuration Manager. 作为一个完整的猜测,您已经安装了Express Edition(您应始终提及该版本,而不仅仅是版本),但尚未使用SQL Server配置管理器启用TCP / IP支持。 By default, Express Edition does not have any network protocols enabled, so connecting over TCP/IP will not work unless you enable it first. 默认情况下,Express Edition没有启用任何网络协议,因此,除非先启用它,否则无法通过TCP / IP连接。

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

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