1

I try to know if I can connect Java to SQL Server using these codes :

package pkgtry;
import java.sql.*;
public class NewMain {

    public static void main(String[] args) {
    String  connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";


            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;

                try {

                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        con = DriverManager.getConnection(connectionUrl);


                        String SQL = "";
                        stmt = con.createStatement();
                        rs = stmt.executeQuery(SQL);


                        while (rs.next()) {

                        }
                }


            catch (Exception e) {
                e.printStackTrace();
            }

            finally {
                if (rs != null) try { rs.close(); } catch(Exception e) {}
                    if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                    if (con != null) try { con.close(); } catch(Exception e) {}
            }
    }
}

but on my end it show an error and says :

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host (local), port 1433 has failed. Error: "null. 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.".

What does it mean? How to fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Crystal Maiden
  • 372
  • 1
  • 7
  • 23
  • check this - http://stackoverflow.com/questions/11820799/com-microsoft-sqlserver-jdbc-sqlserverexception-the-tcp-ip-connection-to-the-ho – Ankit Apr 02 '13 at 05:29
  • for some reason I can't open my SQL Server Configuration Manager it popped out an error and says: 'Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can manage SQL Server 2005 and later servers with SQL Server Configuration Manager. Invalid class[0x80041010]' how to fix it? – Crystal Maiden Apr 02 '13 at 05:34
  • try to “run as administrator” – Ankit Apr 02 '13 at 05:39
  • I tried that too, same error, I dunno how to fix it. – Crystal Maiden Apr 02 '13 at 05:40
  • if I can't open the SQL Server Configuration Manager does it mean that I can't fix my error in Netbeans? – Crystal Maiden Apr 02 '13 at 05:40
  • Not sure if this only the issue, try replacing **(local)** with **localhost** and look at this link to fix above sql-manager issue http://blogs.msdn.com/b/spike/archive/2012/03/19/quot-sql-server-configuration-manager-quot-gives-quot-invalid-class-0x80041010-when-starting.aspx. – Ankit Apr 02 '13 at 05:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27376/discussion-between-crystal-maiden-and-ay89) – Crystal Maiden Apr 02 '13 at 06:00

3 Answers3

1

It means JDBC is unable to connect to the database server. It couldn't find the host (local), did you mean to put localhost here instead of (local)?

String  connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • OK I've tried to changed it to localhost, but it shows me different error, 'com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'.' how to fix this? – Crystal Maiden Apr 02 '13 at 05:37
  • You need to supply proper login credential / speak to your database administrator to get the proper login credential. Please read MS SQL manual – gerrytan Apr 02 '13 at 05:42
  • sorry if I sounds like a newbie here, I really don't know how to do this one because I used to make a program in VB and it's like I'm starting from the scratch again. This java thing is very different to vb. could you give me some link to study how to connect my program to MS SQL? – Crystal Maiden Apr 02 '13 at 05:44
0

Do the following:

1)Make sure that the server is running on your computer . That is, its listening to incoming connection requests .

To do the above, use an IDE (I prefer netbeans....its very easy to use) and try creating a connection to the database. You will have to input username, password, databasename, the port no. on which the server is running and so on.

If you get a successful connection, your server is running fine.

If not, make sure you are using the correct driver, you have mentioned the right port number, right database name and so on.

2)Copy the connection string generated and replace it in the connection URL. I feel you should have a "localhost" instead of "(local)" in your connection string. But to be sure, just copy the connection string generated by your IDE.

3)Try connecting again and see if it works. It mostly should.

Nikhil
  • 1,279
  • 2
  • 23
  • 43
  • 'com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'.' , this is the error occured how to fix it? your kinda right about 'localhost' instead of 'local' – Crystal Maiden Apr 02 '13 at 05:39
  • Do you have a user SA ? Does that user have the privileges to connect to your database? If yes, are you providing the correct pass word? try this: 1)Connect to a pre existing database..eg. test. 2)Use credentials username: root Password: blank (don't type anything) 3)For 1 and 2 , you will need to find out the testdatabases created for your server and their login credentials. Google this. It may vary from server to server – Nikhil Apr 02 '13 at 05:56
0
<%
    String  connectionUrl="jdbc:sqlserver://localhost:1433;DatabaseName=databaseNames;";
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e) {
        out.println("<p>Driver not found:" + e + e.getMessage() + "</p>" );
    }

    try {
        Connection conn = DriverManager.getConnection (connectionUrl, "sa", "yourPassword");
Syon
  • 7,205
  • 5
  • 36
  • 40