简体   繁体   中英

JDBC driver connection problems (sun.jdbc.odbc.JdbcOdbcDriver)

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:

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.

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.".

To resolve no default driver, you need to specify type of database specific driver, eg

  1. oracle.jdbc.driver.OracleDriver for Oracle
  2. com;sybase.jdbc3.jdbc.SybDataSource for sybase

Secondly please add username and password in connection call.

as you said, your protocol (TCP) is disable, so let activate it with some code :) source from 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

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

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

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=*****

finally, have a try and inform me dude about the result, hope you get it run :)

If you are using Windows 7 64 bit Go to

C:\\Windows\\SysWOW64 and search for odbcad32.exe

from there you get ODBC data source administrator and create the dsn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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