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

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

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

问题

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

  1. private void openconnection() {
  2. try {
  3. // 尝试连接数据库
  4. DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
  5. databaseconnection = DriverManager.getConnection("jdbc:derby:" + formattedfolderpath + ";");
  6. databaseconnection.setAutoCommit(false);
  7. currentdb = true;
  8. } catch (SQLException EX) {
  9. try {
  10. // 如果数据库不存在,就创建它
  11. DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
  12. databaseconnection = DriverManager.getConnection("jdbc:derby:" + formattedfolderpath + ";create=true");
  13. databaseconnection.setAutoCommit(false);
  14. currentdb = true;
  15. } catch (SQLException EX2) {
  16. //infoBox("OH MY LAWD", "Error");
  17. }
  18. }
  19. }
  20. JButton open = new JButton();
  21. JFileChooser fc = new JFileChooser();
  22. fc.setCurrentDirectory(new java.io.File("C:/Users/1jenningst/Desktop"));
  23. fc.setDialogTitle("PDF Manager");
  24. fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  25. if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
  26. //
  27. }
  28. String folderpath = fc.getSelectedFile().getAbsolutePath();
  29. try{
  30. formattedfolderpath = new BufferedReader(new FileReader(folderpath));
  31. } catch (Exception e){
  32. //
  33. }
  34. 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/>

  1. try {
  2. // Try to connect to the database
  3. DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
  4. databaseconnection = DriverManager.getConnection(&quot;jdbc:derby:&quot;+formattedfolderpath+&quot;;&quot;);
  5. databaseconnection.setAutoCommit(false);
  6. currentdb = true;
  7. } catch (SQLException EX) {
  8. try {
  9. // Create the DB if it doesn&#39;t exist yet
  10. DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
  11. databaseconnection = DriverManager.getConnection(&quot;jdbc:derby:&quot;+formattedfolderpath+&quot;;create=true&quot;);
  12. databaseconnection.setAutoCommit(false);
  13. currentdb = true;
  14. } catch (SQLException EX2) {
  15. //infoBox(&quot;OH MY LAWD&quot;, &quot;Error&quot;);
  16. }

<br/> and <br/>

  1. JButton open = new JButton();
  2. JFileChooser fc = new JFileChooser();
  3. fc.setCurrentDirectory(new java.io.File(&quot;C:/Users/1jenningst/Desktop&quot;));
  4. fc.setDialogTitle(&quot;PDF Manager&quot;);
  5. fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  6. if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
  7. //
  8. }
  9. String folderpath = fc.getSelectedFile().getAbsolutePath();
  10. try{
  11. formattedfolderpath = new BufferedReader(new FileReader(folderpath));
  12. } catch (Exception e){
  13. //
  14. }
  15. selecting();
  16. }

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

  1. C\users\missouri\desktop\123

变成

  1. C\\users\\missouri\\desktop\\123

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

英文:

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

  1. C\users\missouri\desktop\123

becomes

  1. 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:

确定