2012-07-25 38 views
0

我遇到了從Java程序插入數據的問題。我的問題是,用戶被問到「你在Department Table中需要多少個元組?」MySql 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()); 
      } 
} 
} 
+0

爲什麼在'for'循環會爲您自動增加時使用'while'? 「計數」是什麼意思?你能發佈一個輸入和意外的輸出嗎? – 2012-07-25 13:35:32

+0

大牆o'代碼。幫助我們:你想要解決的症狀是什麼? – yshavit 2012-07-25 13:36:36

+0

計數是插入時的主鍵。只是爲了在表格中沒有任何重複的值。 – 2012-07-25 14:05:29

回答

2

您的問題可能是您正在爲不存在的部門創建學生。你的範圍變量等於departmentnum + 1,這意味着你是nextInt()將返回0-departmentnum(含)。

但是,您是部門ID,因爲您的while循環爲count < departmentnum,所以只能從0到departmentnum - 1。要麼讓你的while循環成爲count <= departmentnum或者擺脫你範圍內的+1。

也歡迎計算器:)

+0

謝謝。雅我可能不會注意到,即使我斷點/調試,廢話了。我真的只是注意插入,我覺得這是問題所在。再次感謝您 布蘭登 – 2012-07-25 14:02:09

0

我覺得你的問題是與隨機部門號。假設用戶輸入5,您將創建5個部門,其ID爲0到4,而您的隨機數發生器選擇0到5之間的數字。