MySql Java:我无法解决的奇怪问题

时间:2012-07-25 13:30:51

标签: java mysql tuples

我在从Java程序插入数据时遇到问题。我的问题是用户被问到“你想在Department Table中使用多少个元组?”

他们可以添加他们想要的许多元组。然后我问他们“你在学生桌上想要多少元组?”他们想要的却是多少。

好吧,我输入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());
            }
}
}

2 个答案:

答案 0 :(得分:2)

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

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

也欢迎来到stackoverflow:)

答案 1 :(得分:0)

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