A quick copy paste code for connecting with SQL Server and executing a select statement or a Stored Procedure.
1. Imports :
2. Code :
try
{
DriverManager.registerDriver (new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection conespacxo = null;
String connectionUrl = new String();
ConnectionUrl="jdbc:sqlserver://?.?.?.?:1433;databaseName=??;user=prabhjot;password=lamba;";
conespacxo = DriverManager.getConnection(connectionUrl);
conespacxo.setAutoCommit(false);
/*
// Executing a select statement
String selectString = "select distinct name from StudentDatabase";
Statement stmt = conespacxo.createStatement();
ResultSet rs1 = stmt.executeQuery(selectString);
while (rs1.next())
{
String prodName = rs1.getString("name");
System.out.println(prodName);
}
rs1.close();
stmt.close();
conespacxo.close();
*/
// Executing a Stored Procedure
CallableStatement call_stmt = conespacxo.prepareCall("{ call myStoredProcedure (?,?,?,?) }");
call_stmt.setString(1, "2011-06-01"); // From Date
call_stmt.setString(2, "2011-06-20"); // To Date
call_stmt.setString(3, "SharePrice"); // What to Get
call_stmt.setString(4, "Google"); // Company Name
ResultSet rs = call_stmt.executeQuery();
while (rs.next())
{
String date = rs.getString(1);
String value = rs.getString(2);
System.out.println("Generated Date: " + date);
System.out.println("Generated Value: " + value);
}
rs.close();
call_stmt.close();
conespacxo.close();
}
catch(Exception e)
{
e.printStackTrace();
}
1. Imports :
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
You will need sqljdbc4.jar in your classpath.
2. Code :
try
{
DriverManager.registerDriver (new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection conespacxo = null;
String connectionUrl = new String();
ConnectionUrl="jdbc:sqlserver://?.?.?.?:1433;databaseName=??;user=prabhjot;password=lamba;";
conespacxo = DriverManager.getConnection(connectionUrl);
conespacxo.setAutoCommit(false);
/*
// Executing a select statement
String selectString = "select distinct name from StudentDatabase";
Statement stmt = conespacxo.createStatement();
ResultSet rs1 = stmt.executeQuery(selectString);
while (rs1.next())
{
String prodName = rs1.getString("name");
System.out.println(prodName);
}
rs1.close();
stmt.close();
conespacxo.close();
*/
// Executing a Stored Procedure
CallableStatement call_stmt = conespacxo.prepareCall("{ call myStoredProcedure (?,?,?,?) }");
call_stmt.setString(1, "2011-06-01"); // From Date
call_stmt.setString(2, "2011-06-20"); // To Date
call_stmt.setString(3, "SharePrice"); // What to Get
call_stmt.setString(4, "Google"); // Company Name
ResultSet rs = call_stmt.executeQuery();
while (rs.next())
{
String date = rs.getString(1);
String value = rs.getString(2);
System.out.println("Generated Date: " + date);
System.out.println("Generated Value: " + value);
}
rs.close();
call_stmt.close();
conespacxo.close();
}
catch(Exception e)
{
e.printStackTrace();
}
No comments:
Post a Comment