Mock tests, Interview questions, Tutorials and Tech news
 
 

Archive

Author Archive

Character data type conversion when using SQL Server JDBC drivers

May 3rd, 2011 sadhna No comments

We have a existing old portal in asp with Sql server and now trying to get new features in new site developing in grails,java & same sql server.

Sometime ago, I was working on a login page so that the same old portal credentials could be used.   One issue I had to address was that the web app captures the password in clear text but sends an encoded password to the database (where the password is also stored in our encoded format).

Now I was trying to implement a simple logic for encryption; add 20 to each character’s int value and convert it to char and store the encrypted password.

For example the java code-

StringBuffer t = new StringBuffer(“”);

String test = “sviadha”;for ( int i = 0; i < test.length(); ++i ) {

char c = test.charAt( i );

int k = (int) c ;

int kk = k +20;

char ss = (char)kk;

t = t.append(ss);

}

System.out.println(“original password =”+ test);

System.out.println(“encrypted password ==”+ t);

Now when I tried to match the stored password using new UI it was failed all the time.

No matter what I tried, in the Java-tier I could not get past the fact that by the time the password was received in the SQL-tier, there was an encoded password mismatch.   So I worked around the problem, but passing the clear text password to the database and the stored procedure did the encoding and finally the validation.

After digging it found the  real problem; it turns out that this is a MS SQL Server JDBC driver configuration.  By default MS JDBC driver is sent to pass all strings as NVARCHAR, not VARCHAR.  This forced a Unicode conversion on the way to the database.

That’s why the same encoding logic was working fine for asp-sql server based old site but not on java based new site.

Here’s the magic to change this behavior so VARCHAR are sent and received…

MyDataSource

jdbc:jtds:sqlserver://mac1.temp.test:1434;DatabaseName=MyDatabase;tds=8.0;lastupdatecount=false; sendStringParametersAsUnicode=false

net.sourceforge.jtds.jdbc.Driver

. . .
Read more…

Beginning-Compare Java Vs Microsoft( ASP.NET)

November 1st, 2010 sadhna 2 comments

When I first started working in project of ASP.NET , I compared everything with my existing java world and summarized the following-

Solution and project files

Solutions and project files are VS.Net-specific thing with is similar to eclipse web/java project.

DLL

Nearest equivalent to java’s jar files are DLL(assemblies) Assemblies provide versioning, security, resource packaging etc. (but no compression like jar files).
you do not need to create them explicitly ;A build creates the compiler output which is either an exe or a dll file&  They are stored in a subdicrtory.

.VB

.vb is in place of .class files and After i do a build i see a solution file, how to generate a dll?

ASP/ASPX

If I compare the above thing as  java/groovy developer then aspx/asp is in place of jsp/jsf/gsp  pages

I would write details about each with similarity and differences as I go deeper into it while working

Categories: Programming / tutorials Tags: ,

Check code error on Visual Studio without explicitly compiling..NO

October 25th, 2010 sadhna No comments

I am using Visual Studio 2003. my experience with Eclipse is more .I have gotten accustomed to the way Eclipse handles underlining errors (in java source). whenever a syntactical error in my code in eclipse, it will usually be underlined & i fix it, the underline will disappear almost immediately. very friendly Coding!

In Visual Studio2003 however, you will not know anything until build and run the project on browser. really annoying!

Visual Studio 2008 with service pack 1 has something for this. But not in 2003 :(

If you are using 2005 or above then can use error list window. it will give you at least some relief(the below part taken from msdn help)-

The Error List helps you speed application development. In the Error List window, you can: Read more…

Transition of a Java Programmer to ASP.Net

October 19th, 2010 sadhna 11 comments

I am a experienced java/j2ee developer , a big fan of open source technologies .in past 10 years I did work in almost everything related to java world(core java, applet, servlet, java bean, jsf, jsp, spring, struts, myfaces, grails, gsp etc etc..and server admin jboss, weblogic, tomcat, jetty, bea etc etc ) . Mostly used IDE eclipse , myeclipse, jbuilder and netbeans.

Recently my company decided that they will stop work on existing projects which are on new technologies and would support legacy products.  The future ….Microsoft technologies

future

future

I was not very happy to have agreed to management demand but sometimes you have to accept the way life is.

I love Java

I love Java

I have been moved to a new project and started working in asp.net .  Now this is a transition from java to Microsoft…. Change of mindset …open source to purchased software world and much…more changes..

I wrote following points and issues which I had faced during  setting up  until start working on the real code….hope it will be helpful to all java/j2ee developer who are moving to Asp.Net/ C#. Read more…

Output parameter not allowed as argument list prevents use of RPC.

April 20th, 2010 sadhna 2 comments

While calling a stored procedure from my java class; I was getting the following  error-

java.sql.SQLException: “Output parameter not allowed as argument list prevents use of RPC.”

After doing a little research; I found that When calling a stored procedure that has output parameters, the driver has to call the procedure using a remote procedure call (RPC). Stored procedures should be invoked using the special JDBC call escape syntax.

For example, {call stored_Proc1(?,?,?,?)}.

In this case the driver will be able to use an RPC successfully as all the parameters are represented by parameter markers (?). If however parameters are supplied as a mixture of parameter markers and literals,

for example {call stored_Proc1(?,’ ‘,?,0)}

Then the driver is unable to use an RPC and therefore cannot return output parameters. In these circumstances the driver raises the above exception and execution fails.

It is possible to use mixed parameter lists to call stored procedures that do not have output parameters. In this case the driver will substitute the parameters locally and use a normal “execute procedure” SQL call; however, this mode of execution is less efficient than an RPC.

Categories: Programming / tutorials Tags: , ,

Jboss 4 Server Configuration changes for SQL Server 2008

April 5th, 2010 sadhna 2 comments

Last week our Sql Server was upgraded to 2008 version. And the upgrade , I found following exception being thrown by when deploying our project.

com.microsoft.sqlserver.jdbc.SQLServerException: The server version is not supported. The target server must be SQL Server 2000 or later.

I upgraded the jdbc driver and replaced to sqljdbc.jar (version 2.0) and stored in lib folder of my project.
But the error was same as under— Read more…

JNDI lookup on JBoss

March 11th, 2010 sadhna No comments

There is no standard way as per Java EE specs for  JNDI naming conventions, hence most of the application servers have their own way of JNDI naming.

On specifying a Datasource’s JNDI name as ‘myDatasource’, JBoss binds as ‘java:myDatasource’. So if one want to deploy the application on Jboss application servers then can use the simplest way is here. The steps for adding and using jndi Datasource to JBOSS 4.x is as under-

  • Copy the jdbc driver to JBOSS_HOME/server/default/lib.
  • Create the jndi xml file for datasource – example ds.xml Read more…

Categories: Programming / tutorials Tags: , ,

Groovy Tutorial – Calling a stored procedure

December 2nd, 2009 sadhna No comments

Calling a stored procedure from a Sql server MySQL database or any other database in groovy is simple. First we need a datasource which spring could provide for us. Do the necessary datasource mapping in the resources.xml ( spring folder) in your grails folder &/Or datasources.groovy.

In service, use the datasource to call a store procedure as below example

import groovy.sql.Sql
class TestService{
def dataSource // using the datasource we define in the spring's resources.xml

<strong>/**Calling Procedure with in-parameters only- use sql.query**/</strong>
def abcList = { params -&gt;
Sql sql = new Sql(dataSource)
def clientkey = (SecurityUtils.getSecureClientKey(params) !=
null)?SecurityUtils.getSecureClientKey(params):1
//calling proc that returns resultset
sql.query("{call svr_RetrieveClientPreferences(?)}",[clientkey]) { rs -&gt;
if (rs != null) {
int i = 0
while (rs.next()) {
/ *all functionality ...... */
}
}
}
}

<strong>/**Calling Procedure with in &amp; Out parameters - use sql.call **/</strong>
def abcCalc = { params -&gt;
Sql sql = new Sql(dataSource)
guid = params.guide
userKey = params.user
//calling proc that returns out parameters
sql.call("{call gdx_ReadSessionData(?,?,?,?,?,?)}",
[guid, userKey, Sql.out(Sql.INTEGER.type),
Sql.INTEGER, Sql.VARCHAR, Sql.INTEGER])
{ date,userKeyCode,msgCode,retCode -&gt; /* The out parameters will be returned in the same order it passed to procedure */
userKey = userKeyCode
println(" return code" + retCode)
}
}
}

Note-
in-Out Param can be passed as Sql.out(Sql.<DATATYPE>.type) OR Sql.<DATATYPE> .
See more for DATATYPE here – http://groovy.codehaus.org/api/groovy/sql/Sql.html
LIMITATION -
Some issues with groovy sql; where we want to use out parameters & resultset both as a return of proc execution.

In this case we have to use the jdbc way (callable stmt) where stored procedure that return ResultSet and OUT parameter.

Categories: Programming / tutorials Tags:
Get Adobe Flash playerPlugin by wpburn.com wordpress themes