0

I am trying to connect with Microsoft SQL Server Database called SimpleBase, which is on my computer with this code:

    static final String JDBCDriver = "com.mysql.jdbc.Driver";
    static final String dbURL = "jdbc:mysql://localhost/SimpleBase";
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

            Connection conn = null;
            Statement stmt = null;

            try{
                    Class.forName(JDBCDriver);
                    System.out.println("Connecting");
                    conn = DriverManager.getConnection(dbURL);
                    conn.close();
            }
            catch(SQLException se){
                    se.printStackTrace();
            }
            catch(Exception e){
                    e.printStackTrace();
            }
    }

and the output in Eclipse is something like this:

Connecting

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The  driver has not received any packets from the server.

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at PROKOM.Zadanie11.test.main(test.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 15 more

I suspect that I did something wrong. Can someone tell me what? If it's not exactly my fault, can someone tell what's wrong?

KonradL
  • 9
  • 3

1 Answers1

2

MySQL and SQL Server use different driver classes and connection URLs

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    
connection = DriverManager.getConnection
("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SimpleBase", "user", "");

SQL server uses a JDBC type 4 driver so it isnt necessary to load the JDBC driver explicitly

Reimeus
  • 158,255
  • 15
  • 216
  • 276