Source code: importjava. SQL. *; publicclassConnectionDemo {staticStringdrivercom. microsoft. sqlserver. jdbc. SQLServerDriver; usage: sqlserver: localhost: 1433; DatabaseNameMyDB; publicstaticvoidmain (S
Source code: import java. SQL. *; public class ConnectionDemo {static String driver = com. microsoft. sqlserver. jdbc. SQLServerDriver; private static final String url = jdbc: sqlserver: // localhost: 1433; DatabaseName = MyDB; public static void main (S
Source code:
Import java. SQL .*;
Public class ConnectionDemo {
Static String driver = "com. microsoft. sqlserver. jdbc. SQLServerDriver ";
Private static final String url = "jdbc: sqlserver: // localhost: 1433; DatabaseName = MyDB ";
Public static void main (String [] args ){
Connection con = null;
Try {
Class. forName (driver); // load the driver
Con = DriverManager. getConnection (url, "sa", "sa ");
} Catch (Exception e ){
E. printStackTrace ();
}
System. out. println (con );
Try {
Con. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
An error is reported after running:
Com. microsoft. sqlserver. jdbc. SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. error: Connection refused: connect. please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
At com. microsoft. sqlserver. jdbc. SQLServerException. makeFromDriverError (SQLServerException. java: 130)
At com. microsoft. sqlserver. jdbc. SQLServerConnection. connectHelper (SQLServerConnection. java: 1195)
At com. microsoft. sqlserver. jdbc. SQLServerConnection. loginWithoutFailover (SQLServerConnection. java: 1054)
At com. microsoft. sqlserver. jdbc. SQLServerConnection. connect (SQLServerConnection. java: 758)
At com. microsoft. sqlserver. jdbc. SQLServerDriver. connect (SQLServerDriver. java: 842)
At java. SQL. DriverManager. getConnection (DriverManager. java: 582)
At java. SQL. DriverManager. getConnection (DriverManager. java: 185)
At ch6.practise. ConnectionDemo. main (ConnectionDemo. java: 14)
Java. lang. NullPointerException
At ch6.practise. ConnectionDemo. main (ConnectionDemo. java: 22)
Null
Let's take a look at what's going on. After one night and one morning, you can't even connect, alas ......