0
votes

i try to connect to my local sqlserver express 2008 R2.

when i specify server name as 127.0.0.1 , like

String url = "jdbc:sqlserver://127.0.0.1;databaseName=mydatabase;";

i can connect.

but when i write servername\named instance as server name like

String url = "jdbc:sqlserver://machinename//instancename;databaseName=mydatabase;";

then i couldnt connect to server.

configurations: i set tcp/ip connection enabled. i set to tcp dynamic ports as 1433 in ipall section. named instance -> enabled , it doesnt affect connection.

package demojdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Iterator;

/**
 *
 * @author ttn
 */
public class DemoJdbc {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        baglanti();
    }

    public static void baglanti() {

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("driver yuklendi");            
        } catch (ClassNotFoundException e) {
            System.out.println("Drive bulunmadi"+e);
        }
        Connection con = null;

        String url = "jdbc:sqlserver://127.0.0.1;databaseName=mydatabase;";
        try {
            con= DriverManager.getConnection(url, "xxx","xxx");
            System.out.println("Veritabanına baglantı kuruldu");            
            Statement stmt=con.createStatement();


        } catch (Exception e) {
            System.out.println("Hata Var"+e);
        }

    }

}
1
try change 127.0.0.1 to MachineName\SQLExpressvasin1987
no i couldnt connect with MachineName\SQLExpressengtuncay

1 Answers

0
votes

i installed the sqlserver by setting named instance and sqlserver 2008 R2 express does not provide sql browser. maybe this is real problem or named pipes may be change, i dont know. i succeeded connection in this way.

when i write only machinename , then i can connect like that

String url = "jdbc:sqlserver://machinename;databaseName=mydatabase;";

by the way i can connect server via toad sql server, by naming server name as machinename\instancename.

i could not find the real solution, but i solved

this can be useful for developers.

https://serverfault.com/questions/371832/why-cannot-connect-to-sql-server-by-the-full-server-name-with-instance-name