Link to home
Start Free TrialLog in
Avatar of VbMonk
VbMonk

asked on

How do I connect to multiple tables in JSP?

I have connected successfully to a database table and executed select statments on the table. I now want to do an insert into a different table but i get a SQL error which says: "SQLException: Result set not positioned properly, perhaps you need to call next(). "
How do I do what I am trying to do?
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VbMonk
VbMonk

ASKER

what does this do? Does it connect to another different table?
this statment assumes a generic connection method, which is reusable then for however many sql call to any number of tables you need to make.  conn, is either out of the api or you can define your own if you require specailization. it would be something like this:
try {
      Class.forName(driver);
           conn = DriverManager.getConnection(connString, userName, password);
        }catch(java.lang.ClassNotFoundException e) {
        }
        catch(java.sql.SQLException e){
        }
Avatar of VbMonk

ASKER

I have something like what reginab has outlined but i get the erro above. The PreparedStatement suggested byTimYates is like a template statement which can be executed a number of times when values are supplied. What if I want to execute a different statment from the one I executed before? for example,
Query1 = "insert into table1 (field1, field2) values (1, 2)"; and the second query statement is:
Query2 = "select * from table2 where id=2"
These are different queries. how can they be executed once?
import java.sql.*;
String Query1 = new String;
String Query2= new String;

Query1 = "insert into table1 (field1, field2) values (1, 2)"; and the second query statement is:
Query2 = "select * from table2 where id=2"

try {
   DriverManager.registerDriver(Class.forName("your-driver-class"));
   Connection con = DriverManager.getConnection("your-connect-string");
   Statement stmt = con.createStatement();
   stmt.execute(Query1);
   stmt.execute(Query2);
   stmt.close();
   con.close();
}
catch(ClassNotFoundException ex) {
   System.err.println("Could not load database driver: " + ex.getMessage());
}
catch(SQLException ex) {
   System.err.println("Database error: " + ex.getMessage());
}
Avatar of VbMonk

ASKER

I'll give you feedback when i test your code tomorrow reginab
Avatar of VbMonk

ASKER

Sorry my query statements need a result table. It doesn't work
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here is something you can play around with.
here are some details for using RowSet


http://www.javaworld.com/javaworld/jw-02-2001/jw-0202-cachedrow.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/CachedRowSet.html


here is an example of what to with a returned recordset when you need to work through a set of records.

https://www.experts-exchange.com/questions/10316891/JDBC-how-to-avoid-null-pointer-exceptions-on-NULL-fields.html?query=java+fill+a+recordset&clearTAFilter=true

 public void executeQuery(String sQuery)
{
//For result returning SQL statements    
          this.theQuery = sQuery;
          ResultRows = new Vector();
          try
          {
            Statement queryStatement = databaseConnection.createStatement();
            ResultSet theResults = queryStatement.executeQuery(theQuery);
            ResultSetMetaData rsmeta = theResults.getMetaData();

            int numColumns = rsmeta.getColumnCount();
            String resultsText = new String("");
            while(theResults.next())
            {
              numRows++;
               Cols = new Vector();
               for(thisCol = 1; thisCol <= numColumns; thisCol++){
                    resultsText = theResults.getString(thisCol).trim();
                    Cols.addElement(resultsText);
               }
               ResultRows.addElement(Cols);
            }
          }
          catch(SQLException sqle)
          {
            System.out.println(sqle.toString());
          }    
     }