Javaプログラムからのデータの挿入に問題があります。私の問題は、ユーザーに「部門テーブルにタプルをいくつ入れますか?」という質問が表示されることです。
必要な数のタプルを追加できます。次に、「生徒用テーブルにタプルをいくつ入れますか?」と尋ねます。繰り返しますが、彼らが望む多くの人。
学生テーブルに10を入力すると、ランダムなエントリをスキップすることがあります。1〜8、1、2、3、5、6、9、またはさまざまなバリエーションを取得する場合があります。
私はwhileループが正しいことを知っているので、これを引き起こしている可能性があるものがわからないので、誰かが私のコードを見て、おそらく私が見逃しているものを見つけてくれることを願っています。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package program2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Random;
public class Program2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Variables
int instructornum=0;
int coursenum = 0;
int studentnum = 0;
int count = 0;
int departmentnum =0;
int counts= 0;
int minimum=0;
int x=0;
int teachesnum=0;
// Variables
//Connection to the database
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "university1";
String Driver = "com.mysql.jdbc.Driver";
// Change the userName & password to what ever your credentials are.
String userName = "root";
String password = "121089bn";
//Connection to the database
try {
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader MyReader = new BufferedReader(istream);
Class.forName(Driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected");
// Ask the user how many tuples in department table.
System.out.println("How many tuples would you like to create in Department Table?");
// Takes in as string the number then parse it to an int.
String dept = MyReader.readLine();
departmentnum = Integer.parseInt(dept);
// ****************** Department Table ******************//
while (count < departmentnum)
{
Statement st = conn.createStatement();
// Counts keeps the counter so the Primary Key is unique.
st.executeUpdate("Insert into department (dept_name, building, budget) values ('Dept "+counts+"', 'Voigt', '1200')");
count++;
counts++;
}
// ****************** Student Table ******************//
count=0;
counts=0;
System.out.println("How many tuples would you like to create in Student Table?");
String student = MyReader.readLine();
studentnum = Integer.parseInt(student);
while (count < studentnum)
{
Random ran = new Random();
int range = departmentnum - minimum + 1;
x = ran.nextInt(range) + minimum;
Statement st = conn.createStatement();
st.executeUpdate("Insert into student (id, name, dept_name,tot_cred) select '"+counts+"', 'Student "+counts+"', dept_name, '10' from department where dept_name='Dept "+x+"'");
count++;
counts++;
}
// ****************** Course Table ******************//
x=0;
count=0;
counts=0;
System.out.println("How many tuples would you like to create in Course Table?");
String course = MyReader.readLine();
coursenum = Integer.parseInt(course);
while (count < coursenum)
{
Random ran = new Random();
int range = departmentnum - minimum + 1;
x = ran.nextInt(range) + minimum;
Statement st = conn.createStatement();
st.executeUpdate("Insert into course (course_id, title, dept_name,credits) select '"+counts+"', 'Computer Science "+counts+"', dept_name, '3' from department where dept_name='Dept "+x+"'");
count++;
counts++;
}
// ****************** Instructor Table ******************//
x=0;
count=0;
counts=0;
System.out.println("How many tuples would you like to create in Instructor Table?");
String instructor = MyReader.readLine();
instructornum = Integer.parseInt(instructor);
while (count < instructornum)
{
Random ran = new Random();
int range = departmentnum - minimum + 1;
x = ran.nextInt(range) + minimum;
Statement st = conn.createStatement();
st.executeUpdate("Insert into instructor (id, name, dept_name,salary) select '"+counts+"', 'Instructor "+counts+"', dept_name, '10000' from department where dept_name='Dept "+x+"'");
count++;
counts++;
}
// ****************** Takes Table ******************//
x=0;
count=0;
counts=0;
System.out.println("How many tuples would you like to create in Teaches Table?");
String teaches = MyReader.readLine();
teachesnum = Integer.parseInt(teaches);
while (count < teachesnum)
{
Random ran = new Random();
int range = instructornum - minimum + 1;
x = ran.nextInt(range) + minimum;
Random random = new Random();
int courserange = coursenum - minimum + 1;
int y = random.nextInt(courserange) + minimum;
Statement st = conn.createStatement();
st.executeUpdate("Insert into teaches (id, course_id, semester, year) select id, course_id, 'Spring', '2010' from course, instructor where instructor.id='"+x+"' and course.course_id='"+y+"'");
count++;
counts++;
}
conn.close();
}
catch (Exception e) {
System.err.println("Error");
System.err.println(e.getMessage());
}
}
}