[pgsql-jp: 27877] テーブル、データの生成時に・・・
Keisuke TATSUMI
dzn02722 @ nifty.com
2002年 11月 6日 (水) 10:41:58 JST
辰己です。
下記のソースにより、テーブルを作成、データ挿入を行っているのですが、
このようなエラーが出てしまいます。
-- error --
Created table TEST
Populating TEST Table with 1000 addresses
Unable to populate TEST table
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.StringBuffer.setCharAt(StringBuffer.java:349)
at org.postgresql.Connection.EscapeSQL(org/postgresql/Connection.java:920)
at org.postgresql.jdbc2.Statement.execute(org/postgresql/jdbc2/Statement.java:282)
at org.postgresql.jdbc2.Statement.executeUpdate(org/postgresql/jdbc2/Statement.java:78)
at org.postgresql.jdbc2.PreparedStatement.executeUpdate(org/postgresql/jdbc2/PreparedStatement.java:122)
at test.populateTestTable(test.java:37)
at test.main(test.java:22)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
上記のエラーは調べたところ、変数のサイズが合わないことから
生じるみたいです。
なので、サイズ変更を行うため、
CREATE TABLE test (TEST_ID int not null, TEST_NAME varchar(40))、
ADDR_STREET1 = getRandomAString(15,40)
上記2箇所のサイズを変えてもエラーに変化ありません。
もしかして見当違い?のような気がするのですが、どこが悪いのでしょうか。
ご教授ください。
以下が環境です。
環境:Windows2000Pro
Java2 SDK v1.4.0_01
Apache Tomcat4.0
PostgreSQL 7.2.1
-- source --
import java.io.*;
import java.sql.*;
import java.util.*;
import java.lang.Math.*;
class test {
private static Connection con;
private static Random rand;
private static final String driverName = "org.postgresql.Driver";
private static final String url = "jdbc:postgresql://localhost:5432/tpcw";
private static final String usr = "tatsumi";
private static final String pwd = "";
private static final int NUM_TEST = 1000;
public static void main(String[] args){
rand = new Random();
getConnection();
deleteTables();
createTables();
populateTestTable();
closeConnection();
}
private static void populateTestTable(){
System.out.println("Populating TEST Table with " + NUM_TEST + " addresses");
String ADDR_STREET1;
try {
PreparedStatement statement = con.prepareStatement
("INSERT INTO TEST(TEST_ID,TEST_NAME) VALUES (?, ?)");
for (int i = 1; i <= NUM_TEST; i++) {
ADDR_STREET1 = getRandomAString(15,40);
statement.setInt(1, i);
statement.setString(2, ADDR_STREET1);
statement.executeUpdate();
}
con.commit();
}
catch (java.lang.Exception ex) {
System.err.println("Unable to populate TEST table");
ex.printStackTrace();
System.exit(1);
}
System.out.print("\n");
}
private static void getConnection(){
try {
Class.forName(driverName);
con = DriverManager.getConnection(url, usr, pwd);
}
catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
private static void closeConnection(){
try {
con.close();
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
private static void deleteTables(){
int i;
int numTables = 1;
for(i = 0; i < numTables; i++){
try {
//Delete each table listed in the tables array
PreparedStatement statement = con.prepareStatement
("DROP TABLE TEST");
statement.executeUpdate();
con.commit();
} catch (java.lang.Exception ex) {
System.out.println("Already dropped table TEST");
}
}
}
private static void createTables(){
try {
PreparedStatement statement = con.prepareStatement
("CREATE TABLE test (TEST_ID int not null, TEST_NAME varchar(40))");
statement.executeUpdate();
con.commit();
System.out.println("Created table TEST");
} catch (java.lang.Exception ex) {
System.out.println("Unable to create table: TEST");
ex.printStackTrace();
System.exit(1);
}
}
private static String getRandomAString(int min, int max){
String newstring = new String();
int i;
final char[] chars = {'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G',
'H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z','!','@','#',
'$','%','^','&','*','(',')','_','-','=','+',
'{','}','[',']','|',':',';',',','.','?','/',
'~',' '}; //79 characters
int strlen = (int) Math.floor(rand.nextDouble()*((max-min)+1));
strlen += min;
for(i = 0; i < strlen; i++){
char c = chars[(int) Math.floor(rand.nextDouble()*79)];
newstring = newstring.concat(String.valueOf(c));
}
return newstring;
}
}
--
Keisuke TATSUMI <dzn02722 @ nifty.com>
pgsql-jp メーリングリストの案内