param1 number(5), 怎么会是number型的?

解决方案 »

  1.   

    不好意思,笔误。是从param2到param4分别插入1000个汉字。
      

  2.   

    我把代码贴一下吧:/**
     * 对PreparedStatement的setCharacterStream进行测试。
     * 
     * 表:
     * CREATE TABLE TABLE1(ID NUMBER(19) NOT NULL, 
        PARAM1 NUMBER(5), PARAM2 VARCHAR2(4000), PARAM3 VARCHAR2(
        4000), PARAM4 VARCHAR2(4000));
        
     * @author 姚炜
     */
    public class PreparedStatementTest extends TestCase {
        
        protected Connection connection;
    private String dbUrl = "jdbc:oracle:thin:@10.10.20.74:1521:yaowei";
    protected String dbUser = "testuser1";
    protected String dbPwd = "test";
    protected String driverClassName = "oracle.jdbc.driver.OracleDriver";
    protected PreparedStatement insertStmt;
    protected PreparedStatement insertStmt2;
    protected PreparedStatement updateStmt;
    private static long newId = 0l;
        /* (non-Javadoc)
         * @see junit.framework.TestCase#setUp()
         */
        protected void setUp() throws Exception {
            super.setUp();
            Class.forName(driverClassName);
            connection = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
            insertStmt = connection.prepareStatement("insert into table1(id,param1,param2,param3,param4) values(?,?,?,?,?)");
            insertStmt2 = connection.prepareStatement("insert into table1(id,param2,param3,param4,param1) values(?,?,?,?,?)");
            updateStmt = connection.prepareStatement("update table1 set param1=?,param2=?,param3=?,param4=? where id=?");
            
        }
        
        /**
         * 在测试参数值时,将所有的setCharacterStream放在最后调用。
         */
        public void testInsert(){
            String content = TestUtil.readContent("message.res");
            //每次运行时,newId都自动加一。
            newId ++;
            
            try {
                insertStmt.setLong(1,newId);
                insertStmt.setShort(2,(short) (newId + 100));
                {
                    StringReader r = new StringReader(content);
                    insertStmt.setCharacterStream(3,r,content.length());
                }
                {
                    StringReader r = new StringReader(content);
                    insertStmt.setCharacterStream(4,r,content.length());
                }
                {
                    StringReader r = new StringReader(content);
                    insertStmt.setCharacterStream(5,r,content.length());
                }
                insertStmt.execute();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 在测试参数值时,在调用了所有的setCharacterStream后,再调用别的setXXX方法。
         */
        public void testInsert2(){
            String content = TestUtil.readContent("message.res");
            //每次运行时,newId都自动加一。
            newId ++;
            
            try {
                insertStmt.setLong(1,newId);
                {
                    StringReader r = new StringReader(content);
                    insertStmt.setCharacterStream(2,r,content.length());
                }
                {
                    StringReader r = new StringReader(content);
                    insertStmt.setCharacterStream(3,r,content.length());
                }
                {
                    StringReader r = new StringReader(content);
                    insertStmt.setCharacterStream(4,r,content.length());
                }
                insertStmt.setShort(5,(short) (newId + 100));
                insertStmt.execute();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 测试修改。
         *
         */
        public void testUpdate(){
            String content = TestUtil.readContent("message.res");
            try {
                updateStmt.setShort(1,(short) 998);
                {
                    StringReader r = new StringReader(content);
                    updateStmt.setCharacterStream(2,r,content.length());
                }
                {
                    StringReader r = new StringReader(content);
                    updateStmt.setCharacterStream(3,r,content.length());
                }
                {
                    StringReader r = new StringReader(content);
                    updateStmt.setCharacterStream(4,r,content.length());
                }
                updateStmt.setLong(5,1);
                updateStmt.execute();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        /* (non-Javadoc)
         * @see junit.framework.TestCase#tearDown()
         */
        protected void tearDown() throws Exception {
            connection.close();
            insertStmt.close();
            updateStmt.close();
            super.tearDown();
        }
    }public class TestUtil {
        
        /**
         * Returns the contents of the file in a byte array.
         */
        public static byte[] getBytesFromFile(File file) throws IOException {
            InputStream is = new FileInputStream(file);
        
            // Get the size of the file
            long length = file.length();
        
            // You cannot create an array using a long type.
            // It needs to be an int type.
            // Before converting to an int type, check
            // to ensure that file is not larger than Integer.MAX_VALUE.
            if (length > Integer.MAX_VALUE) {
                // File is too large
            }
        
            // Create the byte array to hold the data
            byte[] bytes = new byte[(int)length];
        
            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }
        
            // Ensure all the bytes have been read in
            if (offset < bytes.length) {
                throw new IOException("Could not completely read file "+file.getName());
            }
        
            // Close the input stream and return bytes
            is.close();
            return bytes;
        }
        
    /**
         * @param content
         * @return
         */
        public static String readContent(String fileName) {
            String content = null;
            byte[] contentBytes;
            try {
                contentBytes = getBytesFromFile(new File(fileName));
                content = new String(contentBytes);
                System.out.println("default encoding bytes length: " + content.getBytes().length);
                System.out.println("utf8 encoding bytes length:    " + content.getBytes("utf8").length);
                System.out.println("gb2312 encoding bytes length:  " + content.getBytes("gb2312").length);
                System.out.println("string encoding length:        " + content.length());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }        return content;
        }
    }