I am going to share my experience that how to use SQL Server Data Services (SDS) in java using JDBC.
To access this service you first need java runtime i.e. java 1.6 and JDBC for SQL Server. You can get the driver from here.
Here is some code snip
try
{
// First, format the connection string note that the server name here again is, “myserver”. The database name (my user database name) is mydatabase.
String connectionUrl = "jdbc:sqlserver://myserver.data.dev.mscds.com;" +
"database=databasename;encrypt=true;user=userid;password=password";
// Load SQL Server Driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Get the connection using DriverManager.
Connection sqlConn = DriverManager.getConnection(connectionUrl);
// If there is no connection created then exit from the application.
if (sqlConn == null)
{
System.out.println("Unable to obtain connection. exiting");
System.exit(1);
}
// Begin by creating the table we'll use.
Statement sqlStmt = sqlConn.createStatement();
String insertSql =
"INSERT INTO dbo.tbl_Person(FirstName, LastName) VALUES ('Rob, 'Currier'); " +
"INSERT INTO dbo.tbl_Person(FirstName, LastName) VALUES ('Shane', 'Smith');";
// Then, insert some rows into the table that we just created.
sqlStmt.execute(insertSql);
// Now, select all of the, "Jeff' data from the table and print them out.
ResultSet results = sqlStmt.executeQuery("select FirstName, LastName from tbl_Person where FirstName=’Rob’");
while (results.next())
{
System.out.println("FirstName: " + results.getString("FirstName") + " LastName: " + results.getString("LastName"));
}
// Close the ResultSet up.
results.close();
// Finally drop the table and close the conneciton.
sqlStmt.execute("drop table tbl_Person");
sqlConn.close();
} catch (SQLException ex)
{
System.out.println("Error: Unable to execute query ");
ex.printStackTrace();
} catch(ClassNotFoundException ex)
{
System.out.println("Error: Unable to load JDBC Driver!");
}
Wednesday, July 1, 2009
Subscribe to:
Posts (Atom)