2013-07-15 182 views
0

我遇到了一些与java eclipse和sql 2008 express的连接问题。我是新的这是日食,需要一些帮助。即时通讯使用sun.jdbc.odbc.JdbcOdbcDriver驱动程序的连接,并通过管理工具创建了我的dsn,这是使用的编码: import java.sql。*;jdbc驱动程序连接问题(sun.jdbc.odbc.JdbcOdbcDriver)

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(); 
     } 

    } 

    } 

这个,这是错误即时得到:

Data source name not found and no default driver specified 
可以

就如何解决这个错误的人提供建议?也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); 

和错误是: 的TCP/IP连接主机WALSER,端口1433发生故障。错误:“连接被拒绝:connect。验证连接属性确保SQL Server的一个实例正在主机上运行并且在端口上接受TCP/IP连接请确保与端口的TCP连接没有被防火墙阻止“。

+0

很明显,因为它说,尝试检查名称是在OS和代码 – 2013-07-15 20:23:03

+0

所有名称拼写正确相等。还有什么可能吗? – kyle5385

+0

你能直接通过操作系统ping数据源吗?核实。顺便说一句,为什么你不直接连接到SQL Server? ODBC消耗你的性能好友,只需启用[SQL身份验证](http://www.codeproject.com/Articles/616114/SQL-Server-T-SQL-Tips-Tricks#xp_regeditwrite),下载驱动程序,并拥有一个直接连接:) – 2013-07-15 20:38:02

回答

0

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

  1. oracle.jdbc.driver.OracleDriver甲骨文
  2. com;sybase.jdbc3.jdbc.SybDataSource为Sybase

其次请连接呼叫添加用户名和密码。

0

如你所说,你的协议(TCP)为禁用,因此让一些代码激活它:) 源从codeproject

--step 1: creating a login (mandatory) 
create login login_to_system_after_injection with password='[email protected]'; 
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上面的代码后,还需要系统管理员规则太:)

如果上面的代码不工作,所以去开始 - >所有程序 - >微软SQL服务器2008 - >配置工具 - > SQL Server配置管理

然后选择“SQL服务器网络配置“,然后选择所需的实例,然后从右窗格启用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帐户进行连接,但简单,你想补充与连接字符串的用户名和密码as user=MyUserName;password=*****

终于,有一个tr y和通知我关于花花公子的结果,希望你得到它运行:)

0

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

C:\ WINDOWS \ SysWOW64中 和搜索odbcad32.exe的

从那里你ODBC数据源管理员 和创建DSN

相关问题