Hi,
i'm just a beginner to J2ee, and at present i'm workin on jdbc.
i'm facing problems in using RowSet/CachedRowSet.
Here is one sample program, which gives exception while running.
Its al about updation using CachedRowSet object, retrieved from from ResultSet.
I'm using rowset.jar file and msmsqlserver.jar files.
Program gives two types of errors when used with different jdbc drivers.
below is the first one.
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;
public class RsRwst {
Connection con;
Statement st;
ResultSet rs;
CachedRowSet crs;
/**
* Creates a new instance of RsRwst
* Also loads the jdbc driver and creates the connection to the database
*/
public RsRwst() {
try {
//Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
/* con=DriverManager.getConnection("jdbc:microsoft:sq lserver://"
+"localhost:1433;DatabaseName="
+"mydatabase","sa","");*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:chandra ","sa",""); //DSN name- chandra
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSI TIVE,
ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from newtable");
} catch(Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
/**
* creates an instance of CachedRowSet and then using its populate method
* gets all the resultset into cache
* Connection remains open.
* Then using CachedRowSet object it tries to update 2nd row in the databse
* using acceptChanges() method
*/
public void fun(){
try {
crs=new CachedRowSet();
crs.populate(rs);
crs.absolute(2);
crs.updateString("name","chandu");
crs.updateRow();
System.out.println("****************************** ******");
//System.out.println("table name: "+crs.getTableName()); // printing null
//crs.setTableName("newtable");
crs.acceptChanges(con); // exception thrown
crs.beforeFirst();
while(crs.next()) {
System.out.println(crs.getString("name"));
}
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
public static void main(String arg[]) {
RsRwst obj=new RsRwst();
obj.fun();
}
}
Below is the output that i got. here i'm using jdbcodbcdriver, and sql. mydatabse is my database n the table name is "newtable", and "mydatabase" is configured in ODBC,.
n the table contains only one column, "name"
************************************
java.sql.SQLException: writeData cannot determine the table name.
at sun.jdbc.rowset.RowSetWriterImpl.initSQLStatements (RowSetWriterImpl.java:596)
at sun.jdbc.rowset.RowSetWriterImpl.writeData(RowSetW riterImpl.java:231)
at sun.jdbc.rowset.CachedRowSet.acceptChanges(CachedR owSet.java:782)
at sun.jdbc.rowset.CachedRowSet.acceptChanges(CachedR owSet.java:808)java.sql.SQLException: writeData cannot determine the table name.
at RsRwst.fun(RsRwst.java:60)
at RsRwst.main(RsRwst.java:75)
=============================================
one small modification in the code : just set the table name to "newtable" , output wil be
************************************
[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'mydatabase.newtable'.
java.sql.SQLException: acceptChanges Failed
java.sql.SQLException: acceptChanges Failed
at sun.jdbc.rowset.CachedRowSet.acceptChanges(CachedR owSet.java:789)
at sun.jdbc.rowset.CachedRowSet.acceptChanges(CachedR owSet.java:808)
at RsRwst.fun(RsRwst.java:60)
at RsRwst.main(RsRwst.java:75)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
Now , if i use SQLServerDiver for connection.
For the first case,without setting the table name it gives the same output as in jdbcodbcdriver case.
n if i set the tablename to newtable, as below.
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;
public class RsRwst {
Connection con;
Statement st;
ResultSet rs;
CachedRowSet crs;
/**
* Creates a new instance of RsRwst
* Also loads the jdbc driver and creates the connection to the database
*/
public RsRwst() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
con=DriverManager.getConnection("jdbc:microsoft:sq lserver://"
+"localhost:1433;DatabaseName="
+"mydatabase","sa","");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSI TIVE,
ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from newtable");
} catch(Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
/**
* creates an instance of CachedRowSet and then using its populate method
* gets all the resultset into cache
* Connection remains open.
* Then using CachedRowSet object it tries to update 2nd row in the databse
* using acceptChanges() method
*/
public void fun(){
try {
crs=new CachedRowSet();
crs.populate(rs);
crs.absolute(2);
crs.updateString("name","chandu");
crs.updateRow();
System.out.println("****************************** ******");
//System.out.println("table name: "+crs.getTableName()); // printing null
crs.setTableName("newtable");
crs.acceptChanges(con); // exception thrown
crs.beforeFirst();
while(crs.next()) {
System.out.println(crs.getString("name"));
}
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
public static void main(String arg[]) {
RsRwst obj=new RsRwst();
obj.fun();
}
}
and the output with exception is,
************************************
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
at com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getImplConn ection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.setupImplCon nection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.<init>(Unkno wn Source)
at com.microsoft.jdbc.base.BasePreparedStatement.<ini t>(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.prepareStat ement(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.prepareStat ement(Unknown Source)
at sun.jdbc.rowset.RowSetWriterImpl.writeData(RowSetW riterImpl.java:234)
at sun.jdbc.rowset.CachedRowSet.acceptChanges(CachedR owSet.java:782)
at sun.jdbc.rowset.CachedRowSet.acceptChanges(CachedR owSet.java:808)
at RsRwst.fun(RsRwst.java:57)
at RsRwst.main(RsRwst.java:72)
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
__________________________________________________ __
This is the probelm i'm facing. i'm using Netbeans editor.
I need help on this.
if u have any document on this topic, please mail me.