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?
How do I do what I am trying to do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.getConnectio n(connStri ng, userName, password);
}catch(java.lang.ClassNotF oundExcept ion e) {
}
catch(java.sql.SQLExceptio n e){
}
try {
Class.forName(driver);
conn = DriverManager.getConnectio
}catch(java.lang.ClassNotF
}
catch(java.sql.SQLExceptio
}
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?
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.registerDriv er(Class.f orName("yo ur-driver- class"));
Connection con = DriverManager.getConnectio n("your-co nnect-stri ng");
Statement stmt = con.createStatement();
stmt.execute(Query1);
stmt.execute(Query2);
stmt.close();
con.close();
}
catch(ClassNotFoundExcepti on ex) {
System.err.println("Could not load database driver: " + ex.getMessage());
}
catch(SQLException ex) {
System.err.println("Databa se error: " + ex.getMessage());
}
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.registerDriv
Connection con = DriverManager.getConnectio
Statement stmt = con.createStatement();
stmt.execute(Query1);
stmt.execute(Query2);
stmt.close();
con.close();
}
catch(ClassNotFoundExcepti
System.err.println("Could not load database driver: " + ex.getMessage());
}
catch(SQLException ex) {
System.err.println("Databa
}
ASKER
I'll give you feedback when i test your code tomorrow reginab
ASKER
Sorry my query statements need a result table. It doesn't work
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.createS tatement() ;
ResultSet theResults = queryStatement.executeQuer y(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(thisC ol).trim() ;
Cols.addElement(resultsTex t);
}
ResultRows.addElement(Cols );
}
}
catch(SQLException sqle)
{
System.out.println(sqle.to String());
}
}
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.createS
ResultSet theResults = queryStatement.executeQuer
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(thisC
Cols.addElement(resultsTex
}
ResultRows.addElement(Cols
}
}
catch(SQLException sqle)
{
System.out.println(sqle.to
}
}
ASKER