0

I have the following code, that's basically two methods. My issue is, whenever I call them both one after another in my main class, the scanner does not scan the input from the second method. I tried with next(), nextLine(); I also tried using different scanners, but I don't see why that would have an effect on anything. There's 2 different methods with their own local scanners, what exactly is wrong?

I tried to look around the web and on stackoverflow but I couldn't find anything that could particularly apply in this case.
Thank you!

package database;

    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Scanner;

    public class SQLops {

        public void createTB(){
            Scanner scn = new Scanner(System.in);
            String DBname;
            String TBname;
            Connection c = null;
            Statement stmt = null;

            try {
              System.out.print("What's the name of the database: ");

              DBname=scn.next();

              Class.forName("org.sqlite.JDBC");
              c = DriverManager.getConnection("jdbc:sqlite:"+DBname+".db");
              System.out.println("Opened database " + DBname +" successfully");
              stmt = c.createStatement();

              System.out.print("What's the name of the table you want to create: ");
              TBname = scn.next();

              String sql =  "CREATE TABLE " + "'" + TBname + "'"+
                            "(ID        INT PRIMARY KEY NOT NULL, " +
                            "NUME   TEXT NOT NULL, " +
                            "PRENUME TEXT NOT NULL, " +
                            "FACULTATE TEXT NOT NULL, " +
                            "ORAS TEXT NOT NULL, " +
                            "ANSTUD INT NOT NULL, " +
                            "CAMERA INT NOT NULL, " +
                            "MAIL TEXT NOT NULL)";
              scn.close();
              stmt.executeUpdate(sql);
              System.out.println("Table " + TBname +" was added successfully");
              stmt.close();
              c.close();

            } catch ( Exception e ) {
              System.err.println( e.getClass().getName() + ": " + e.getMessage() );
              System.exit(0);
            }
        }

        public void createRecord(){
            Scanner scn1 = new Scanner(System.in);
            String DBname = null;
            Connection c=null;
            Statement stmt=null;
            Students[] student = new Students[100];
            String[] tableName = new String[100];
            int tbIterator = 0;

            try{
                System.out.print("What's the name of the database: ");
                if(scn1.hasNext())
                DBname=scn1.next();

                Class.forName("org.sqlite.JDBC");
                c = DriverManager.getConnection("jdbc:sqlite:"+DBname+".db");
                c.setAutoCommit(false);

                DatabaseMetaData md = c.getMetaData();
                ResultSet rs = md.getTables(null, null, "%", null);
                while (rs.next()) {
                      tableName[tbIterator] = rs.getString(3);
                      tbIterator++;
                    }

                System.out.println("Opened database " + DBname +" successfully");

                student[0] = new Students(2, "moby", "dick", "braila", "csie", 2, 0, "dragospaul@icloud.com");

                stmt=c.createStatement();

                String sql = "INSERT INTO " + tableName[0] + " (ID, NUME, PRENUME, ORAS, FACULTATE, ANSTUD, CAMERA, MAIL)" +
                             " VALUES (" + "'" + student[0].getID() + "'" + ", " + "'" + student[0].getFname() + "'" + ", " + "'" +student[0].getLname() + "'" + ", " + "'" + student[0].getCity() + "'" +  ", " + "'" +  student[0].getCollege() + "'" + ", " + "'" +  student[0].getYear() + "'" +  ", " + "'" +  student[0].getRoom() + "'" + ", " +  "'" +  student[0].getMail() + "'" +  ")";
                stmt.executeUpdate(sql);

                stmt.close();
                c.commit();
                c.close();
                scn1.close();
            }
            catch (Exception e) {
                System.err.println(e.getClass().getName() +": " + e.getMessage());
                System.exit(0);
            }
            System.out.println("That guy got added to the db");
        }

    }

Also, this is the code in the main class:

package database;

public class dbz {
     public static void main( String args[] )
      { SQLops dbOP = new SQLops();
        dbOP.createTB();
        dbOP.createRecord();

      }
    }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
bamtheboozle
  • 5,847
  • 2
  • 17
  • 31

0 Answers0