我在从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());
            }
}
}

最佳答案

您的问题可能是您正在为不存在的部门创建学生。您的范围变量等于departmentnum + 1,这意味着您nextInt()将返回0-departmentnum(含)。

您是部门ID,但是只能从0到departmentnum-1,因为您的while循环为count < departmentnum。要么将while循环设为count <= departmentnum,要么摆脱范围内的+ 1。

也欢迎使用stackoverflow :)

08-28 09:02