Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Couldn't retreive a String from the result set

 
elizabeth thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to execute a simple query and output the result to swing components. it retrieves correctly for the numeric columns in the query. However, when use rs.getString(cloumn) statement, it gives me error "java.sql.SQLException: [Oracle][ODBC]Invalid column number <2>." I tried replacing <2> and <3> with the actual column name, but no luck.
Please help me
CODE] void FindName_actionPerformed(ActionEvent e) {
try
{
if (!fields.WorkID.getText().equals(""))
{
Statement stmt = connection.createStatement();
String query = "SELECT Work_ID, Work_Requested, Work_Requested_By, Organization_Element_ID " +
"FROM Work Where Work_ID = " + fields.WorkID.getText();
output.append ("\n Sending query: " + query + "\n");
ResultSet rs = stmt.executeQuery(query);
display (rs);
output.append ("\n Query successful");
}
else
{
fields.WorkID.setText ("Enter name here then press Find");
}
}
catch (SQLException sqle)
{
sqle.printStackTrace();
output.append(sqle.toString());
}
}
public void display (ResultSet rs)
{
try
{
rs.next();
int work_id = rs.getInt("Work_ID");
int org_id = rs.getInt("Organization_Element_ID");
if (work_id != 0)
{
fields.WorkID.setText (String.valueOf(work_id));
fields.OrganizationID.setText(String.valueOf(org_id));
fields.WorkRequested.setText(rs.getString(2));
fields.WorkRequestedBy.setText(rs.getString(3));
}
else
output.append ("\n No record found");
}
catch (SQLException sqle)
{
sqle.printStackTrace();
output.append(sqle.toString());
}
}
}[ [/CODE]
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is not necessarily in your code( well actually it is, but I'll explain ). You've decided to use the Oracle ODBC-JDBC drivers, which are to say the least "finicky"...otherwise known as "crappy". They can get the job done, but have exponentially more limitations than that of its type 4 rival the "thin" drivers. One of the limitations of most ODBC drivers is that you must retrieve column values in order that they appear in your query. So in your case, you should reword your query to start with
"SELECT Work_ID, Organization_Element_ID, Work_Requested, Work_Requested_By..."
because it matches the order in which you retrieve the values in your code. This is not a limitation of the "thin" drivers ( or OCI for that matter ) and your code would work fine with them.
Can't see anything else that is "Exception worthy" in your code,
Jamie
 
elizabeth thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jamie.
It solved my problem.
Could you please give me an example on how to use an Oracle thin driver? I tried the follwing, but didn't work. Gives me an error : "CLassNotFoundException"
String Driver = "oracle.jdbc.driver.OracleDriver";
String URL = "jdbc racle:thin@hostID ort:service";
try {
Class.forName (Driver);
connection = DriverManager.getConnection (URL, "scott", "tiger");
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic