1

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);
Danubian Sailor
  • 1
  • 38
  • 145
  • 223
dedmar
  • 401
  • 3
  • 12
  • 22
  • 1
    Always search SO before you post a duplicate query: [http://stackoverflow.com/questions/2942788/check-if-table-exists][1] [1]: http://stackoverflow.com/questions/2942788/check-if-table-exists – Juned Ahsan May 16 '13 at 10:58
  • how will you select the structure of the table. creating a table every time from java can be tedious – Bhavik Shah May 16 '13 at 10:59
  • yes, i have seen this post but it doesn't work to me. I mean when i use if(rs.next()){ //table exist } else { //table does not exist } and ran the program it does the hole program even if the table already exists. – dedmar May 16 '13 at 11:01

1 Answers1

2

Check DatabaseMetadata api and you should be able to get the information you need. Here is the link to learn more about it :

http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136