Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:02:51+00:00 2026-06-08T11:02:51+00:00

I am having an issue inserting data from a Java program. My issue is

  • 0

I am having an issue inserting data from a Java program. My issue is that the user is asked “How many tuples would you like in Department Table?”

They can add however many tuples they want. Then I ask them “How many tuples would you like in the Student Table?” Again however many they want.

Well say I enter in 10 for the student table, sometimes in skips random entries. There are times I will get 1-8, or 1,2,3,5,6,9 or different variations.

I know my while loop is correct so I am not sure what could be causing this so I hope someone can look at my code and spot something that maybe I am missing.

/*
 * 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());
            }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:02:52+00:00Added an answer on June 8, 2026 at 11:02 am

    Your problem might be that you are creating students for departments that don’t exist. Your range variable is equal to departmentnum + 1 which means you’re nextInt() will return 0-departmentnum inclusive.

    You’re department ids, however, only go from 0 to departmentnum – 1 because your while loop is count < departmentnum. Either go have your while loop be count <= departmentnum or get rid of the + 1 in your range.

    Also welcome to stackoverflow 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with inserting JSON into a database, my intention is to
I having issue that content assistant / intellisense is working in methods such as
Hi guys I am having issue I have this query: SELECT * FROM useraccount
Having an issue here that I have tried everything I can think of but
I'm having the following issue: I'm inserting a row to table (say, 'temp') inside
I am having an issue with inserting words with special characters into my database.
I've recently written a custom API for inserting data into my database from multiple
I am having usues with performance when inserting 2000 records in my Core Data
Good afternoon everyone, I am having an issue with a stored procedure inserting an
I have a nightly SSIS process that exports a TON of data from an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.