JDBC驱动程序连接问题(sun.jdbc.odbc.JdbcOdbcDriver)

时间:2013-07-15 20:20:52

标签: java sql-server dsn jdbc-odbc

我遇到了与Java和SQL 2008 Express连接的一些问题。我正在使用sun.jdbc.odbc.JdbcOdbcDriver驱动程序进行连接,并通过管理工具创建了我的dsn,这是我正在使用的代码:

import java.sql.*;

public class JdbcFirstTry 
{
     public static void main(String args[]) throws SQLException
     {

         try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                Connection con = DriverManager.getConnection("jdbc:odbc:movie_archive_DSN");
                System.out.print("you made connection");
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
    }
}

这是我得到的错误:

  

找不到数据源名称且未指定默认驱动程序

有人可以提供有关如何解决此错误的建议吗?此外,tcp / ip已启用,端口设置为1433。

我也尝试过这种方式,但一直有错误的时间:

 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

      String connectionUrl = "jdbc:sqlserver://WALSER:1433;databaseName=MYSQLDATABASE;user=walser/kyle;password=brenna1020;";

      Connection con = DriverManager.getConnection(connectionUrl);

,错误是:

  

与主机WALSER,端口1433的TCP / IP连接失败。错误:   “连接被拒绝:连接。验证连接属性。制作   确保在主机上运行SQL Server实例   接受端口的TCP / IP连接。确保TCP   防火墙不会阻止与端口的连接。“。

3 个答案:

答案 0 :(得分:0)

要解决没有默认驱动程序,您需要指定数据库特定驱动程序的类型,例如

  1. oracle.jdbc.driver.OracleDriver for Oracle
  2. com;sybase.jdbc3.jdbc.SybDataSource用于sybase
  3. 其次请在连接电话中添加用户名和密码。

答案 1 :(得分:0)

如你所说,你的协议(TCP)是禁用的,所以让我们用一些代码激活它:) 源自codeproject

--step 1: creating a login (mandatory)
create login login_to_system_after_injection with password='Thank$SQL4Registry@ccess';
GO
--step 2: enabling both windows/SQL Authentication mode
/*some server specific configurations are not stored in system (SQL)*/
--set the value to 1 for disabling the SQL Authentication Mode after . . .
exec xp_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2;
--step 3:getting the server instance name
declare @spath nvarchar(256);
--SQL SERVER V100 path, use SQL9 for V90
exec master..xp_regread N'HKEY_LOCAL_MACHINE',
                 N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL' ,N'SQL10',@spath output,no_output 
--step 4:preparing registry path
declare @insRegPath nvarchar(1024)=N'Software\Microsoft\Microsoft SQL Server\' + 
                                      @spath + '\MSSQLServer\SuperSocketNetLib\Tcp';
--step 5:enabling tcp protocol
exec xp_regwrite N'HKEY_LOCAL_MACHINE', @insRegPath, N'Enabled', REG_DWORD, 1 --generally tries to enable all addresses. NOT Recommended
--step 6:enabling remote access
--EXEC sys.sp_configure N'remote access', 1
GO
RECONFIGURE WITH OVERRIDE --reconfigure is required!
GO
--step 7:a system restart is required in order to enabling remote access.
--step 7.1:shutting down the server
shutdown
--After this command you need to start the server implicitly yourself.
--or just configure the Agent in order to start the server at any shutdown or failure

运行上面的代码后你需要一个服务器reastart,你也需要sysadmin规则:)

如果上述代码不起作用,请转到开始 - >所有节目 - > Microsoft SQL Server 2008 - >配置工具 - > SQL Server配置管理器

然后从左窗格中选择“SQL Server网络配置”,并选择所需的实例,然后从右窗格中启用TCP / IP,然后重新启动服务器

然后在Java应用程序中,您需要更改类名和连接字符串 在下载库之后,将其添加到类路径中,现在您的代码就像这样

public class JdbcFirstTry 
    {
      public static void main(String args[]) throws SQLException
     {

         try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

                Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DatabaseName;integratedSecurity=true;");
                System.out.print("you made connection");
          }
          catch (Exception e)
          {

             e.printStackTrace();
         }

     }

    }

上面的integratedSecurity = true表示使用windows帐户进行连接,但很简单,你可以添加用户名和密码,连接字符串为user=MyUserName;password=*****

最后,请尝试告诉我有关结果的老兄,希望你得到它:)

答案 2 :(得分:0)

如果您使用的是Windows 7 64位转到

C:\的Windows \ Syswow64资料 并搜索odbcad32.exe

从那里你得到ODBC数据源管理员 并创建dsn

相关问题