Πρόβλημα στη σύνδεση MS SQL SERVER - Netbeans

Συζητήσεις για γλώσσες προγραμματισμού και θέματα σχετικά με προγραμματισμό.
Post Reply
User avatar
PASCAL
Wow! Terabyte level
Wow! Terabyte level
Posts: 3587
Joined: Wed Nov 23, 2005 10:58 pm
Academic status: Alumnus/a
Gender:

Πρόβλημα στη σύνδεση MS SQL SERVER - Netbeans

Post by PASCAL » Fri Mar 11, 2011 5:36 pm

Είμαι σε Netbeans και έχω τον ακόλουθο κώδικα σε Java:

Code: Select all

package testsql;

import java.sql.*;

public class Main {

    public static void main(String[] args) {
        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://localhost:1433;database=Northwind;integratedSecurity=true";
        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(connectionUrl);
            // Create and execute an SQL statement that returns some data.
            String SQL = "SELECT TOP 10 * FROM dbo.customers";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                System.out.println(rs.getString(4) + " " + rs.getString(6));
            }
        }//Handle any errors that may have occurred.
        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) {}
            }//if
        }//finally
    }//main
}//Main
Έχω εγκαταστήσει MS SQL Server, Northwind, JDBC Driver και το jarάκι σωστά αλλά μου πετάει το εξής σφάλμα όταν πάω να συνδεθώ με την Northwind:

Code: Select all

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."
Κλείνω το firewall αλλά πάλι τα ίδια.
Ξέρει κανείς τι φταίει;
User avatar
statakos
Kilobyte level
Kilobyte level
Posts: 482
Joined: Wed Sep 17, 2008 3:07 am
Academic status: Alumnus/a
Gender:

Re: Πρόβλημα στη σύνδεση MS SQL SERVER - Netbeans

Post by statakos » Fri Mar 11, 2011 6:06 pm

Κάτι μου θυμίζει από πέρσι στις βάσεις αυτό...

Check here. :-)
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
Post Reply

Return to “Προγραμματισμός”