I am making a program where I would read data from excel file and store them in tables. Each table would have the name of the file and the data that are in the file. At the beggining of the program I would like to check whether the user give the right path of the file and then check if the table already exist in the database. How I would do this? I mean after reading the path from user I would like to use only the name of the file which i have specified in my code as "tablename" to check whether there is already in the database or not.
My program for getting the connection, giving the path, and testing the path is the below. The correct path that the user should give is like: C:\Users\myuser\Docs\myfile.xls
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/kainourgia", "root", "root");
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of the databases: ");
while (res.next()) {
System.out.println(" " + res.getString(1));
}
String strfullPath = "";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Please enter the fullpath of the file:");
strfullPath = scanner.nextLine();
File f = new File(strfullPath);
if (f.canRead())
break;
System.out.println("Error:File does not exist");
}
String file = strfullPath.substring(strfullPath.lastIndexOf('/') + 1);
System.out.println(file.substring(0, file.indexOf('.')));
System.out.println("The path of the file is:");
String Path = strfullPath.substring(strfullPath.indexOf('\\') - 2,
(strfullPath.lastIndexOf('\\') - 2));
System.out.println(Path);
@SuppressWarnings("unused")
String filename = strfullPath
.substring(strfullPath.lastIndexOf('\\') + 1);
System.out.println("The filename is");
System.out.println(filename);
String[] parts = filename.split("\\.");
String tablename = parts[0];
DatabaseMetaData md2 = (DatabaseMetaData) con.getMetaData();
ResultSet rs = md2.getTables(null, null, "tablename", null);
System.out.println("The name of the table would be:");
System.out.println(tablename);