简体   繁体   中英

How to use batch SQL in JDBC with PreparedStatement?

I am getting this error while executing my code, which is supposed to process SQL statements in batch mode:

ORA-03115: unsupported network datatype or representation

This is the code, that when run, generates the error:

public class Login {

    public static void main(String args[]) throws SQLException {
        Connection con = null;
        PreparedStatement stmt = null;
        Statement st = null;
        try {
            Driver driver = new oracle.jdbc.driver.OracleDriver();
            DriverManager.registerDriver(driver);
            System.out.println("coneecting to the database:");
            con = DriverManager.getConnection("url", "user", "pass");
            con.setAutoCommit(false);
            System.out.println("creating statement");
            String sql = "insert into shweta values(?,?)";
            stmt = con.prepareStatement(sql);

            printrows(stmt);

            stmt.setString(1, "love");
            stmt.setInt(2, 45);
            stmt.addBatch();

            stmt.setString(1, "kaun");
            stmt.setInt(2, 29);
            stmt.addBatch();

            int count[] = st.executeBatch();

            con.commit();

            printrows(stmt);

            // printRs(rs);

            st.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (con != null)
                    con.rollback();
            } catch (SQLException en) {
                en.printStackTrace();
            }
        }

        catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (Exception e) {
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        System.out.println("good bye ashish");
    }

    public static void printrows(Statement stmt) throws SQLException {
        String sql = "select * from shweta";
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {
            // Retrieve by column name

            int age = rs.getInt("age");
            String first = rs.getString("auth");

            System.out.print(", Age :    " + age + "\n");
            System.out.print("AUTH :     " + first + "\n");

        }
        System.out.println();
    }
}

I am new to coding and am unsure how to resolve this error, all help appreciated. Thanks.

A user bind or define, or an Oracle function, is not supported by this heterogeneous SQL*Net connection. Upgrade the older version of Oracle and try again.

Statement st is not initialized - and used in executeBatch. Statement stmt which is used to insert shouldn't be changed to run another sql inside printrows.

i find out the problem that is causing the code to throw exception.the reason is that i am
nt intializing my statement as Statement st = con.createstatement(); due to which compiler is throwing error.we cannt pass directly prepared statement object as a parameter to execute the query .so any how we have to use statement and pass its reference to parameter in printrows .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM