How to establish connection between JAVA & MS SQL Server ?
-
// For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQL {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://","", "");System.out.println("DATABASE NAME IS:" + connection.getMetaData().getDatabaseProductName()); Statement statement = connection.createStatement(); ResultSet resultSet = statement .executeQuery("select \* from dbo.try"); while (resultSet.next()) { System.out.println(resultSet.getString("Name")); } } catch (Exception e) { e.printStackTrace(); } }
}
-
// For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQL {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://","", "");System.out.println("DATABASE NAME IS:" + connection.getMetaData().getDatabaseProductName()); Statement statement = connection.createStatement(); ResultSet resultSet = statement .executeQuery("select \* from dbo.try"); while (resultSet.next()) { System.out.println(resultSet.getString("Name")); } } catch (Exception e) { e.printStackTrace(); } }
}
-
excuse me sir, ;) It's only article for information. :-D
-
excuse me sir, ;) It's only article for information. :-D
Very poor info. Lots of glitch : 1. What happen if Connection return as null. 2. What happen if ResultSet return as null. 3. Finally if all run good, then where is the code to close the resources.
Regards Shubhashish
-
excuse me sir, ;) It's only article for information. :-D
Well, this is not the place to post it. Please read http://www.codeproject.com/Messages/3137511/Forum-Guidelines-PLEASE-READ.aspx[^], and if you want to post an article then please read http://www.codeproject.com/info/Submit.aspx[^]. But do not submit a code dump as an article as it will most likely be rejected.
Veni, vidi, abiit domum
-
Well, this is not the place to post it. Please read http://www.codeproject.com/Messages/3137511/Forum-Guidelines-PLEASE-READ.aspx[^], and if you want to post an article then please read http://www.codeproject.com/info/Submit.aspx[^]. But do not submit a code dump as an article as it will most likely be rejected.
Veni, vidi, abiit domum
Ok, Thank you so much for assist.
-
Very poor info. Lots of glitch : 1. What happen if Connection return as null. 2. What happen if ResultSet return as null. 3. Finally if all run good, then where is the code to close the resources.
Regards Shubhashish
import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.ResultSet ; import java.sql.Statement ; import java.sql.SQLException; class JdbcTestMssql { public static void main (String args[]) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { System.err.println (e) ; System.exit (-1) ; } try { Connection connection = DriverManager.getConnection( "jdbc:sqlserver://192.168.100.68:1433;databaseName=master;user=ezadmin;password=ezadmin;"); String query = "SELECT * From sys.databases" ; Statement statement = connection.createStatement () ; ResultSet rs = statement.executeQuery (query) ; while ( rs.next () ) System.out.println ("MS-SQL Query result: " + rs.getString ("name")) ; connection.close () ; } catch (java.sql.SQLException e) { System.err.println (e) ; System.exit (-1) ; } } }
-
// For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQL {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://","", "");System.out.println("DATABASE NAME IS:" + connection.getMetaData().getDatabaseProductName()); Statement statement = connection.createStatement(); ResultSet resultSet = statement .executeQuery("select \* from dbo.try"); while (resultSet.next()) { System.out.println(resultSet.getString("Name")); } } catch (Exception e) { e.printStackTrace(); } }
}
Step 1: Connect Use the connection class to connect to SQL Database.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class SQLDatabaseConnection {
// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) {
String connectionUrl =
"jdbc:sqlserver://yourserver.database.windows.net:1433;"
+ "database=AdventureWorks;"
+ "user=yourusername@yourserver;"
+ "password=yourpassword;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "loginTimeout=30;";try (Connection connection = DriverManager.getConnection(connectionUrl);) { // Code here. } // Handle any errors that may have occurred. catch (SQLException e) { e.printStackTrace(); } }
}
Step 2: Execute a query
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class SQLDatabaseConnection {
// Connect to your database. // Replace server name, username, and password with your credentials public static void main(String\[\] args) { String connectionUrl = "jdbc:sqlserver://yourserver.database.windows.net:1433;" + "database=AdventureWorks;" + "user=yourusername@yourserver;" + "password=yourpassword;" + "encrypt=true;" + "trustServerCertificate=false;" + "loginTimeout=30;"; ResultSet resultSet = null; try (Connection connection = DriverManager.getConnection(connectionUrl); Statement statement = connection.createStatement();) { // Create and execute a SELECT SQL statement. String selectSql = "SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer"; resultSet = statement.executeQuery(selectSql); // Print results from select statement while (resultSet.next()) { System.out.println(resultSet.getString(2) + " " + resultSet.getString(3)); } } catch (SQLException e) { e.printStackTrace(); }