我有以下代码,基本上有两种方法。我的问题是,每当我在主类中一个接一个地调用它们时,扫描仪就不会扫描第二种方法的输入。我尝试了next(),nextLine(); 我也试过使用不同的扫描仪,但我不知道为什么会对任何东西产生影响。有两种不同的方法和他们自己的本地扫描仪,究竟出了什么问题?
我试图环顾网络和堆栈溢出,但在这种情况下,我找不到任何特别适用的东西。
谢谢!
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");
}
}
此外,这是主类中的代码:
包数据库;
public class dbz {
public static void main( String args[] )
{ SQLops dbOP = new SQLops();
dbOP.createTB();
dbOP.createRecord();
}
}