Developing SQL Server based Java Apps using NetBeans IDE - Creating a Microsoft SQL Server database connection using NetBeans IDE: the nucleus
(Page 6 of 6 )
After completing all the steps in previous section, we need to add Microsoft SQL Server related JDBC drivers to our application. This can be achieved with the following steps:
Within the “Projects” view, right-click on “Libraries” and go to “Add Jar/Folder” (Fig 03).

- Move to your JDBC driver path (in my case, it is at “C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClib”), and select all of the “.jar” files available (Fig 04).

- After selecting “.jar” files, click “open” and your “Projects” view should look something like the following (Fig 05).

Once you complete all the above steps, you can copy the following code in to your “btnConnectActionPerformed” event:
try {
//Load and register SQL Server driver
Class.forName("
com.microsoft.jdbc.sqlserver.SQLServerDriver");
//Establish a connection
Connection conn = DriverManager.getConnection("
jdbc:microsoft:sqlserver://serverjag:1433","sa","");
//Create a Statement object
Statement sql_stmt = conn.createStatement();
//Create a ResultSet object, execute the query and return a
// resultset
ResultSet rset = sql_stmt.executeQuery("SELECT * FROM
Northwind..orders ");
int i=0;
while (rset.next()){
i++;
}
//Close the ResultSet and Statement
rset.close();
sql_stmt.close();
//Close the database connection
conn.close();
this.txtMsg.setText(Integer.toString(i) + " rows found");
}
catch(Exception e) {
this.txtMsg.setText("Failed to connect; Please view Stack
Trace");
e.printStackTrace();
}
You can hit F5 to execute your solution and click on the button to show you the number of rows available in “Orders” table.
Any doubts, bugs, errors, suggestions, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |