0

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

2 回答 2

2

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

但是,您是部门 ID,仅从 0 到部门编号 - 1 因为您的 while 循环是count < departmentnum. 要么让你的while循环成为你的,要么count <= departmentnum摆脱你范围内的+ 1。

也欢迎使用stackoverflow :)

于 2012-07-25T13:52:23.663 回答
0

我认为您遇到的问题是随机部门编号。假设用户输入 5,您将创建 5 个部门,其 ID 为 0 到 4,而随机生成器选择 0 到 5 之间的数字。

于 2012-07-25T13:54:37.880 回答