1

When I try something like this:
Class.forName("com.mysql.jdbc.Driver").newInstance(); DriverManager.getConnection("jdbc:mysql://192.168.2.116:3306/SocialFamilyTree");

I get an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Tried:

try{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    DriverManager.getConnection("jdbc:sqlserver://localhost:1433");

}catch(Exception e){
    System.out.println("Couldn't get database connection.");
    e.printStackTrace();
}

and got:

Couldn't get database connection.
Oct 06, 2012 11:15:37 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
Mike
  • 1,302
  • 6
  • 23
  • 43
  • do you have mysql started as well ? – Satya Oct 07 '12 at 02:51
  • @Satya can you be more specific? I have SQL Server Management Studio running. Is that what you mean? – Mike Oct 07 '12 at 02:55
  • Possible duplicate of http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – sakthisundar Oct 07 '12 at 02:58
  • @Satya So I did what you said. I got the right driver now, when I try to connect I get: **com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port."** – Mike Oct 07 '12 at 03:22
  • I got the answer: **open SQL Server Configuration Manager -> Protocols for SQL SQLEXPRESS, select Properties of TCP/IP. In the tab IP Addresses, set the TCPPort in section IPAll to 1433.** But now I get: **com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'SOSCOMP'.** I don't understand. There is no user for the DB, so I used my windows user name and I don't have a password to this user. **What's wrong?** – Mike Oct 07 '12 at 03:45

1 Answers1

0

if you are trying to connect to sql server your code is wrong , as in that you are trying to connect to mysql .

Use jtds or sql server driver to connect Microsoft SQL Server 2005 JDBC Driver

DRIVER CLASS: com.microsoft.sqlserver.jdbc.SQLServerDriver 

DRIVER LOCATION: Specify the location on your machine of the Microsoft SQL Server driver. See your Microsoft SQL Server driver documentation for more details. Certain versions of the Microsoft SQL Server driver require more than one jar file for the driver location. In this case, simply separate each file location by a semi-colon.

JDBC URL FORMAT: jdbc:sqlserver://<server_name>:<port> 

The default port for Microsoft SQL Server is 1433. Usually, if the default port is being used by the database server, the : value of the JDBC url can be omitted.

Examples:

jdbc:sqlserver://neptune.acme.com:1433 

jdbc:sqlserver://127.0.0.1:1433 
Satya
  • 8,693
  • 5
  • 34
  • 55