如何使文件路径与嵌入式数据库兼容?(Apache Derby 嵌入式)

huangapple go评论75阅读模式
英文:

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

问题

最近我一直在尝试使用 JFileChooser 来选择数据库将被创建的位置;然而,我遇到的问题是,我从 JFileChooser 得到的文件路径中有反斜杠而不是正斜杠,我认为这就是不允许我创建数据库的原因。这是我的代码,以及解决这个问题的尝试。

private void openconnection() {  
    try {
        // 尝试连接数据库
        DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
        databaseconnection = DriverManager.getConnection("jdbc:derby:" + formattedfolderpath + ";");
        databaseconnection.setAutoCommit(false);
        currentdb = true;
    } catch (SQLException EX) {
        try {
            // 如果数据库不存在,就创建它
            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");
        }
    }
}

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();

有人有任何想法,我如何使用变量来完成使用 JFileChooser 的文件路径吗?

谢谢,
Michael

英文:

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. <br/>

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

<br/> and <br/>

    JButton open = new JButton();
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File(&quot;C:/Users/1jenningst/Desktop&quot;));
fc.setDialogTitle(&quot;PDF Manager&quot;);
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? <br/>
Thanks, <br/>
Michael

答案1

得分: 0

Ok, <br/>
我只需要在文件路径中添加两个反斜杠,而不是一个:<br/>

C\users\missouri\desktop\123

变成

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

希望这能帮助,<br/>
Trevor

英文:

Ok, <br/>
I just needed to add two backslashes to the file path, instead of one: <br/>

C\users\missouri\desktop\123

becomes

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

Hope this helps, <br/>
Trevor

huangapple
  • 本文由 发表于 2020年9月6日 19:49:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63763825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定