私は今パッケージjava.sqlを使い始めており、それを使っていくつかの実験を行っています。
私はこれらの2つのテーブルを持っています
最初は:
`user` (
`userID` INT NOT NULL AUTO_INCREMENT ,
`nickname` VARCHAR(20) NULL ,
PRIMARY KEY (`userID`) )
そして2番目は:
`club` (
`clubID` INT NOT NULL AUTO_INCREMENT ,
'clubName` VARCHAR(20) NOT NULL ,
`userID` INT NULL ,
PRIMARY KEY (`clubID`) ,...
ここで、userIDは、最初のテーブルのuserIDに関連付けられた外部キーです。
そして、これは私がやりたいことを説明するコードです。(これは1つのユーザークラブ専用です)
Class.forName("com.mysql.jdbc.Driver");
this.connect = DriverManager.getConnection("jdbc:mysql://localhost/" + this.database + "?user=" + this.user + "&password=" + this.password);
String s;
s = ("insert into " + this.database + ".user (nickname) values (?)");
this.preparedStatement = this.connect.prepareStatement(s);
this.preparedStatement.setString(1, "username");
this.preparedStatement.executeUpdate();
s = ("SELECT userID from " + this.database + ".user where nickname = 'username'");
this.preparedStatement = this.connect.prepareStatement(s);
this.resultSet = this.preparedStatement.executeQuery();
int n=0;
while (resultSet.next())
{
n = this.resultSet.getInt("userID");
}
s = ("insert into " + this.database + ".club (clubName, userID) values (?, ?)");
this.preparedStatement = this.connect.prepareStatement(s);
this.preparedStatement.setString(1, "club");
this.preparedStatement.setInt(2, n);
this.preparedStatement.executeUpdate();
たとえばHashMapに保存されている、より多くのカップル(ユーザー名、クラブ名)に対してこのプロセスを実行する場合、preparedStatement IntefaceのaddBatch()メソッドをどのように使用できますか?
アクションごとに1つずつ3つのバッチを使用する必要があります。1ユーザー名の挿入2ユーザーIDの選択(および記録)3正しいユーザーIDに関連付けられたクラブ名の挿入
または、すべてのプロセスを1つのバッチに含めることができますか?
そして別の質問、resultSet.getInt()メソッドを囲むwhileサイクルを削除しようとすると、エラーが発生するのはなぜですか?
私を助けようとするすべての人に事前に感謝します!