javadatabasepathderbyembedded-database

How to make a file path compatible with an embedded database? (Apache Derby Embedded)


Recently I've been trying to use a JFileChooser to select where a database will be created; however, the problem I've run into is that the file path that I got from the JFileChooser has it has backslashes instead of forward slashes, and I think that this is what isn't allowing me to create the database. Here is my code, and attempt at solving the problem.

        try {
            // Try to connect to the database 
            DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
            databaseconnection = DriverManager.getConnection("jdbc:derby:"+formattedfolderpath+";");
            databaseconnection.setAutoCommit(false);
            currentdb = true;
        } catch (SQLException EX) {
           try {
                // Create the DB if it doesn't exist yet 
                DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
                databaseconnection = DriverManager.getConnection("jdbc:derby:"+formattedfolderpath+";create=true"); 
                databaseconnection.setAutoCommit(false);
                currentdb = true;
            } catch (SQLException EX2) {
                //infoBox("OH MY LAWD", "Error");
            }


and

    JButton open = new JButton();
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("C:/Users/1jenningst/Desktop"));
    fc.setDialogTitle("PDF Manager");
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    
    if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
        //
    }
    
    String folderpath = fc.getSelectedFile().getAbsolutePath();
    
    try{
        formattedfolderpath = new BufferedReader(new FileReader(folderpath));
    } catch (Exception e){
        //
    }
       
    selecting();
}

Anyone have any ideas on how I could use a variable to complete the file path using a JFileChooser?
Thanks,
Michael


Solution

  • Ok,
    I just needed to add two backslashes to the file path, instead of one:

    C\users\missouri\desktop\123
    

    becomes

    C\\users\\missouri\\desktop\\123
    

    Hope this helps,
    Trevor