与java代码的数据库连接

时间:2013-11-23 15:51:36

标签: java

我使用以下代码使用sql和eclips将我的java代码与数据库连接 包APD;

import java.sql.*;

    public class APD {

        public static void main(String[] args) {

            // Create a variable for the connection string.
            String connectionUrl = "jdbc:sqlserver://user:1433;DatabaseName=Project;user=Safyan;Password=mehran";
                //"databaseName=HelloWorld;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 Person.Contact";
                        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) {}
            }
        }

}

但它会发生以下错误

 com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
 to the host user, port 1433 has failed. Error: "null. Verify the
 connection properties. Make sure that an instance of SQL Server is
 running on the host and accepting TCP/IP connections at the port. Make
 sure that TCP connections to the port are not blocked by a firewall.".
    at
 com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at
com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
    at
 com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
    at
 com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
    at
 com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at
 com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at
 com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(Unknown Source)     at
 java.sql.DriverManager.getConnection(Unknown Source)   at
 APD.APD.main(APD.java:20)

如何解决这些???

0 个答案:

没有答案