Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Kevin Jenkins   on Jun 24 In Java Category.

  
Question Answered By: Samuel Costa   on Jun 24

As for as my concern there is no direct way of passing values  to a
PreparedStatement using STRING NAMES, the only options exists is the
way of using parameter  indexes to pass  values. For tht also u have to
remember the order of indexes...

But if u still wanted to do, you can use Double dimension Arrays /
Hashtable & use a method to retrieve the corresponding index...

Go through my example, it may help u to understand wht im saying..

import java.sql.*;
import java.util.*;

public class PSDemo
{
static Hashtable params = new Hashtable();

static
{
params.put("EMPNO",new Integer(1));
params.put("EMPNAME",new Integer(2));
params.put("EMPSAL",new Integer(3));
}

public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:odbc:JTESTING","admin","admin");

PreparedStatement pstSave = con.prepareStatement("INSERT INTO
EMP(EMPNO,EMPNAME,EMPSAL) VALUES(?,?,?)");
pstSave.setInt(getIndex("EMPNO"),Integer.parseInt(args[0]));
pstSave.setString(getIndex("EMPNAME"),args[1]);
pstSave.setDouble(getIndex("EMPSAL"),Double.parseDouble(args[2]));
pstSave.execute();
pstSave.close();
con.close();
System.out.println("Records Saved Successfully!!");
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}

public static int getIndex(String strHeader)
{
return Integer.parseInt(params.get(strHeader).toString());
}
}

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Problem in calling stored procedure Or get search suggestion and latest updates.


Tagged: