Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
Could anyone explain how to connect to a SQL Server instance from Java(Eclipse or NetBeans will do)?
I'ved tried the following:
-->set up the SQL Server driver
-->added a connection string like jdbc:sqlserver://127.0.0.1:1433;databaseName=NORTHWND;integratedSecurity=true;
or
jdbc:sqlserver://localhost:1433;databaseName=NORTHWND;integratedSecurity=true;

SSMS opens up perfectly the DB, the TCP/IP is set to 1433 (both tcp dynamic ports and tcp port are set to 1433 ). However I have 2 instances of Sql Server (SQLEXPRESS and MSSQLSERVERENT).
How should I set the connection string?

PS:Firewall is turned off.
Posted
Comments
tamash_ionut 24-Mar-11 13:47pm    
The problem was that the SQL driver could not accept integrated security mode. After specifying a password and a username it worked.

Try using:
jdbc:sqlserver://127.0.0.1:1433;instanceName=SQLEXPRESS;databaseName...

[Addition}
Since this is driver specific you can also try:
...//127.0.0.1:1433\SQLEXPRESS;databaseName...
 
Share this answer
 
v2
Comments
tamash_ionut 23-Mar-11 18:38pm    
Tried. Doesn't work.
Wendelius 23-Mar-11 18:41pm    
What kind of error do you get?
tamash_ionut 24-Mar-11 13:39pm    
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.
Wendelius 24-Mar-11 13:56pm    
Okay so the problem actually lies on the security. Have a look at this conversation: http://social.msdn.microsoft.com/forums/en-US/sqldataaccess/thread/1c1a1313-057d-46d4-9ca7-82fc4f23a150/

Also if you like you can use SQL Server identified logins and modify your connection string like:
jdbc:sqlserver://localhost:1433;databaseName=NORTHWND;user=someuser;password=verysecret

In order to do that you first have to create proper logins and users in SQL Server
To connect to SQL Server from a Java application, you need to use the JDBC API. The JDBC API provides classes and methods that connect to the database, load the appropriate driver, send SQL queries, retrieve results etc.

HOW TO CONNECT TO THE DATABASE
A 'Connection' object represents a connection with a database.
To establish the connection, use the method 'DriverManager.getConnection'. This method takes a string containing a URL which represents the database we are trying to connect to.
Below is the sample code for establishing a connection:
private String DATABASE_URL = "jdbc:odbc:embedded_sql_app";
// establish connection to database
Connection connection = DriverManager.getConnection( DATABASE_URL,"sa","123" );

Detailed discussion about the Database URL and how to create it can be found in the resource provided at the end of this post.

QUERYING THE DATABASE
The JDBC API provides three interfaces for sending SQL statements to the database, and corresponding methods in the 'Connection' interface create instances of them.
1. Statement - created by the 'Connection.createStatement' methods. A 'Statement' object is used for sending SQL statements with no parameters.
2. PreparedStatement - created by the 'Connection.prepareStatement methods'. A 'PreparedStatement' object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
3. CallableStatement - created by the 'Connection.prepareCall' methods.

'CallableStatement' objects are used to execute SQL stored procedures from Java database applications.

RETRIEVING THE RESULT
A 'ResultSet' is a Java object that contains the results of executing a SQL query. The data stored in a 'ResultSet' object is retrieved through a set of get methods that allows access to the various columns of the current row. The 'ResultSet.next' method is used to move to the next row of the 'ResultSet', making it the current row.
The following code fragment executes a query that returns a collection of rows, with column 'a' as an 'int', column 'b' as a 'String', and column 'c' as a 'float':
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
// retrieve and print the values for the current row
int i = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
System.out.println("ROW = " + i + " " + s + " " + f);
}


This is just a brief introduction on how to interact with a database from Java. For more details on the items discussed above as well as information on passing parameters, executing stored procedures etc. please refer to the following resource: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html )
Here, you will also find information on how to interact with a database programmatically; i.e. without using SQL.

Hope you find this useful.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900