我对Java和SQL2008Express的连接有一些问题。我正在为连接使用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连接失败。错误:“连接拒绝:连接。请验证连接属性。请确保Server实例在主机上运行,并在端口接受TCP/IP连接。确保到端口的TCP连接不会被防火墙阻塞”。
发布于 2013-07-15 20:59:46
若要解决任何默认驱动程序,您需要指定特定于数据库的驱动程序类型。
oracle.jdbc.driver.OracleDriver
com;sybase.jdbc3.jdbc.SybDataSource
其次,请在连接呼叫中添加用户名和密码。
发布于 2013-07-16 20:07:58
正如您所说的,您的协议(TCP)是禁用的,所以让我们用一些代码(代码:)从代码工程激活它。
--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配置管理器。
然后从左窗格中选择“”,并选择所需的实例,然后从右窗格中启用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=*****
。
最后,试着告诉我这个结果,希望你能运行:)
发布于 2016-05-10 12:23:50
如果您正在使用Windows 7 64位,请转到
C:\Windows\SysWOW64 64和搜索odbcad32.exe
从那里可以获得ODBC数据源管理员并创建dsn。
https://stackoverflow.com/questions/17663167
复制相似问题