In java, when you want to connect MS SQL Server Database and want to show Table’s record in java, then you have to remember one thing, you have to require suitable database driver for java.
So, Here SKOTechLearn will describe the process steps by step for SQL Server Database Connection String in Java with Query.
Connect Database And Access All Tables and Records with NetBeans
For This process you have to follow the steps given following.
So, For SQL Server Connection in Java, you can use “sqljdbc4.jar” file for driver.
You can find it from many sites, and here are two sites where you can find this driver easily.
First Site
Second Site
This jar file will be added on your project’s “Libraries” like above given image.
How to Set Default Main Class in Java Using Netbeans
When you run this code, the following output will be shown:
Simply copy above code and modify servername, databasename, username and password then run your project.
Simple Way to Display Image in Java
So, if you want to display all records, you have to use “Select Query Statement”. And write code in NetBeans in following way.
When you run this code, this will show you following output.
So, these are the steps for establishing connection from database server or SQL Server Database Connection String in Java Netbeans with Query.
What Settings for Auto Resize Control in Java Using Netbeans?
So, Here SKOTechLearn will describe the process steps by step for SQL Server Database Connection String in Java with Query.
Connect Database And Access All Tables and Records with NetBeans
For This process you have to follow the steps given following.
- Download “sqljdbc4.jar” file
- Attach “sqljdbc4.jar” File in Project’s Package
- Import SQL Class and Other Related Class
- SQL Server Connection Code in Java with Example
- Use SQL Server Query in Java NetBeans
Step(1). Download “sqljdbc4.jar” file :
If you want to use database connection on your projects, you have to know that you have to required suitable driver for database connection in java.So, For SQL Server Connection in Java, you can use “sqljdbc4.jar” file for driver.
You can find it from many sites, and here are two sites where you can find this driver easily.
First Site
Second Site
Step(2). Attach “sqljdbc4.jar” File in Project’s Package :
After that you have to create your project and when you expand your project in NetBeans, you will find there are many Package Folders available. 1.
Find “Libraries” Package Folder from you project. 2.
Right click on “Libraries” Package Folder. And Select third option “Add JAR/Folder…”. 3.
This option will show you “Add JAR/Folder” Window. 4.
Browse your downloaded “sqljdbc4.jar” file.This jar file will be added on your project’s “Libraries” like above given image.
How to Set Default Main Class in Java Using Netbeans
Step(3). Import SQL Class and Other Related Class :
Now, before start connection string, you have to import some class as described below.//java.sql.Connection is use for establishing connection from database import java.sql.Connection; //java.sql.DriverManager is used for calling driver with connection string import java.sql.DriverManager; //javax.swing.JOptionPane for Display Message import javax.swing.JOptionPane;
Step(4). SQL Server Connection Code in Java with Example :
Now let’s try to connect database from server in java Netbeans, We will write code inside jButton’s ActionPerformed event.private void SQLConn_BtnActionPerformed(java.awt.event.ActionEvent evt) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection SQlCnn = DriverManager.getConnection("jdbc:sqlserver://XXX.XXX.X.XX; databaseName= MySQLDB; user= MYSQLUser; password= MySQLPasswrd"); JOptionPane.showMessageDialog(null, "SQL Server Connection Successfully."); SQlCnn.close(); }catch (Exception CnnErr){ JOptionPane.showMessageDialog(null, "SQL Server Connection Problem"); } }
When you run this code, the following output will be shown:
Simply copy above code and modify servername, databasename, username and password then run your project.
Simple Way to Display Image in Java
Step(5). Use SQL Server Query in Java NetBeans :
Suppose we have a table “MySQLDBTable” and there is two column “MyTBL_ID” and “MyUsrName”. And this table contains some records like bellow:So, if you want to display all records, you have to use “Select Query Statement”. And write code in NetBeans in following way.
Use of Select Query in Java
private void SQLQuery_BtnActionPerformed(java.awt.event.ActionEvent evt) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection SQlCnn = DriverManager.getConnection("jdbc:sqlserver://XXX.XXX.X.XX; databaseName= MySQLDB; user= MYSQLUser; password= MySQLPasswrd"); Statement MyDBstmt = SQlCnn.createStatement(); ResultSet MyRSet = MyDBstmt.executeQuery("Select * From MySQLDBTable"); while( MyRSet.next()) { System.out.println(MyRSet.getString("MyTBL_ID") + " | " + MyRSet.getString("MyUsrName")); } MyRSet.close(); MyDBstmt.close(); SQlCnn.close(); }catch (Exception DBErr){ JOptionPane.showMessageDialog(null, "SQL Server Connection Problem"); } }
When you run this code, this will show you following output.
So, these are the steps for establishing connection from database server or SQL Server Database Connection String in Java Netbeans with Query.
What Settings for Auto Resize Control in Java Using Netbeans?
0 comments: