java JDBC驱动连接问题(sun.jdbc.odbc.JdbcOdbcDriver)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17663167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC driver connection problems (sun.jdbc.odbc.JdbcOdbcDriver)
提问by kyle5385
I'm having some issues with the connection with Java and SQL 2008 Express. I'm using sun.jdbc.odbc.JdbcOdbcDriver
driver for the connections and have created my dsn through the admin tools and this is the code I'm using:
我在与 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();
}
}
}
This is the error I'm getting:
这是我得到的错误:
Data source name not found and no default driver specified
未找到数据源名称且未指定默认驱动程序
Can anyone offer advice on how to fix this error? Also tcp/ip is enabled and port set to 1433.
任何人都可以提供有关如何解决此错误的建议吗?还启用了 tcp/ip 并将端口设置为 1433。
I have also tried this way as well but kept getting a time out error:
我也尝试过这种方式,但一直出现超时错误:
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);
and the error is:
错误是:
The TCP/IP connection to the host WALSER, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
到主机 WALSER 的 TCP/IP 连接,端口 1433 失败。错误:“连接被拒绝:连接。验证连接属性。确保 SQL Server 实例正在主机上运行并在端口接受 TCP/IP 连接。确保到端口的 TCP 连接没有被防火墙阻止.”。
回答by Sachin Thapa
To resolve no default driver, you need to specify type of database specific driver, e.g.
要解决没有默认驱动程序,您需要指定数据库特定驱动程序的类型,例如
oracle.jdbc.driver.OracleDriver
for Oraclecom;sybase.jdbc3.jdbc.SybDataSource
for sybase
oracle.jdbc.driver.OracleDriver
甲骨文com;sybase.jdbc3.jdbc.SybDataSource
用于 sybase
Secondly please add username and password in connection call.
其次请在连接调用中添加用户名和密码。
回答by Sachin Thapa
as you said, your protocol (TCP) is disable, so let activate it with some code :) source from codeproject
正如你所说,你的协议(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
you need a server reastart after running the above code, also you need sysadmin rule too :)
运行上述代码后,您需要重新启动服务器,还需要系统管理员规则:)
if the above code doesn't work so go to start -> all programs -> Microsoft SQL server 2008 -> configuration tools -> SQL server configuration manager
如果上述代码不起作用,请转到开始 -> 所有程序 -> Microsoft SQL Server 2008 -> 配置工具 -> SQL 服务器配置管理器
then select the "SQL Server network configuration" from the left pane, and select the desired instance, then enable the TCP/IP from the right pane, and restart the server
然后在左侧窗格中选择“SQL Server 网络配置”,并选择所需的实例,然后在右侧窗格中启用 TCP/IP,然后重新启动服务器
then in Java application, you need to change the class name and connection string just after downloading the library, add it to the class path and now your code will be like this
然后在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();
}
}
}
in above integratedSecurity=true means uses the windows account to connect, but simple you would add the username and password with connection string as user=MyUserName;password=*****
在上面的integratedSecurity=true 表示使用windows 帐户连接,但很简单,您可以将用户名和密码与连接字符串添加为 user=MyUserName;password=*****
finally, have a try and inform me dude about the result, hope you get it run :)
最后,试一试并告诉我结果,希望你能运行:)
回答by swanand keskar
If you are using Windows 7 64 bit Go to
如果您使用的是 Windows 7 64 位 转到
C:\Windows\SysWOW64 and search for odbcad32.exe
C:\Windows\SysWOW64 并搜索 odbcad32.exe
from there you get ODBC data source administrator and create the dsn
从那里你得到 ODBC 数据源管理员并创建 dsn