SQL Exception - Invalid Handle
843842Jul 8 2008 — edited Jul 8 2008I am writing a program for a order company using java beans. I have everything working except when i try to add a new item into the database it throws a exception saying: invlaid handle. The code for that bean file is given below..any help will be really appreciated. The error is under setNewItem function on code: results = statement.executeQuery(select).
package stockBeans;
import java.sql.*;
public class StockAccess
{
private Connection connection;
private Statement statement;
private ResultSet results;
public StockAccess() throws ClassNotFoundException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfEx)
{
throw new ClassNotFoundException(
"Unable to locate JDBC driver!");
}
}
public String getStockLevel(String code) throws SQLException
{
int stockLevel;
String returnValue="";
boolean match=false;
connectAndCreateStatement();
results = statement.executeQuery("SELECT * FROM StockItems");
while((results.next())&&(match==false))
{
if(code.equalsIgnoreCase(results.getString(1)))
{
match=true;
stockLevel = results.getInt(3);
returnValue = Integer.toString(stockLevel);
}
}
if(!match)
{
returnValue="error";
}
disconnectFromDb();
return returnValue;
}
public void setNewItem(String code, String desc, int currentLvl,int reorderLvl,Float price)
throws SQLException
{
boolean found;
connectAndCreateStatement();
found = findMatch(code);
if(found==false)
{
try
{
String select = "SELECT * FROM StockItems";
**results = statement.executeQuery(select);**
String insert = "INSERT INTO StockItems VALUES (" + "'" + code
+ "'" + "," + "'" + desc + "'" + "," +
+ currentLvl + "," reorderLvl "," price ")";
int result = statement.executeUpdate(insert);
System.out.print("Congratulation. Match Data Saved");
}
catch(SQLException sqlEx)
{
System.out.println("* Error Inserting Data! *" + sqlEx);
}
}
else
{
System.out.println("\n Code Already Entered. Try Again \n");
}
}
public void getStock()
throws SQLException
{
connectAndCreateStatement();
System.out.println();
System.out.println("Code"+"\t\t"+" Description"
+"\t"+"Current Level"+"\t"+"Reorder Level"+"\t"+"Cost");
results = statement.executeQuery("SELECT * FROM StockItems");
while (results.next())
{
System.out.println(results.getString(1)
+"\t\t"+results.getString(2)
+"\t\t"+results.getInt(3)
+"\t\t"+ results.getInt(4)
+"\t\t"+results.getFloat(5));
}
}
public boolean findMatch(String code)
throws SQLException
{
boolean found=false;
connectAndCreateStatement();
try
{
results = statement.executeQuery("SELECT * FROM StockItems");
while(results.next()&& (found==false))
{
if(code.equalsIgnoreCase(results.getString(1)))
{
found=true;
}
else
{
found=false;
}
}
}
catch(SQLException sqlEx)
{
System.out.println("Unable to retrieve data");
}
disconnectFromDb();
return found;
}
private void connectAndCreateStatement() throws SQLException
{
try
{
connection = DriverManager.getConnection(
"jdbc:odbc:StockData","","");
//*** Change DSN name if yours is different! ***
}
catch (SQLException sqlEx)
{
throw new SQLException("Unable to connect to database!");
}
try
{
statement = connection.createStatement();
}
catch (SQLException sqlEx)
{
throw new SQLException("Unable to create SQL statement!");
}
}
private void disconnectFromDb() throws SQLException
{
try
{
connection.close();
}
catch (SQLException sqlEx)
{
throw new SQLException(
"Unable to disconnect from database!");
}
}
}