Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Connecting MS SQL Server thru Java for UNIX environment.

Hi all,

I am supposed to connect (retrive data from) MS Sql server 2000's database, from Unix (solaris). I think connecting to it thru java would be the best option. If thru Java, then what is the procedure to  be followed? Any ideas? Urgent help is requested.

Thanks in advance.
abbas
0
contactabbasa
Asked:
contactabbasa
2 Solutions
 
mili910Commented:
Create a dsn pointing to the MSSQL Databse

Connection conn = DriverManager.getConnection(url, userName, password);

/*
url = "jdbc:jdc:DSNName"
userName ="sa"
password=""
*/
Statemenet stmt = conn.createStatement();
stmt.executeQuery("SELECT * FROM EMP");
0
 
coolsumuCommented:
The SQL Server 2000 Driver for JDBC is supported on the following operating systems:
- Microsoft Windows® XP
- Microsoft Windows 2000 with Service Pack 2 or higher
- AIX
- HP-UX
- Solaris
- Linux

But you can download the JDBC drivers at:
http://www.microsoft.com/sql/default.asp
The download includes a detailed description about the URL which you need to use to connect to your server.
Install the driver as admin.
A sample code :
-----------------------
//////
import java.sql.*;

public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "pubs";
private final String userName = "xxxxx";
private final String password = "xxxxx";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";

// Constructor
public Connect(){}

private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}

private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}

/*
Display the driver properties, database details
*/

public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriver Name: "+ dm.getDriverName());
System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
System.out.println("\nDatabase Information ");
System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
System.out.println("Avalilable Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcatalog: "+ rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}

private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
}
}
0
 
CleanupPingCommented:
contactabbasa:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now