Developing SQL Server based Java Apps using NetBeans IDE - How to establish a Microsoft SQL Server database connection using JDBC
(Page 4 of 6 )
You can skip to the next section if you are using NetBeans IDE. This section is only for the Java developers who don’t make use of any IDE.
Make sure that you complete all the steps in the previous section before proceeding with this section. Once you have the correct JDBC driver installed and have configured CLASSPATH (and PATH) correctly, establishing a connection from your Java programs to your SQL Server database is pretty easy.
Regardless of whether you're trying to connect to Microsoft SQL Server, Oracle, Sybase (or any other JDBC data source), establishing a connection to any RDBMS database with Java JDBC is a simple two-step process:
- Load the JDBC driver.
- Establish the connection.
Let us go through the following program to connect to Microsoft SQL Server database.
import java.sql.*;
class JdbcSample1 {
public static void main (String[] args) {
try {
Class.forName("
com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = " jdbc:microsoft:sqlserver://serverjag:1433";
Connection conn = DriverManager.getConnection(url,"sa","");
} catch (Exception e) {
System.err.println("Could not connect! ");
e.printStackTrace();
}
}
}
From the above code, the following are the only two important statements I used to connect to Microsoft SQL Server.
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = " jdbc:microsoft:sqlserver://serverjag:1433";
Connection conn = DriverManager.getConnection(url,"sa","");
You need to modify “serverjag” with your server name, which has Microsoft SQL Server installed. Similarly, you also need to modify the port (default Microsoft SQL Server port is 1433), user id and password to get connected successfully.
Next: Creating a Microsoft SQL Server database connection using NetBeans IDE: Form Design >>
More Java Articles
More By Jagadish Chaterjee