Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
|
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 4th, 2004, 12:56 AM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default jdbc connection to MSsql & CachedRowSet problems

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.




 
Old September 7th, 2004, 04:14 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to gokul_blr Send a message via Yahoo to gokul_blr
Default

http://www.onjava.com/pub/a/onjava/2...hedrowset.html






Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL to MSSQL PHP connection aspBegineer83 MySQL 4 December 26th, 2007 12:40 AM
how do you configure jdbc connection saf01 Crystal Reports 0 September 12th, 2005 07:24 AM
Connection to Oracle via JDBC swaminathanb Java Databases 3 August 9th, 2005 06:37 AM
connection to oracle thru jdbc venkitaraman Java Databases 5 April 6th, 2005 06:05 AM
Problems with JSP and MSSQL Server 2000 kakram JSP Basics 0 August 22nd, 2003 10:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.